From 7849c561bdedada39f89f204815f813aac87bb6c Mon Sep 17 00:00:00 2001 From: Alexeyzem Date: Fri, 20 Dec 2024 21:30:39 +0300 Subject: [PATCH] fix: wrong id community check now --- internal/community/controller/controller.go | 16 ++++++++++++++++ internal/community/repository/repository.go | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/internal/community/controller/controller.go b/internal/community/controller/controller.go index cd9e9a6..74cd71b 100644 --- a/internal/community/controller/controller.go +++ b/internal/community/controller/controller.go @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/internal/community/repository/repository.go b/internal/community/repository/repository.go index 8cdd15f..397470b 100644 --- a/internal/community/repository/repository.go +++ b/internal/community/repository/repository.go @@ -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) } @@ -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) } @@ -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) } @@ -119,6 +128,9 @@ 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) @@ -126,6 +138,9 @@ func (c CommunityRepository) LeaveCommunity(ctx context.Context, communityId, au 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) } } @@ -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