Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Removal of note observer #4150

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 0 additions & 85 deletions server/src/database/notes_observer.go

This file was deleted.

106 changes: 0 additions & 106 deletions server/src/database/notes_observer_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions server/src/realtime/board_sessions_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ const (

func (b *Broker) BroadcastUpdateOnBoardSessionRequest(board, user uuid.UUID, msg BoardSessionRequestEventType) error {
logger.Get().Debugw("broadcasting to board session request", "board", board, "user", user, "msg", msg)
return b.con.Publish(requestSubject(board, user), msg)
return b.Con.Publish(requestSubject(board, user), msg)
}

func (b *Broker) GetBoardSessionRequestChannel(board, user uuid.UUID) chan *BoardSessionRequestEventType {
c, err := b.con.SubscribeToBoardSessionEvents(requestSubject(board, user))
c, err := b.Con.SubscribeToBoardSessionEvents(requestSubject(board, user))
if err != nil {
// TODO: Bubble up this error, so the caller can retry to establish this subscription
logger.Get().Errorw("failed to subscribe to BoardSessionRequestChannel", "err", err)
Expand Down
4 changes: 2 additions & 2 deletions server/src/realtime/boards.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ type BoardEvent struct {

func (b *Broker) BroadcastToBoard(boardID uuid.UUID, msg BoardEvent) error {
logger.Get().Debugw("broadcasting to board", "board", boardID, "msg", msg.Type)
return b.con.Publish(boardsSubject(boardID), msg)
return b.Con.Publish(boardsSubject(boardID), msg)
}

func (b *Broker) GetBoardChannel(boardID uuid.UUID) chan *BoardEvent {
c, err := b.con.SubscribeToBoardEvents(boardsSubject(boardID))
c, err := b.Con.SubscribeToBoardEvents(boardsSubject(boardID))
if err != nil {
// TODO: Bubble up this error, so the caller can retry to establish this subscription
logger.Get().Errorw("failed to subscribe to BoardChannel", "err", err)
Expand Down
2 changes: 1 addition & 1 deletion server/src/realtime/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ type Client interface {

// The Broker enables a user to broadcast and receive events
type Broker struct {
con Client
Con Client
}
4 changes: 2 additions & 2 deletions server/src/realtime/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package realtime

// IsHealthy returns true if the Broker is in a healthy state
func (b *Broker) IsHealthy() bool {
if b == nil || b.con == nil {
if b == nil || b.Con == nil {
return false
}
err := b.con.Publish("health", "test")
err := b.Con.Publish("health", "test")
return err == nil
}
2 changes: 1 addition & 1 deletion server/src/realtime/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ func NewNats(url string) (*Broker, error) {
}

return &Broker{
con: &natsClient{con: c},
Con: &natsClient{con: c},
}, nil
}
2 changes: 1 addition & 1 deletion server/src/realtime/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func NewRedis(server RedisServer) (*Broker, error) {
return &Broker{con: connectRedis(server)}, nil
return &Broker{Con: connectRedis(server)}, nil
}

type redisClient struct {
Expand Down
39 changes: 34 additions & 5 deletions server/src/services/notes/notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package notes
import (
"context"
"database/sql"
"scrumlr.io/server/identifiers"

"scrumlr.io/server/common"
"scrumlr.io/server/identifiers"
"scrumlr.io/server/services"

"github.com/google/uuid"

"scrumlr.io/server/common/dto"
"scrumlr.io/server/common/filter"
"scrumlr.io/server/realtime"

"scrumlr.io/server/database"
Expand All @@ -27,19 +28,18 @@ type Observer interface {
}

type DB interface {
Observer
CreateNote(insert database.NoteInsert) (database.Note, error)
GetNote(id uuid.UUID) (database.Note, error)
GetNotes(board uuid.UUID, columns ...uuid.UUID) ([]database.Note, error)
UpdateNote(caller uuid.UUID, update database.NoteUpdate) (database.Note, error)
DeleteNote(caller uuid.UUID, board uuid.UUID, id uuid.UUID, deleteStack bool) error
GetVotes(f filter.VoteFilter) ([]database.Vote, error)
}

func NewNoteService(db DB, rt *realtime.Broker) services.Notes {
b := new(NoteService)
b.database = db
b.realtime = rt
b.database.AttachObserver((database.NotesObserver)(b))
return b
}

Expand All @@ -50,6 +50,7 @@ func (s *NoteService) Create(ctx context.Context, body dto.NoteCreateRequest) (*
log.Errorw("unable to create note", "board", body.Board, "user", body.User, "error", err)
return nil, common.InternalServerError
}
s.UpdatedNotes(body.Board)
return new(dto.Note).From(note), err
}

Expand Down Expand Up @@ -98,18 +99,46 @@ func (s *NoteService) Update(ctx context.Context, body dto.NoteUpdateRequest) (*
log.Errorw("unable to update note", "error", err, "note", body.ID)
return nil, common.InternalServerError
}
s.UpdatedNotes(body.Board)
return new(dto.Note).From(note), err
}

func (s *NoteService) Delete(ctx context.Context, body dto.NoteDeleteRequest, id uuid.UUID) error {
return s.database.DeleteNote(ctx.Value(identifiers.UserIdentifier).(uuid.UUID), ctx.Value(identifiers.BoardIdentifier).(uuid.UUID), id, body.DeleteStack)
user := ctx.Value(identifiers.UserIdentifier).(uuid.UUID)
board := ctx.Value(identifiers.BoardIdentifier).(uuid.UUID)
note := ctx.Value(identifiers.NoteIdentifier).(uuid.UUID)
voteFilter := filter.VoteFilter{
User: &user,
Board: board,
Note: &note,
}

votes, vErr := s.database.GetVotes(voteFilter)
if vErr != nil {
logger.Get().Errorw("unable to retrieve votes for a note delete", "err", vErr)
}

err := s.database.DeleteNote(user, board, id, body.DeleteStack)
if err != nil {
return err
}

s.DeletedNote(user, board, note, votes, body.DeleteStack)

return err
}

func (s *NoteService) UpdatedNotes(board uuid.UUID, notes []database.Note) {
func (s *NoteService) UpdatedNotes(board uuid.UUID) {
notes, dbErr := s.database.GetNotes(board)
if dbErr != nil {
logger.Get().Errorw("unable to retrieve notes in UpdatedNotes call", "err", dbErr)
}

eventNotes := make([]dto.Note, len(notes))
for index, note := range notes {
eventNotes[index] = *new(dto.Note).From(note)
}

err := s.realtime.BroadcastToBoard(board, realtime.BoardEvent{
Type: realtime.BoardEventNotesUpdated,
Data: eventNotes,
Expand Down
Loading
Loading