100 lines
2.5 KiB
Go
100 lines
2.5 KiB
Go
|
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
|
||
|
}
|
||
|
resp := make([]api.TeamListItem, 0, len(teams))
|
||
|
for _, t := range teams {
|
||
|
resp = append(resp, api.TeamListItem{
|
||
|
Id: int(t.ID),
|
||
|
Members: len(t.Members),
|
||
|
Name: t.Name,
|
||
|
CurrentTeam: currentTeamID == t.ID,
|
||
|
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(),
|
||
|
})
|
||
|
}
|
||
|
|
||
|
return t.getTeamResponse(ctx, teamID)
|
||
|
}
|
||
|
|
||
|
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"),
|
||
|
}))
|
||
|
}
|