From 238c462c1809a8a6630e38f6039655520e9e0d99 Mon Sep 17 00:00:00 2001 From: Vedant Matanhelia Date: Fri, 15 Mar 2024 23:21:03 +0530 Subject: [PATCH 1/4] git --- internal/controllers/team_controller.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/controllers/team_controller.go b/internal/controllers/team_controller.go index 8a66d1f..a13791e 100644 --- a/internal/controllers/team_controller.go +++ b/internal/controllers/team_controller.go @@ -4,6 +4,7 @@ import ( "database/sql" "errors" "net/http" + "strings" "github.com/google/uuid" "github.com/jackc/pgx/v5/pgconn" @@ -24,6 +25,8 @@ func CreateTeam(ctx echo.Context) error { }) } + payload.Name = strings.TrimSpace(payload.Name) + if err := ctx.Validate(&payload); err != nil { return ctx.JSON(http.StatusBadRequest, map[string]string{ "message": err.Error(), From 7081d60eece5a6b4b237b980a1bc6603f42859d0 Mon Sep 17 00:00:00 2001 From: Vedant Matanhelia Date: Fri, 15 Mar 2024 23:38:34 +0530 Subject: [PATCH 2/4] feat gutgutu --- internal/controllers/team_controller.go | 51 +++++++++++++++++++++++++ internal/models/team_model.go | 3 +- internal/routes/team_routes.go | 1 + internal/services/team/update_team.go | 5 +++ 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/internal/controllers/team_controller.go b/internal/controllers/team_controller.go index a13791e..1e64a9e 100644 --- a/internal/controllers/team_controller.go +++ b/internal/controllers/team_controller.go @@ -77,6 +77,57 @@ func CreateTeam(ctx echo.Context) error { }) } +func UpdateTeamName(ctx echo.Context) error { + var payload models.CreateTeamRequest + + if err := ctx.Bind(&payload); err != nil { + return ctx.JSON(http.StatusBadRequest, map[string]string{ + "message": err.Error(), + "status": "fail", + }) + } + + payload.Name = strings.TrimSpace(payload.Name) + + if err := ctx.Validate(&payload); err != nil { + return ctx.JSON(http.StatusBadRequest, map[string]string{ + "message": err.Error(), + "status": "fail", + }) + } + + user := ctx.Get("user").(*models.User) + if user.TeamID != uuid.Nil { + return ctx.JSON(http.StatusExpectationFailed, map[string]string{ + "message": "user is already in a team", + "status": "fail", + }) + } + + team, err := services.FindTeamByTeamID(payload.ID) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return ctx.JSON(http.StatusNotFound, map[string]string{ + "message": "user not found", + "status": "fail", + }) + } + } + + err = services.UpdateTeamName(payload.Name, team.ID) + if err != nil { + return ctx.JSON(http.StatusInternalServerError, map[string]string{ + "message": err.Error(), + "status": "error", + }) + } + + return ctx.JSON(http.StatusCreated, map[string]interface{}{ + "message": "team updated successfully", + "status": "success", + }) +} + func GetTeamDetails(ctx echo.Context) error { user := ctx.Get("user").(*models.User) diff --git a/internal/models/team_model.go b/internal/models/team_model.go index 00c537e..3d91d40 100644 --- a/internal/models/team_model.go +++ b/internal/models/team_model.go @@ -13,7 +13,8 @@ type Team struct { } type CreateTeamRequest struct { - Name string `json:"name" validate:"required,min=1,max=50"` + ID uuid.UUID `json:"id" validate:"required,uuid"` + Name string `json:"name" validate:"required,min=1,max=50"` } type JoinTeamRequest struct { diff --git a/internal/routes/team_routes.go b/internal/routes/team_routes.go index 9828f7d..4bdf1e5 100644 --- a/internal/routes/team_routes.go +++ b/internal/routes/team_routes.go @@ -15,4 +15,5 @@ func TeamRoutes(incomingRoutes *echo.Echo) { team.POST("/join", controllers.JoinTeam) team.DELETE("/leave", controllers.LeaveTeam) team.POST("/kick", controllers.KickMember) + team.PATCH("/update", controllers.UpdateTeamName) } diff --git a/internal/services/team/update_team.go b/internal/services/team/update_team.go index 95e555b..a890e28 100644 --- a/internal/services/team/update_team.go +++ b/internal/services/team/update_team.go @@ -19,3 +19,8 @@ func UpdateUserTeamDetails(teamid uuid.UUID, id uuid.UUID) error { return err } } + +func UpdateTeamName(teamName string, id uuid.UUID) error { + _, err := database.DB.Exec("UPDATE teams SET name = $1 where id = $2", teamName, id) + return err +} From 3eeabc6dc07c1425d05746cab9c4df7d5640a65f Mon Sep 17 00:00:00 2001 From: Vedant Matanhelia Date: Fri, 15 Mar 2024 23:40:14 +0530 Subject: [PATCH 3/4] feat: feat --- internal/controllers/team_controller.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/internal/controllers/team_controller.go b/internal/controllers/team_controller.go index 1e64a9e..10010d5 100644 --- a/internal/controllers/team_controller.go +++ b/internal/controllers/team_controller.go @@ -104,6 +104,13 @@ func UpdateTeamName(ctx echo.Context) error { }) } + if !user.IsLeader { + return ctx.JSON(http.StatusForbidden, map[string]string{ + "status": "fail", + "message": "user is not leader", + }) + } + team, err := services.FindTeamByTeamID(payload.ID) if err != nil { if errors.Is(err, sql.ErrNoRows) { From 41ff9c34fed530e2d79c08fb1aa975a5244c289a Mon Sep 17 00:00:00 2001 From: Vedant Matanhelia Date: Fri, 15 Mar 2024 23:40:14 +0530 Subject: [PATCH 4/4] feat: feat --- internal/controllers/team_controller.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/internal/controllers/team_controller.go b/internal/controllers/team_controller.go index 1e64a9e..bb49cec 100644 --- a/internal/controllers/team_controller.go +++ b/internal/controllers/team_controller.go @@ -97,10 +97,11 @@ func UpdateTeamName(ctx echo.Context) error { } user := ctx.Get("user").(*models.User) - if user.TeamID != uuid.Nil { - return ctx.JSON(http.StatusExpectationFailed, map[string]string{ - "message": "user is already in a team", + + if !user.IsLeader { + return ctx.JSON(http.StatusForbidden, map[string]string{ "status": "fail", + "message": "user is not leader", }) } @@ -122,7 +123,7 @@ func UpdateTeamName(ctx echo.Context) error { }) } - return ctx.JSON(http.StatusCreated, map[string]interface{}{ + return ctx.JSON(http.StatusOK, map[string]interface{}{ "message": "team updated successfully", "status": "success", })