Skip to content

Commit

Permalink
fix: get one
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexeyzem committed Dec 20, 2024
1 parent f890abd commit 0d24013
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 4 additions & 0 deletions internal/community/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func (c *Controller) GetOne(w http.ResponseWriter, r *http.Request) {

community, err := c.service.GetOne(r.Context(), id, sess.UserID)
if err != nil {
if errors.Is(err, my_err.ErrWrongCommunity) {
c.responder.ErrorBadRequest(w, err, reqID)
return
}
c.responder.ErrorInternal(w, err, reqID)
return
}
Expand Down
15 changes: 12 additions & 3 deletions internal/community/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ func (c CommunityRepository) GetBatch(ctx context.Context, lastID uint32) ([]*mo

func (c CommunityRepository) GetOne(ctx context.Context, id uint32) (*models.Community, error) {
res := &models.Community{}
err := c.db.QueryRowContext(ctx, GetOne, id).Scan(&res.ID, &res.Name, &res.Avatar, &res.About, &res.CountSubscribers)
err := c.db.QueryRowContext(ctx, GetOne, id).Scan(
&res.ID, &res.Name, &res.Avatar, &res.About, &res.CountSubscribers,
)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, my_err.ErrWrongCommunity
}
return nil, fmt.Errorf("get community db: %w", err)
}

Expand All @@ -59,7 +64,9 @@ func (c CommunityRepository) Create(ctx context.Context, community *models.Commu
if community.Avatar == "" {
res = c.db.QueryRowContext(ctx, CreateNewCommunity, community.Name, community.About, author)
} else {
res = c.db.QueryRowContext(ctx, CreateNewCommunityWithAvatar, community.Name, community.About, community.Avatar, author)
res = c.db.QueryRowContext(
ctx, CreateNewCommunityWithAvatar, community.Name, community.About, community.Avatar, author,
)
}

err := res.Err()
Expand All @@ -80,7 +87,9 @@ func (c CommunityRepository) Update(ctx context.Context, community *models.Commu
if community.Avatar == "" {
_, err = c.db.ExecContext(ctx, UpdateWithoutAvatar, community.Name, community.About, community.ID)
} else {
_, err = c.db.ExecContext(ctx, UpdateWithAvatar, community.Name, community.Avatar, community.About, community.ID)
_, err = c.db.ExecContext(
ctx, UpdateWithAvatar, community.Name, community.Avatar, community.About, community.ID,
)
}
if err != nil {
return fmt.Errorf("update community: %w", err)
Expand Down

0 comments on commit 0d24013

Please sign in to comment.