Skip to content

Commit 7e9f4a8

Browse files
Merge pull request #43 from Black-Sirius69/master
gugugtu
2 parents d9e972f + 27cce1c commit 7e9f4a8

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

internal/controllers/team_controller.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"database/sql"
55
"errors"
66
"net/http"
7+
"strings"
78

89
"github.com/google/uuid"
910
"github.com/jackc/pgx/v5/pgconn"
@@ -24,6 +25,8 @@ func CreateTeam(ctx echo.Context) error {
2425
})
2526
}
2627

28+
payload.Name = strings.TrimSpace(payload.Name)
29+
2730
if err := ctx.Validate(&payload); err != nil {
2831
return ctx.JSON(http.StatusBadRequest, map[string]string{
2932
"message": err.Error(),
@@ -74,6 +77,65 @@ func CreateTeam(ctx echo.Context) error {
7477
})
7578
}
7679

80+
func UpdateTeamName(ctx echo.Context) error {
81+
var payload models.CreateTeamRequest
82+
83+
if err := ctx.Bind(&payload); err != nil {
84+
return ctx.JSON(http.StatusBadRequest, map[string]string{
85+
"message": err.Error(),
86+
"status": "fail",
87+
})
88+
}
89+
90+
payload.Name = strings.TrimSpace(payload.Name)
91+
92+
if err := ctx.Validate(&payload); err != nil {
93+
return ctx.JSON(http.StatusBadRequest, map[string]string{
94+
"message": err.Error(),
95+
"status": "fail",
96+
})
97+
}
98+
99+
user := ctx.Get("user").(*models.User)
100+
101+
if !user.IsLeader {
102+
return ctx.JSON(http.StatusForbidden, map[string]string{
103+
"status": "fail",
104+
"message": "user is not leader",
105+
})
106+
}
107+
108+
if !user.IsLeader {
109+
return ctx.JSON(http.StatusForbidden, map[string]string{
110+
"status": "fail",
111+
"message": "user is not leader",
112+
})
113+
}
114+
115+
team, err := services.FindTeamByTeamID(payload.ID)
116+
if err != nil {
117+
if errors.Is(err, sql.ErrNoRows) {
118+
return ctx.JSON(http.StatusNotFound, map[string]string{
119+
"message": "user not found",
120+
"status": "fail",
121+
})
122+
}
123+
}
124+
125+
err = services.UpdateTeamName(payload.Name, team.ID)
126+
if err != nil {
127+
return ctx.JSON(http.StatusInternalServerError, map[string]string{
128+
"message": err.Error(),
129+
"status": "error",
130+
})
131+
}
132+
133+
return ctx.JSON(http.StatusOK, map[string]interface{}{
134+
"message": "team updated successfully",
135+
"status": "success",
136+
})
137+
}
138+
77139
func GetTeamDetails(ctx echo.Context) error {
78140
user := ctx.Get("user").(*models.User)
79141

internal/models/team_model.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ type Team struct {
1313
}
1414

1515
type CreateTeamRequest struct {
16-
Name string `json:"name" validate:"required,min=1,max=50"`
16+
ID uuid.UUID `json:"id" validate:"required,uuid"`
17+
Name string `json:"name" validate:"required,min=1,max=50"`
1718
}
1819

1920
type JoinTeamRequest struct {

internal/routes/team_routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ func TeamRoutes(incomingRoutes *echo.Echo) {
1515
team.POST("/join", controllers.JoinTeam)
1616
team.DELETE("/leave", controllers.LeaveTeam)
1717
team.POST("/kick", controllers.KickMember)
18+
team.PATCH("/update", controllers.UpdateTeamName)
1819
}

internal/services/team/update_team.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ func UpdateUserTeamDetails(teamid uuid.UUID, id uuid.UUID) error {
1919
return err
2020
}
2121
}
22+
23+
func UpdateTeamName(teamName string, id uuid.UUID) error {
24+
_, err := database.DB.Exec("UPDATE teams SET name = $1 where id = $2", teamName, id)
25+
return err
26+
}

0 commit comments

Comments
 (0)