Skip to content

Commit 772d388

Browse files
Matin Gohar FarMatin Gohar Far
authored andcommitted
implemented PR suggestions
1 parent f2dc1c8 commit 772d388

13 files changed

Lines changed: 234 additions & 347 deletions

server/src/api/votings.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ func (s *Server) updateVoting(w http.ResponseWriter, r *http.Request) {
8787
board := ctx.Value(identifiers.BoardIdentifier).(uuid.UUID)
8888
id := ctx.Value(identifiers.VotingIdentifier).(uuid.UUID)
8989

90+
var body votings.VotingCloseRequest
91+
if err := render.Decode(r, &body); err != nil {
92+
span.SetStatus(codes.Error, "unable to decode body")
93+
span.RecordError(err)
94+
common.Throw(w, r, common.BadRequestError(err))
95+
return
96+
}
97+
body.ID = id
98+
body.Board = board
99+
90100
notes, err := s.notes.GetAll(ctx, board)
91101
if err != nil {
92102
span.SetStatus(codes.Error, "failed to get notes")
@@ -110,25 +120,7 @@ func (s *Server) updateVoting(w http.ResponseWriter, r *http.Request) {
110120
})
111121
}
112122

113-
var body votings.VotingCloseRequest
114-
115-
if r.ContentLength != 0 {
116-
if err := render.Decode(r, &body); err != nil {
117-
span.SetStatus(codes.Error, "unable to decode body")
118-
span.RecordError(err)
119-
common.Throw(w, r, common.BadRequestError(err))
120-
return
121-
}
122-
}
123-
body.ID = id
124-
body.Board = board
125-
126-
var voting *votings.Voting
127-
if body.Status == votings.Canceled {
128-
voting, err = s.votings.Cancel(ctx, id, board, affectedNotes)
129-
} else {
130-
voting, err = s.votings.Close(ctx, id, board, affectedNotes)
131-
}
123+
voting, err := s.votings.Update(ctx, id, board, affectedNotes, body.Status)
132124

133125
if err != nil {
134126
span.SetStatus(codes.Error, "failed to update voting")

server/src/api/votings_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,27 +94,28 @@ func (suite *VotingTestSuite) TestCloseVoting() {
9494
s.votings = votingMock
9595
s.notes = notesMock
9696

97-
req := technical_helper.NewTestRequestBuilder("PUT", "/", nil)
97+
votingStatus := votings.Closed
98+
req := technical_helper.NewTestRequestBuilder("PUT", "/", strings.NewReader(fmt.Sprintf(`{"status": "%s"}`, votingStatus)))
9899
req.Req = logger.InitTestLoggerRequest(req.Request())
99100
req.AddToContext(identifiers.BoardIdentifier, boardId).
100101
AddToContext(identifiers.VotingIdentifier, votingId)
101102
rr := httptest.NewRecorder()
102103

103104
notesMock.EXPECT().GetAll(mock.Anything, boardId).Return([]*notes.Note{}, nil)
104105

105-
votingMock.EXPECT().Close(mock.Anything, votingId, boardId, []votings.Note(nil)).
106+
votingMock.EXPECT().Update(mock.Anything, votingId, boardId, []votings.Note(nil), votings.Closed).
106107
Return(&votings.Voting{Status: votings.Closed}, tt.err)
107108

108109
s.updateVoting(rr, req.Request())
109110
suite.Equal(tt.expectedCode, rr.Result().StatusCode)
110111
votingMock.AssertExpectations(suite.T())
111-
votingMock.AssertNumberOfCalls(suite.T(), "Close", 1)
112+
votingMock.AssertNumberOfCalls(suite.T(), "Update", 1)
112113
})
113114
}
114115

115116
}
116117

117-
func (suite *VotingTestSuite) TestCancelVoting() {
118+
func (suite *VotingTestSuite) TestAbortVoting() {
118119

119120
testParameterBundles := *TestParameterBundles{}.
120121
Append("all ok", http.StatusOK, nil, false, false, nil).
@@ -123,7 +124,6 @@ func (suite *VotingTestSuite) TestCancelVoting() {
123124
for _, tt := range testParameterBundles {
124125
suite.Run(tt.name, func() {
125126
s := new(Server)
126-
//s.basePath = "/"
127127
votingMock := votings.NewMockVotingService(suite.T())
128128
notesMock := notes.NewMockNotesService(suite.T())
129129

@@ -132,21 +132,22 @@ func (suite *VotingTestSuite) TestCancelVoting() {
132132
s.votings = votingMock
133133
s.notes = notesMock
134134

135-
req := technical_helper.NewTestRequestBuilder("PUT", "/", strings.NewReader(`{"status": "ABORTED"}`))
135+
votingStatus := votings.Aborted
136+
req := technical_helper.NewTestRequestBuilder("PUT", "/", strings.NewReader(fmt.Sprintf(`{"status": "%s"}`, votingStatus)))
136137
req.Req = logger.InitTestLoggerRequest(req.Request())
137138
req.AddToContext(identifiers.BoardIdentifier, boardId).
138139
AddToContext(identifiers.VotingIdentifier, votingId)
139140

140141
notesMock.EXPECT().GetAll(mock.Anything, boardId).Return([]*notes.Note{}, nil)
141142

142-
votingMock.EXPECT().Cancel(mock.Anything, votingId, boardId, []votings.Note(nil)).
143-
Return(&votings.Voting{Status: votings.Canceled}, tt.err)
143+
votingMock.EXPECT().Update(mock.Anything, votingId, boardId, []votings.Note(nil), votings.Aborted).
144+
Return(&votings.Voting{Status: votings.Aborted}, tt.err)
144145

145146
rr := httptest.NewRecorder()
146147
s.updateVoting(rr, req.Request())
147148
suite.Equal(tt.expectedCode, rr.Result().StatusCode)
148149
votingMock.AssertExpectations(suite.T())
149-
votingMock.AssertNumberOfCalls(suite.T(), "Cancel", 1)
150+
votingMock.AssertNumberOfCalls(suite.T(), "Update", 1)
150151
})
151152
}
152153
}

server/src/votings/api.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ type VotingService interface {
1414
GetVotes(ctx context.Context, board uuid.UUID, f VoteFilter) ([]*Vote, error)
1515
AddVote(ctx context.Context, req VoteRequest) (*Vote, error)
1616
RemoveVote(ctx context.Context, req VoteRequest) error
17-
Close(ctx context.Context, id uuid.UUID, board uuid.UUID, affectedNotes []Note) (*Voting, error)
18-
Cancel(ctx context.Context, id uuid.UUID, board uuid.UUID, affectedNotes []Note) (*Voting, error)
17+
Update(ctx context.Context, id uuid.UUID, board uuid.UUID, affectedNotes []Note, votingStatus VotingStatus) (*Voting, error)
1918
}
2019

2120
type VotingApi struct {

server/src/votings/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (d *DB) Create(ctx context.Context, insert DatabaseVotingInsert) (DatabaseV
2929
return voting, err
3030
}
3131

32-
func (d *DB) Close(ctx context.Context, update DatabaseVotingUpdate) (DatabaseVoting, error) {
32+
func (d *DB) Update(ctx context.Context, update DatabaseVotingUpdate) (DatabaseVoting, error) {
3333
var voting DatabaseVoting
3434

3535
if update.Status == Closed {

server/src/votings/database_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (suite *DatabaseVotingTestSuite) Test_Database_Close() {
7878
votingId := suite.baseData.Votings["Update"].ID
7979
boardId := suite.baseData.Boards["Update"].ID
8080

81-
dbVoting, err := database.Close(context.Background(),
81+
dbVoting, err := database.Update(context.Background(),
8282
DatabaseVotingUpdate{
8383
ID: votingId,
8484
Board: boardId,
@@ -290,18 +290,18 @@ func (suite *DatabaseVotingTestSuite) Test_Database_Cancel() {
290290
votingId := suite.baseData.Votings["Update"].ID
291291
boardId := suite.baseData.Boards["Update"].ID
292292

293-
dbVoting, err := database.Close(context.Background(),
293+
dbVoting, err := database.Update(context.Background(),
294294
DatabaseVotingUpdate{
295295
ID: votingId,
296296
Board: boardId,
297-
Status: Canceled,
297+
Status: Aborted,
298298
},
299299
)
300300

301301
assert.Nil(t, err)
302302
assert.Equal(t, votingId, dbVoting.ID)
303303
assert.Equal(t, boardId, dbVoting.Board)
304-
assert.Equal(t, Canceled, dbVoting.Status)
304+
assert.Equal(t, Aborted, dbVoting.Status)
305305
assert.Equal(t, 7, dbVoting.VoteLimit)
306306
assert.True(t, dbVoting.AllowMultipleVotes)
307307
assert.False(t, dbVoting.ShowVotesOfOthers)

server/src/votings/dto.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ type VotingCreateRequest struct {
4949

5050
// VotingCloseRequest represents the request to update a voting session.
5151
type VotingCloseRequest struct {
52-
ID uuid.UUID `json:"-"`
53-
Board uuid.UUID `json:"-"`
54-
Status VotingStatus `json:"status,omitempty"`
52+
ID uuid.UUID `json:"-"`
53+
Board uuid.UUID `json:"-"`
54+
Status VotingStatus `json:"status"`
5555
}
5656

5757
// Voting is the response for all voting requests.

server/src/votings/mock_VotingDatabase.go

Lines changed: 66 additions & 66 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)