Skip to content

Commit fcc0154

Browse files
authored
refactor: move logic from reaction db to reaction service (#5419)
1 parent 2c5f27f commit fcc0154

File tree

11 files changed

+561
-227
lines changed

11 files changed

+561
-227
lines changed

server/src/api/reactions.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ func (s *Server) createReaction(w http.ResponseWriter, r *http.Request) {
7171

7272
// user is filled from context
7373
body.User = user
74+
body.Board = board
7475

75-
reaction, err := s.reactions.Create(ctx, board, body)
76+
reaction, err := s.reactions.Create(ctx, body)
7677
if err != nil {
7778
span.SetStatus(codes.Error, "failed to create reaction")
7879
span.RecordError(err)

server/src/common/error.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ func ForbiddenError(err error) *APIError {
4141
}
4242
}
4343

44+
func ConflictError(err error) *APIError {
45+
return &APIError{
46+
Err: err,
47+
StatusCode: http.StatusConflict,
48+
StatusText: "Conflict",
49+
ErrorText: err.Error(),
50+
}
51+
}
52+
4453
var NotFoundError = &APIError{StatusCode: http.StatusNotFound, StatusText: "Resource not found."}
4554
var InternalServerError = &APIError{StatusCode: http.StatusInternalServerError, StatusText: "Internal server error."}
4655

server/src/initialize/otel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"go.opentelemetry.io/otel/sdk/metric"
1818
"go.opentelemetry.io/otel/sdk/resource"
1919
"go.opentelemetry.io/otel/sdk/trace"
20-
semconv "go.opentelemetry.io/otel/semconv/v1.34.0"
20+
semconv "go.opentelemetry.io/otel/semconv/v1.37.0"
2121
"google.golang.org/grpc"
2222
"google.golang.org/grpc/credentials/insecure"
2323
"scrumlr.io/server/logger"

server/src/reactions/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
type ReactionService interface {
1010
Get(ctx context.Context, id uuid.UUID) (*Reaction, error)
1111
GetAll(ctx context.Context, boardId uuid.UUID) ([]*Reaction, error)
12-
Create(ctx context.Context, board uuid.UUID, body ReactionCreateRequest) (*Reaction, error)
12+
Create(ctx context.Context, body ReactionCreateRequest) (*Reaction, error)
1313
Delete(ctx context.Context, board, user, id uuid.UUID) error
1414
Update(ctx context.Context, board, user, id uuid.UUID, body ReactionUpdateTypeRequest) (*Reaction, error)
1515
}

server/src/reactions/database.go

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package reactions
22

33
import (
44
"context"
5-
"errors"
65

76
"github.com/google/uuid"
87
"github.com/uptrace/bun"
@@ -60,20 +59,8 @@ func (d *DB) GetAllForNote(ctx context.Context, note uuid.UUID) ([]DatabaseReact
6059

6160
// Create inserts a new reaction
6261
func (d *DB) Create(ctx context.Context, board uuid.UUID, insert DatabaseReactionInsert) (DatabaseReaction, error) {
63-
var currentNoteReactions, err = d.GetAllForNote(ctx, insert.Note)
64-
if err != nil {
65-
return DatabaseReaction{}, err
66-
}
67-
68-
// check if user has already made a reaction on this note
69-
for _, r := range currentNoteReactions {
70-
if r.User == insert.User {
71-
return DatabaseReaction{}, errors.New("cannot make multiple reactions on the same note by the same user")
72-
}
73-
}
74-
7562
var reaction DatabaseReaction
76-
_, err = d.db.NewInsert().
63+
_, err := d.db.NewInsert().
7764
Model(&insert).
7865
Returning("*").
7966
Exec(common.ContextWithValues(ctx, "Database", d, identifiers.BoardIdentifier, board), &reaction)
@@ -82,42 +69,25 @@ func (d *DB) Create(ctx context.Context, board uuid.UUID, insert DatabaseReactio
8269
}
8370

8471
// Delete deletes a reaction
85-
func (d *DB) Delete(ctx context.Context, board, user, id uuid.UUID) error {
86-
var reaction, err = d.Get(ctx, id)
87-
if err != nil {
88-
return err
89-
}
90-
91-
if reaction.User != user {
92-
return common.ForbiddenError(errors.New("forbidden"))
93-
}
94-
_, err = d.db.NewDelete().
72+
func (d *DB) Delete(ctx context.Context, id uuid.UUID) error {
73+
_, err := d.db.NewDelete().
9574
Model((*DatabaseReaction)(nil)).
9675
Where("id = ?", id).
97-
Exec(common.ContextWithValues(ctx, "Database", d, identifiers.BoardIdentifier, board, identifiers.ReactionIdentifier, id))
76+
Exec(common.ContextWithValues(ctx, "Database", d, identifiers.ReactionIdentifier, id))
9877

9978
return err
10079
}
10180

10281
// Update updates the reaction type
103-
func (d *DB) Update(ctx context.Context, board, user, id uuid.UUID, update DatabaseReactionUpdate) (DatabaseReaction, error) {
104-
var r, err = d.Get(ctx, id)
105-
if err != nil {
106-
return DatabaseReaction{}, err
107-
}
108-
109-
if r.User != user {
110-
return DatabaseReaction{}, common.ForbiddenError(errors.New("forbidden"))
111-
}
112-
82+
func (d *DB) Update(ctx context.Context, id uuid.UUID, update DatabaseReactionUpdate) (DatabaseReaction, error) {
11383
var reaction DatabaseReaction
114-
_, err = d.db.
84+
_, err := d.db.
11585
NewUpdate().
11686
Model(&reaction).
11787
Set("reaction_type = ?", update.ReactionType).
11888
Where("id = ?", id).
11989
Returning("*").
120-
Exec(common.ContextWithValues(ctx, "Database", d, identifiers.BoardIdentifier, board, identifiers.ReactionIdentifier, reaction))
90+
Exec(common.ContextWithValues(ctx, "Database", d, identifiers.ReactionIdentifier, reaction))
12191

12292
return reaction, err
12393
}

server/src/reactions/database_test.go

Lines changed: 6 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package reactions
22

33
import (
44
"context"
5-
"database/sql"
6-
"errors"
75
"log"
86
"testing"
97

@@ -62,31 +60,13 @@ func (suite *DatabaseReactionTestSuite) Test_Database_CreateReaction() {
6260
assert.Equal(t, reaction.ReactionType, dbReaction.ReactionType)
6361
}
6462

65-
func (suite *DatabaseReactionTestSuite) Test_Database_CreateReaction_MultipleReaction() {
66-
t := suite.T()
67-
database := NewReactionsDatabase(suite.db)
68-
69-
reactionInsert := DatabaseReactionInsert{
70-
Note: suite.notes["Update"].id,
71-
User: suite.users["Santa"].id,
72-
ReactionType: Like,
73-
}
74-
75-
dbReaction, err := database.Create(context.Background(), suite.boards["Write"].id, reactionInsert)
76-
77-
assert.Equal(t, DatabaseReaction{}, dbReaction)
78-
assert.Equal(t, errors.New("cannot make multiple reactions on the same note by the same user"), err)
79-
}
80-
8163
func (suite *DatabaseReactionTestSuite) Test_Database_Delete() {
8264
t := suite.T()
8365
database := NewReactionsDatabase(suite.db)
8466

85-
boardId := suite.boards["Write"].id
86-
userId := suite.users["Stan"].id
8767
reactionId := suite.reactions["Delete"].ID
8868

89-
err := database.Delete(context.Background(), boardId, userId, reactionId)
69+
err := database.Delete(context.Background(), reactionId)
9070

9171
assert.Nil(t, err)
9272
}
@@ -95,35 +75,17 @@ func (suite *DatabaseReactionTestSuite) Test_Database_Delete_NotFound() {
9575
t := suite.T()
9676
database := NewReactionsDatabase(suite.db)
9777

98-
boardId := suite.boards["Write"].id
99-
userId := suite.users["Stan"].id
10078
reactionId := uuid.New()
10179

102-
err := database.Delete(context.Background(), boardId, userId, reactionId)
103-
104-
assert.NotNil(t, err)
105-
assert.Equal(t, sql.ErrNoRows, err)
106-
}
107-
108-
func (suite *DatabaseReactionTestSuite) Test_Database_Delete_UserError() {
109-
t := suite.T()
110-
database := NewReactionsDatabase(suite.db)
111-
112-
boardId := suite.boards["Write"].id
113-
userId := suite.users["Stan"].id
114-
reactionId := suite.reactions["Update"].ID
115-
116-
err := database.Delete(context.Background(), boardId, userId, reactionId)
80+
err := database.Delete(context.Background(), reactionId)
11781

118-
assert.NotNil(t, err)
119-
assert.Equal(t, common.ForbiddenError(errors.New("forbidden")), err)
82+
assert.Nil(t, err)
12083
}
12184

12285
func (suite *DatabaseReactionTestSuite) Test_Database_Update() {
12386
t := suite.T()
12487
database := NewReactionsDatabase(suite.db)
12588

126-
boardId := suite.boards["Write"].id
12789
userId := suite.users["Santa"].id
12890
noteId := suite.notes["Update"].id
12991
reactionId := suite.reactions["Update"].ID
@@ -135,7 +97,7 @@ func (suite *DatabaseReactionTestSuite) Test_Database_Update() {
13597
ReactionType: Poop,
13698
}
13799

138-
dbReaction, err := database.Update(context.Background(), boardId, userId, reactionId, DatabaseReactionUpdate{ReactionType: Poop})
100+
dbReaction, err := database.Update(context.Background(), reactionId, DatabaseReactionUpdate{ReactionType: Poop})
139101

140102
assert.Nil(t, err)
141103
assert.Equal(t, reaction.ID, dbReaction.ID)
@@ -148,30 +110,12 @@ func (suite *DatabaseReactionTestSuite) Test_Database_Update_NotFound() {
148110
t := suite.T()
149111
database := NewReactionsDatabase(suite.db)
150112

151-
boardId := suite.boards["Write"].id
152-
userId := suite.users["Santa"].id
153113
reactionId := uuid.New()
154114

155-
dbReaction, err := database.Update(context.Background(), boardId, userId, reactionId, DatabaseReactionUpdate{ReactionType: Poop})
115+
dbReaction, err := database.Update(context.Background(), reactionId, DatabaseReactionUpdate{ReactionType: Poop})
156116

157117
assert.Equal(t, DatabaseReaction{}, dbReaction)
158-
assert.NotNil(t, err)
159-
assert.Equal(t, sql.ErrNoRows, err)
160-
}
161-
162-
func (suite *DatabaseReactionTestSuite) Test_Database_Update_UserError() {
163-
t := suite.T()
164-
database := NewReactionsDatabase(suite.db)
165-
166-
boardId := suite.boards["Write"].id
167-
userId := suite.users["Stan"].id
168-
reactionId := suite.reactions["Update"].ID
169-
170-
dbReaction, err := database.Update(context.Background(), boardId, userId, reactionId, DatabaseReactionUpdate{ReactionType: Poop})
171-
172-
assert.Equal(t, DatabaseReaction{}, dbReaction)
173-
assert.NotNil(t, err)
174-
assert.Equal(t, common.ForbiddenError(errors.New("forbidden")), err)
118+
assert.Nil(t, err)
175119
}
176120

177121
func (suite *DatabaseReactionTestSuite) Test_Database_GetReaction() {

0 commit comments

Comments
 (0)