Skip to content

Commit

Permalink
use newer event approach
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanfaerman committed Jan 31, 2024
1 parent 73801a1 commit a365261
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
24 changes: 12 additions & 12 deletions internal/services/event.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package services

import (
"bytes"
"context"
"encoding/gob"
"encoding/json"
"fmt"
"reflect"
"runtime"
"time"

"github.com/r3labs/sse/v2"
"github.com/ryanfaerman/netctl/internal/dao"
"github.com/ryanfaerman/netctl/internal/events"
"github.com/ryanfaerman/netctl/internal/models"
"github.com/ryanfaerman/netctl/workgroup"
)
Expand All @@ -33,33 +33,33 @@ var Event = &event{
}

func (e *event) Create(ctx context.Context, stream string, evt any) error {
var (
b bytes.Buffer
p any
)

p = &evt
if err := gob.NewEncoder(&b).Encode(p); err != nil {
return err
d, err := json.Marshal(evt)
if err != nil {
global.log.Error("unable to marshal event", "error", err)
}

id, err := global.dao.CreateEvent(ctx, dao.CreateEventParams{
StreamID: stream,
AccountID: 1,
EventType: fmt.Sprintf("%T", evt),
EventData: b.Bytes(),
EventData: d,
})
if err != nil {
return err
}

j, err := events.Decode(fmt.Sprintf("%T", evt), d)
if err != nil {
return err
}

event := models.Event{
ID: id,
At: time.Now(),
StreamID: stream,
AccountID: 1,
Name: fmt.Sprintf("%T", evt),
Event: evt,
Event: j,
}

go e.Publish(context.Background(), event)
Expand Down
13 changes: 3 additions & 10 deletions internal/services/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services
import (
"context"
"errors"
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -59,14 +60,6 @@ func (net) GetNetFromSession(ctx context.Context, sessionID string) (*models.Net
return n, n.Replay(ctx, sessionID)
}

// func (net) Create(ctx context.Context, name string) (*models.Net, error) {
// id, err := global.dao.CreateNetAndReturnId(ctx, name)
// if err != nil {
// return nil, err
// }
// return models.FindNetById(ctx, id)
// }

func (net) Create(ctx context.Context, m *models.Net) (*models.Net, error) {
if err := Validation.Apply(m); err != nil {
return m, err
Expand All @@ -91,7 +84,7 @@ func init() {
}

func (n net) ValidateCheckinHeard(ctx context.Context, event models.Event) error {
if e, ok := event.Event.(events.NetCheckinHeard); ok {
if e, ok := event.Event.(*events.NetCheckinHeard); ok {
m := &models.NetCheckin{
ID: e.ID,
}
Expand All @@ -104,7 +97,7 @@ func (n net) ValidateCheckinHeard(ctx context.Context, event models.Event) error

}
global.log.Debug("invalid event", "event", event)
return errors.New("invalid event")
return fmt.Errorf("invalid event %T", event.Event)
}

func (n net) ValidateCheckin(ctx context.Context, stream string, checkin *models.NetCheckin) error {
Expand Down

0 comments on commit a365261

Please sign in to comment.