Skip to content
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
3 changes: 2 additions & 1 deletion server/src/api/reactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ func (s *Server) createReaction(w http.ResponseWriter, r *http.Request) {

// user is filled from context
body.User = user
body.Board = board

reaction, err := s.reactions.Create(ctx, board, body)
reaction, err := s.reactions.Create(ctx, body)
if err != nil {
span.SetStatus(codes.Error, "failed to create reaction")
span.RecordError(err)
Expand Down
9 changes: 9 additions & 0 deletions server/src/common/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ func ForbiddenError(err error) *APIError {
}
}

func ConflictError(err error) *APIError {
return &APIError{
Err: err,
StatusCode: http.StatusConflict,
StatusText: "Conflict",
ErrorText: err.Error(),
}
}

var NotFoundError = &APIError{StatusCode: http.StatusNotFound, StatusText: "Resource not found."}
var InternalServerError = &APIError{StatusCode: http.StatusInternalServerError, StatusText: "Internal server error."}

Expand Down
2 changes: 1 addition & 1 deletion server/src/initialize/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.34.0"
semconv "go.opentelemetry.io/otel/semconv/v1.37.0"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"scrumlr.io/server/logger"
Expand Down
2 changes: 1 addition & 1 deletion server/src/reactions/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type ReactionService interface {
Get(ctx context.Context, id uuid.UUID) (*Reaction, error)
GetAll(ctx context.Context, boardId uuid.UUID) ([]*Reaction, error)
Create(ctx context.Context, board uuid.UUID, body ReactionCreateRequest) (*Reaction, error)
Create(ctx context.Context, body ReactionCreateRequest) (*Reaction, error)
Delete(ctx context.Context, board, user, id uuid.UUID) error
Update(ctx context.Context, board, user, id uuid.UUID, body ReactionUpdateTypeRequest) (*Reaction, error)
}
Expand Down
44 changes: 7 additions & 37 deletions server/src/reactions/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package reactions

import (
"context"
"errors"

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

// Create inserts a new reaction
func (d *DB) Create(ctx context.Context, board uuid.UUID, insert DatabaseReactionInsert) (DatabaseReaction, error) {
var currentNoteReactions, err = d.GetAllForNote(ctx, insert.Note)
if err != nil {
return DatabaseReaction{}, err
}

// check if user has already made a reaction on this note
for _, r := range currentNoteReactions {
if r.User == insert.User {
return DatabaseReaction{}, errors.New("cannot make multiple reactions on the same note by the same user")
}
}

var reaction DatabaseReaction
_, err = d.db.NewInsert().
_, err := d.db.NewInsert().
Model(&insert).
Returning("*").
Exec(common.ContextWithValues(ctx, "Database", d, identifiers.BoardIdentifier, board), &reaction)
Expand All @@ -82,42 +69,25 @@ func (d *DB) Create(ctx context.Context, board uuid.UUID, insert DatabaseReactio
}

// Delete deletes a reaction
func (d *DB) Delete(ctx context.Context, board, user, id uuid.UUID) error {
var reaction, err = d.Get(ctx, id)
if err != nil {
return err
}

if reaction.User != user {
return common.ForbiddenError(errors.New("forbidden"))
}
_, err = d.db.NewDelete().
func (d *DB) Delete(ctx context.Context, id uuid.UUID) error {
_, err := d.db.NewDelete().
Model((*DatabaseReaction)(nil)).
Where("id = ?", id).
Exec(common.ContextWithValues(ctx, "Database", d, identifiers.BoardIdentifier, board, identifiers.ReactionIdentifier, id))
Exec(common.ContextWithValues(ctx, "Database", d, identifiers.ReactionIdentifier, id))

return err
}

// Update updates the reaction type
func (d *DB) Update(ctx context.Context, board, user, id uuid.UUID, update DatabaseReactionUpdate) (DatabaseReaction, error) {
var r, err = d.Get(ctx, id)
if err != nil {
return DatabaseReaction{}, err
}

if r.User != user {
return DatabaseReaction{}, common.ForbiddenError(errors.New("forbidden"))
}

func (d *DB) Update(ctx context.Context, id uuid.UUID, update DatabaseReactionUpdate) (DatabaseReaction, error) {
var reaction DatabaseReaction
_, err = d.db.
_, err := d.db.
NewUpdate().
Model(&reaction).
Set("reaction_type = ?", update.ReactionType).
Where("id = ?", id).
Returning("*").
Exec(common.ContextWithValues(ctx, "Database", d, identifiers.BoardIdentifier, board, identifiers.ReactionIdentifier, reaction))
Exec(common.ContextWithValues(ctx, "Database", d, identifiers.ReactionIdentifier, reaction))

return reaction, err
}
68 changes: 6 additions & 62 deletions server/src/reactions/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package reactions

import (
"context"
"database/sql"
"errors"
"log"
"testing"

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

func (suite *DatabaseReactionTestSuite) Test_Database_CreateReaction_MultipleReaction() {
t := suite.T()
database := NewReactionsDatabase(suite.db)

reactionInsert := DatabaseReactionInsert{
Note: suite.notes["Update"].id,
User: suite.users["Santa"].id,
ReactionType: Like,
}

dbReaction, err := database.Create(context.Background(), suite.boards["Write"].id, reactionInsert)

assert.Equal(t, DatabaseReaction{}, dbReaction)
assert.Equal(t, errors.New("cannot make multiple reactions on the same note by the same user"), err)
}

func (suite *DatabaseReactionTestSuite) Test_Database_Delete() {
t := suite.T()
database := NewReactionsDatabase(suite.db)

boardId := suite.boards["Write"].id
userId := suite.users["Stan"].id
reactionId := suite.reactions["Delete"].ID

err := database.Delete(context.Background(), boardId, userId, reactionId)
err := database.Delete(context.Background(), reactionId)

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

boardId := suite.boards["Write"].id
userId := suite.users["Stan"].id
reactionId := uuid.New()

err := database.Delete(context.Background(), boardId, userId, reactionId)

assert.NotNil(t, err)
assert.Equal(t, sql.ErrNoRows, err)
}

func (suite *DatabaseReactionTestSuite) Test_Database_Delete_UserError() {
t := suite.T()
database := NewReactionsDatabase(suite.db)

boardId := suite.boards["Write"].id
userId := suite.users["Stan"].id
reactionId := suite.reactions["Update"].ID

err := database.Delete(context.Background(), boardId, userId, reactionId)
err := database.Delete(context.Background(), reactionId)

assert.NotNil(t, err)
assert.Equal(t, common.ForbiddenError(errors.New("forbidden")), err)
assert.Nil(t, err)
}

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

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

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

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

boardId := suite.boards["Write"].id
userId := suite.users["Santa"].id
reactionId := uuid.New()

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

assert.Equal(t, DatabaseReaction{}, dbReaction)
assert.NotNil(t, err)
assert.Equal(t, sql.ErrNoRows, err)
}

func (suite *DatabaseReactionTestSuite) Test_Database_Update_UserError() {
t := suite.T()
database := NewReactionsDatabase(suite.db)

boardId := suite.boards["Write"].id
userId := suite.users["Stan"].id
reactionId := suite.reactions["Update"].ID

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

assert.Equal(t, DatabaseReaction{}, dbReaction)
assert.NotNil(t, err)
assert.Equal(t, common.ForbiddenError(errors.New("forbidden")), err)
assert.Nil(t, err)
}

func (suite *DatabaseReactionTestSuite) Test_Database_GetReaction() {
Expand Down
Loading
Loading