Skip to content

Commit e775f33

Browse files
authored
refactor: move logic from board database to board service (#5496)
1 parent 2cfbcad commit e775f33

22 files changed

+1604
-517
lines changed

server/src/.mockery.yaml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ packages:
2828
ReactionService:
2929
ReactionDatabase:
3030

31+
scrumlr.io/server/hash:
32+
interfaces:
33+
Hash:
34+
3135
scrumlr.io/server/health:
3236
interfaces:
3337
HealthService:
@@ -50,11 +54,11 @@ packages:
5054
interfaces:
5155
NotesService:
5256
NotesDatabase:
53-
# todo: reimplement this interface
54-
# scrumlr.io/server/sessions:
55-
# interfaces:
56-
# UserService:
57-
# UserDatabase:
57+
# todo: reimplement this interface
58+
# scrumlr.io/server/sessions:
59+
# interfaces:
60+
# UserService:
61+
# UserDatabase:
5862

5963
scrumlr.io/server/votings:
6064
interfaces:

server/src/api/boards.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strconv"
1010

1111
"go.opentelemetry.io/otel/codes"
12+
"scrumlr.io/server/hash"
1213
"scrumlr.io/server/sessions"
1314

1415
"scrumlr.io/server/boards"
@@ -249,7 +250,7 @@ func (s *Server) joinBoard(w http.ResponseWriter, r *http.Request) {
249250
common.Throw(w, r, common.BadRequestError(err))
250251
return
251252
}
252-
encodedPassphrase := common.Sha512BySalt(body.Passphrase, *b.Salt)
253+
encodedPassphrase := hash.NewHashSha512().HashBySalt(body.Passphrase, *b.Salt)
253254
if encodedPassphrase == *b.Passphrase {
254255
_, err := s.sessions.Create(ctx, sessions.BoardSessionCreateRequest{Board: board, User: user, Role: common.ParticipantRole})
255256
if err != nil {

server/src/api/boards_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010
"time"
1111

12+
"scrumlr.io/server/hash"
1213
"scrumlr.io/server/sessions"
1314

1415
"scrumlr.io/server/boards"
@@ -244,7 +245,7 @@ func (suite *BoardTestSuite) TestJoinBoard() {
244245
boardName := "Test Name"
245246
boardDescription := "Test Description"
246247
salt := "z9YcpBno6azI2ueA"
247-
passphrase := common.Sha512BySalt("123", salt)
248+
passphrase := hash.NewHashSha512().HashBySalt("123", salt)
248249

249250
testParameterBundles := *TestParameterBundles{}.
250251
Append("Successfully join board", http.StatusSeeOther, nil, true, false, suite.createBoard(&boardName, &boardDescription, boards.Public, nil, nil)).

server/src/boards/database.go

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ package boards
22

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

8-
"scrumlr.io/server/sessions"
97
"scrumlr.io/server/votings"
108

119
"github.com/google/uuid"
1210
"github.com/uptrace/bun"
13-
"scrumlr.io/server/columns"
1411
"scrumlr.io/server/common"
1512
"scrumlr.io/server/identifiers"
1613
)
@@ -26,38 +23,12 @@ func NewBoardDatabase(database *bun.DB) BoardDatabase {
2623
return db
2724
}
2825

29-
func (d *DB) CreateBoard(ctx context.Context, creator uuid.UUID, board DatabaseBoardInsert, columns []columns.DatabaseColumnInsert) (DatabaseBoard, error) {
30-
boardInsert := d.db.NewInsert().
31-
Model(&board).
32-
Returning("*")
33-
34-
if board.AccessPolicy == ByPassphrase && (board.Passphrase == nil || board.Salt == nil) {
35-
return DatabaseBoard{}, errors.New("passphrase or salt may not be empty")
36-
} else if board.AccessPolicy != ByPassphrase && (board.Passphrase != nil || board.Salt != nil) {
37-
return DatabaseBoard{}, errors.New("passphrase or salt should not be set for policies except 'BY_PASSPHRASE'")
38-
}
39-
40-
session := sessions.DatabaseBoardSessionInsert{User: creator, Role: common.OwnerRole}
41-
26+
func (d *DB) CreateBoard(ctx context.Context, board DatabaseBoardInsert) (DatabaseBoard, error) {
4227
var b DatabaseBoard
43-
query := d.db.NewSelect().With("createdBoard", boardInsert)
44-
if len(columns) > 0 {
45-
for index := range columns {
46-
newColumnIndex := index
47-
columns[index].Index = newColumnIndex
48-
}
49-
50-
query = query.With("createdColumns", d.db.NewInsert().
51-
Model(&columns).
52-
Value("board", "(SELECT id FROM \"createdBoard\")"))
53-
}
54-
err := query.
55-
With("createdSession", d.db.NewInsert().
56-
Model(&session).
57-
Value("board", "(SELECT id FROM \"createdBoard\")")).
58-
Table("createdBoard").
59-
Column("*").
60-
Scan(ctx, &b)
28+
_, err := d.db.NewInsert().
29+
Model(&board).
30+
Returning("*").
31+
Exec(ctx, &b)
6132

6233
return b, err
6334
}
@@ -86,18 +57,6 @@ func (d *DB) UpdateBoard(ctx context.Context, update DatabaseBoardUpdate) (Datab
8657
query.Column("description")
8758
}
8859
if update.AccessPolicy != nil {
89-
if *update.AccessPolicy == ByPassphrase && (update.Passphrase == nil || update.Salt == nil) {
90-
return DatabaseBoard{}, errors.New("passphrase and salt should be set when access policy is updated")
91-
} else if *update.AccessPolicy != ByPassphrase && (update.Passphrase != nil || update.Salt != nil) {
92-
return DatabaseBoard{}, errors.New("passphrase and salt should not be set if access policy is defined as 'BY_PASSPHRASE'")
93-
}
94-
95-
if *update.AccessPolicy == ByInvite {
96-
query.Where("access_policy = ?", ByInvite)
97-
} else {
98-
query.Where("access_policy <> ?", ByInvite)
99-
}
100-
10160
query.Column("access_policy", "passphrase", "salt")
10261
}
10362
if update.ShowAuthors != nil {

0 commit comments

Comments
 (0)