Skip to content

Commit

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

err = c.service.Update(r.Context(), id, &newCommunity)
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 Expand Up @@ -209,6 +213,10 @@ func (c *Controller) Delete(w http.ResponseWriter, r *http.Request) {

err = c.service.Delete(r.Context(), id)
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 Expand Up @@ -263,6 +271,10 @@ func (c *Controller) JoinToCommunity(w http.ResponseWriter, r *http.Request) {

err = c.service.JoinCommunity(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 Expand Up @@ -290,6 +302,10 @@ func (c *Controller) LeaveFromCommunity(w http.ResponseWriter, r *http.Request)

err = c.service.LeaveCommunity(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
18 changes: 18 additions & 0 deletions internal/community/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ func (c CommunityRepository) Update(ctx context.Context, community *models.Commu
)
}
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return my_err.ErrWrongCommunity
}
return fmt.Errorf("update community: %w", err)
}

Expand All @@ -101,6 +104,9 @@ func (c CommunityRepository) Update(ctx context.Context, community *models.Commu
func (c CommunityRepository) Delete(ctx context.Context, id uint32) error {
_, err := c.db.ExecContext(ctx, Delete, id)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return my_err.ErrWrongCommunity
}
return fmt.Errorf("delete community: %w", err)
}

Expand All @@ -110,6 +116,9 @@ func (c CommunityRepository) Delete(ctx context.Context, id uint32) error {
func (c CommunityRepository) JoinCommunity(ctx context.Context, communityId, author uint32) error {
_, err := c.db.ExecContext(ctx, JoinCommunity, communityId, author)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return my_err.ErrWrongCommunity
}
return fmt.Errorf("join community: %w", err)
}

Expand All @@ -119,13 +128,19 @@ func (c CommunityRepository) JoinCommunity(ctx context.Context, communityId, aut
func (c CommunityRepository) LeaveCommunity(ctx context.Context, communityId, author uint32) error {
_, err := c.db.ExecContext(ctx, LeaveCommunity, communityId, author)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return my_err.ErrWrongCommunity
}
return fmt.Errorf("leave community: %w", err)
}
access := c.CheckAccess(ctx, communityId, author)

if access {
_, err := c.db.ExecContext(ctx, DeleteAdmin, communityId, author)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return my_err.ErrWrongCommunity
}
return fmt.Errorf("delete admin: %w", err)
}
}
Expand All @@ -136,6 +151,9 @@ func (c CommunityRepository) LeaveCommunity(ctx context.Context, communityId, au
func (c CommunityRepository) NewAdmin(ctx context.Context, communityId uint32, author uint32) error {
_, err := c.db.ExecContext(ctx, InsertNewAdmin, communityId, author)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return my_err.ErrWrongCommunity
}
return fmt.Errorf("insert new admin: %w", err)
}
return nil
Expand Down

0 comments on commit 7849c56

Please sign in to comment.