2023-11-01 23:21:12 +03:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"gitrepo.ru/neonxp/nquest/api"
|
|
|
|
"gitrepo.ru/neonxp/nquest/pkg/contextlib"
|
|
|
|
"gitrepo.ru/neonxp/nquest/pkg/service"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Team struct {
|
|
|
|
UserService *service.User
|
|
|
|
TeamService *service.Team
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Team) GetTeams(ctx echo.Context) error {
|
|
|
|
currentTeamID := uint(0)
|
|
|
|
user := contextlib.GetUser(ctx)
|
|
|
|
if user.Team != nil {
|
|
|
|
currentTeamID = user.Team.TeamID
|
|
|
|
}
|
|
|
|
teams, err := t.TeamService.List(ctx.Request().Context())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-02 01:21:13 +03:00
|
|
|
resp := make([]api.TeamView, 0, len(teams))
|
2023-11-01 23:21:12 +03:00
|
|
|
for _, t := range teams {
|
2023-11-02 01:21:13 +03:00
|
|
|
memberCount := len(t.Members)
|
|
|
|
isCurrentTeam := currentTeamID == t.ID
|
|
|
|
resp = append(resp, api.TeamView{
|
2023-11-01 23:21:12 +03:00
|
|
|
Id: int(t.ID),
|
2023-11-02 01:21:13 +03:00
|
|
|
Members: &memberCount,
|
2023-11-01 23:21:12 +03:00
|
|
|
Name: t.Name,
|
2023-11-02 01:21:13 +03:00
|
|
|
CurrentTeam: &isCurrentTeam,
|
2023-11-01 23:21:12 +03:00
|
|
|
CreatedAt: t.CreatedAt.Format("02.01.06"),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.JSON(http.StatusOK, api.TeamsListResponse(resp))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Team) GetTeamsTeamID(ctx echo.Context, teamID int) error {
|
|
|
|
return t.getTeamResponse(ctx, teamID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Team) DeleteTeamsTeamIDMembers(ctx echo.Context, teamID int) error {
|
|
|
|
return t.getTeamResponse(ctx, teamID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Team) PostTeamsTeamIDMembers(ctx echo.Context, teamID int) error {
|
|
|
|
team, err := t.TeamService.GetByID(ctx.Request().Context(), uint(teamID))
|
|
|
|
if err != nil {
|
|
|
|
return ctx.JSON(http.StatusNotFound, api.ErrorResponse{
|
|
|
|
Code: http.StatusNotFound,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-02 00:17:15 +03:00
|
|
|
user := contextlib.GetUser(ctx)
|
|
|
|
if user.Team != nil {
|
|
|
|
if err := t.TeamService.RemoveMember(ctx.Request().Context(), user.Team.Team, user); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user.Team = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := t.TeamService.Request(ctx.Request().Context(), team, user); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-01 23:21:12 +03:00
|
|
|
return t.getTeamResponse(ctx, teamID)
|
|
|
|
}
|
|
|
|
|
2023-11-02 23:38:50 +03:00
|
|
|
func (t *Team) PostTeams(ctx echo.Context) error {
|
|
|
|
req := &api.PostTeamsJSONRequestBody{}
|
|
|
|
if err := ctx.Bind(req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
user := contextlib.GetUser(ctx)
|
|
|
|
|
|
|
|
team, err := t.TeamService.Create(ctx.Request().Context(), req.Name, user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.JSON(http.StatusCreated, api.TeamView{
|
|
|
|
Id: int(team.ID),
|
|
|
|
Name: team.Name,
|
|
|
|
CreatedAt: team.CreatedAt.Format("02.01.06"),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-01 23:21:12 +03:00
|
|
|
func (t *Team) getTeamResponse(ctx echo.Context, teamID int) error {
|
|
|
|
team, err := t.TeamService.GetByID(ctx.Request().Context(), uint(teamID))
|
|
|
|
if err != nil {
|
|
|
|
return ctx.JSON(http.StatusNotFound, api.ErrorResponse{
|
|
|
|
Code: http.StatusNotFound,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
members := make([]api.TeamMember, 0, len(team.Members))
|
|
|
|
for _, tm := range team.Members {
|
|
|
|
members = append(members, api.TeamMember{
|
|
|
|
Role: api.MapRole[tm.Role],
|
|
|
|
User: api.UserView{
|
|
|
|
Id: int(tm.User.ID),
|
|
|
|
Username: tm.User.Username,
|
|
|
|
},
|
|
|
|
CreatedAt: tm.CreatedAt.Format("02.01.06"),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
requests := make([]api.TeamRequest, 0, len(team.Requests))
|
|
|
|
for _, tm := range team.Requests {
|
|
|
|
requests = append(requests, api.TeamRequest{
|
|
|
|
User: api.UserView{
|
|
|
|
Id: int(tm.User.ID),
|
|
|
|
Username: tm.User.Username,
|
|
|
|
},
|
|
|
|
CreatedAt: tm.CreatedAt.Format("02.01.06"),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.JSON(http.StatusOK, api.TeamResponse(api.TeamResponse{
|
|
|
|
Id: teamID,
|
|
|
|
Name: team.Name,
|
|
|
|
Members: members,
|
|
|
|
Requests: requests,
|
|
|
|
CreatedAt: team.CreatedAt.Format("02.01.06"),
|
|
|
|
}))
|
|
|
|
}
|