Skip to content

Commit

Permalink
Merge pull request #43 from Black-Sirius69/master
Browse files Browse the repository at this point in the history
gugugtu
  • Loading branch information
Mr-Emerald-Wolf authored Mar 15, 2024
2 parents d9e972f + 27cce1c commit 7e9f4a8
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
62 changes: 62 additions & 0 deletions internal/controllers/team_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"errors"
"net/http"
"strings"

"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgconn"
Expand All @@ -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(),
Expand Down Expand Up @@ -74,6 +77,65 @@ 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.IsLeader {
return ctx.JSON(http.StatusForbidden, map[string]string{
"status": "fail",
"message": "user is not leader",
})
}

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) {
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.StatusOK, map[string]interface{}{
"message": "team updated successfully",
"status": "success",
})
}

func GetTeamDetails(ctx echo.Context) error {
user := ctx.Get("user").(*models.User)

Expand Down
3 changes: 2 additions & 1 deletion internal/models/team_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions internal/routes/team_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
5 changes: 5 additions & 0 deletions internal/services/team/update_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 7e9f4a8

Please sign in to comment.