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

🐛 Fix Ticker Users API #361

Merged
merged 1 commit into from
Feb 2, 2025
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
22 changes: 17 additions & 5 deletions internal/api/tickers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ type TickerLocationParam struct {
Lon float64 `json:"lon"`
}

type TickerUsersParam struct {
Users []struct {
ID int `json:"id"`
} `json:"users" binding:"required"`
}

type TickerWebsitesParam struct {
Websites []TickerWebsiteParam `json:"websites" binding:"required"`
}
Expand Down Expand Up @@ -145,18 +151,24 @@ func (h *handler) PutTickerUsers(c *gin.Context) {
return
}

var body struct {
Users []storage.User `json:"users" binding:"required"`
}

var body TickerUsersParam
err = c.Bind(&body)
if err != nil {
c.JSON(http.StatusBadRequest, response.ErrorResponse(response.CodeDefault, response.FormError))
return
}

ticker.Users = body.Users
userIds := make([]int, 0)
for _, user := range body.Users {
userIds = append(userIds, user.ID)
}
users, err := h.storage.FindUsersByIDs(userIds)
if err != nil {
c.JSON(http.StatusInternalServerError, response.ErrorResponse(response.CodeDefault, response.StorageError))
return
}

ticker.Users = users
err = h.storage.SaveTicker(&ticker)
if err != nil {
c.JSON(http.StatusInternalServerError, response.ErrorResponse(response.CodeDefault, response.StorageError))
Expand Down
15 changes: 15 additions & 0 deletions internal/api/tickers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,25 @@ func (s *TickerTestSuite) TestPutTickerUsers() {
s.store.AssertExpectations(s.T())
})

s.Run("when find users not working", func() {
s.ctx.Set("ticker", storage.Ticker{})
body := `{"users":[{"id":1},{"id":2},{"id":3}]}`
s.ctx.Request = httptest.NewRequest(http.MethodPut, "/v1/admin/tickers/1/user", strings.NewReader(body))
s.ctx.Request.Header.Add("Content-Type", "application/json")
s.store.On("FindUsersByIDs", mock.Anything).Return(nil, errors.New("storage error")).Once()
h := s.handler()
h.PutTickerUsers(s.ctx)

s.Equal(http.StatusInternalServerError, s.w.Code)
s.store.AssertExpectations(s.T())
})

s.Run("when storage returns error", func() {
s.ctx.Set("ticker", storage.Ticker{})
body := `{"users":[{"id":1},{"id":2},{"id":3}]}`
s.ctx.Request = httptest.NewRequest(http.MethodPut, "/v1/admin/tickers/1/user", strings.NewReader(body))
s.ctx.Request.Header.Add("Content-Type", "application/json")
s.store.On("FindUsersByIDs", mock.Anything).Return([]storage.User{{ID: 1}, {ID: 2}, {ID: 3}}, nil).Once()
s.store.On("SaveTicker", mock.Anything).Return(errors.New("storage error")).Once()
h := s.handler()
h.PutTickerUsers(s.ctx)
Expand All @@ -288,6 +302,7 @@ func (s *TickerTestSuite) TestPutTickerUsers() {
body := `{"users":[{"id":1},{"id":2},{"id":3}]}`
s.ctx.Request = httptest.NewRequest(http.MethodPut, "/v1/admin/tickers/1/user", strings.NewReader(body))
s.ctx.Request.Header.Add("Content-Type", "application/json")
s.store.On("FindUsersByIDs", mock.Anything).Return([]storage.User{{ID: 1}, {ID: 2}, {ID: 3}}, nil).Once()
s.store.On("SaveTicker", mock.Anything).Return(nil).Once()
h := s.handler()
h.PutTickerUsers(s.ctx)
Expand Down
5 changes: 5 additions & 0 deletions internal/storage/sql_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func (s *SqlStorage) FindUserByID(id int, opts ...func(*gorm.DB) *gorm.DB) (User

func (s *SqlStorage) FindUsersByIDs(ids []int, opts ...func(*gorm.DB) *gorm.DB) ([]User, error) {
users := make([]User, 0)

if len(ids) == 0 {
return users, nil
}

db := s.prepareDb(opts...)
err := db.Find(&users, ids).Error

Expand Down
6 changes: 6 additions & 0 deletions internal/storage/sql_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ func (s *SqlStorageTestSuite) TestFindUsersByIDs() {
s.Empty(users)
})

s.Run("when empty ids", func() {
users, err := s.store.FindUsersByIDs([]int{})
s.NoError(err)
s.Empty(users)
})

s.Run("when users exist", func() {
user.Tickers = []Ticker{{ID: 1}}
err := s.db.Create(&user).Error
Expand Down
Loading