Skip to content

Commit

Permalink
wip: teams controller
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored May 16, 2024
1 parent 61838e4 commit 263a4f1
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 39 deletions.
30 changes: 11 additions & 19 deletions internal/api/adapters/db/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,24 @@ import (
"context"

"github.com/zeiss/typhoon/internal/api/models"

"github.com/google/uuid"
)

// CreateTeam creates a new team.
func (db *DB) CreateTeam(ctx context.Context, team models.Team) (models.Team, error) {
if err := db.conn.WithContext(ctx).Create(&team).Error; err != nil {
return models.Team{}, err
}

return team, nil
func (db *DB) CreateTeam(ctx context.Context, team *models.Team) error {
return db.conn.WithContext(ctx).Create(team).Error
}

// GetTeam retrieves a team by its ID.
func (db *DB) GetTeam(ctx context.Context, id uuid.UUID) (models.Team, error) {
team := models.Team{}

err := db.conn.WithContext(ctx).Where("id = ?", id).First(&team).Error
if err != nil {
return models.Team{}, err
}

return team, nil
func (db *DB) GetTeam(ctx context.Context, team *models.Team) error {
return db.conn.WithContext(ctx).First(team).Error
}

// DeleteTeam deletes a team by its ID.
func (db *DB) DeleteTeam(ctx context.Context, id uuid.UUID) error {
return db.conn.WithContext(ctx).Where("id = ?", id).Delete(&models.Team{}).Error
func (db *DB) DeleteTeam(ctx context.Context, team *models.Team) error {
return db.conn.WithContext(ctx).Delete(team).Error
}

// ListTeams retrieves all teams.
func (db *DB) ListTeams(ctx context.Context, pagination *models.Pagination[models.Team]) error {
return db.conn.WithContext(ctx).Scopes(models.Paginate(&pagination.Rows, pagination, db.conn)).Find(&pagination.Rows).Error
}
42 changes: 35 additions & 7 deletions internal/api/adapters/handlers/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,54 @@ package handlers
import (
"context"

"github.com/zeiss/typhoon/internal/utils"
openapi "github.com/zeiss/typhoon/pkg/apis"
"github.com/zeiss/typhoon/pkg/apis/dto"
)

// CreateTeam ...
func (a *ApiHandlers) CreateTeam(ctx context.Context, req openapi.CreateTeamRequestObject) (openapi.CreateTeamResponseObject, error) {
team, err := a.teams.CreateTeam(ctx, req.Body.Name, req.Body.Description)
cmd := dto.FromCreateTeamRequest(req)

team, err := a.teams.CreateTeam(ctx, cmd)
if err != nil {
return openapi.CreateTeamdefaultJSONResponse{}, nil
return nil, err
}

return openapi.CreateTeam201JSONResponse(openapi.CreateTeam201JSONResponse(openapi.Team{Name: team.Name, Description: team.Description})), nil
return dto.ToCreateTeamResponse(team), nil
}

// GetTeam ...
func (a *ApiHandlers) GetTeam(ctx context.Context, req openapi.GetTeamRequestObject) (openapi.GetTeamResponseObject, error) {
team, err := a.teams.GetTeam(ctx, req.TeamId)
query := dto.FromGetTeamRequest(req)

team, err := a.teams.GetTeam(ctx, query)
if err != nil {
return nil, err
}

return dto.ToGetTeamResponse(team), nil
}

// DeleteTeam ...
func (a *ApiHandlers) DeleteTeam(ctx context.Context, req openapi.DeleteTeamRequestObject) (openapi.DeleteTeamResponseObject, error) {
cmd := dto.FromDeleteTeamRequest(req)

err := a.teams.DeleteTeam(ctx, cmd)
if err != nil {
return nil, err
}

return dto.ToDeleteTeamResponse(), nil
}

// ListTeams ...
func (a *ApiHandlers) ListTeams(ctx context.Context, req openapi.ListTeamsRequestObject) (openapi.ListTeamsResponseObject, error) {
query := dto.FromListTeamsRequest(req)

teams, err := a.teams.ListTeams(ctx, query)
if err != nil {
return openapi.GetTeamdefaultJSONResponse{}, nil
return nil, err
}

return openapi.GetTeam200JSONResponse(openapi.Team{Id: utils.PtrUUID(team.ID), Name: team.Name, Description: team.Description, CreatedAt: utils.PtrTime(team.CreatedAt), UpdatedAt: utils.PtrTime(team.UpdatedAt), DeletedAt: utils.PtrTime(team.DeletedAt.Time)}), nil
return dto.ToListTeamsResponse(teams), nil
}
92 changes: 83 additions & 9 deletions internal/api/controllers/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,45 @@ import (
authz "github.com/zeiss/fiber-authz"
"github.com/zeiss/typhoon/internal/api/models"
"github.com/zeiss/typhoon/internal/api/ports"
"github.com/zeiss/typhoon/internal/utils"
)

// CreateTeamCommand ...
type CreateTeamCommand struct {
Name string `json:"name" validate:"required,min=3,max=255"`
Description string `json:"description" validate:"max=1024"`
}

// DeleteTeamCommand ...
type DeleteTeamCommand struct {
ID uuid.UUID `json:"id" validate:"required"`
}

// GetTeamQuery ...
type GetTeamQuery struct {
ID uuid.UUID `json:"id" validate:"required"`
}

// ListTeamsQuery ...
type ListTeamsQuery struct {
Offset int `json:"offset" validate:"required"`
Limit int `json:"limit" validate:"required"`
Sort string `json:"sort" validate:"required"`
Search string `json:"search" validate:"required"`
}

var _ TeamsController = (*teamsController)(nil)

// TeamsController ...
type TeamsController interface {
// CreateTeam ...
CreateTeam(ctx context.Context, name string, description *string) (models.Team, error)
CreateTeam(ctx context.Context, cmd CreateTeamCommand) (models.Team, error)
// DeleteTeam ...
DeleteTeam(ctx context.Context, id uuid.UUID) error
DeleteTeam(ctx context.Context, cmd DeleteTeamCommand) error
// GetTeam ...
GetTeam(ctx context.Context, id uuid.UUID) (models.Team, error)
GetTeam(ctx context.Context, query GetTeamQuery) (models.Team, error)
// ListTeams ...
ListTeams(ctx context.Context, query ListTeamsQuery) (models.Pagination[models.Team], error)
}

type teamsController struct {
Expand All @@ -29,16 +58,61 @@ func NewTeamsController(db ports.Teams) *teamsController {
}

// CreateTeam ...
func (c *teamsController) CreateTeam(ctx context.Context, name string, description *string) (models.Team, error) {
return c.db.CreateTeam(ctx, models.Team{Team: &authz.Team{Name: name, Description: description}})
func (c *teamsController) CreateTeam(ctx context.Context, cmd CreateTeamCommand) (models.Team, error) {
team := models.Team{
Team: &authz.Team{
Name: cmd.Name,
Description: utils.StrPtr(cmd.Description),
},
}

err := c.db.CreateTeam(ctx, &team)
if err != nil {
return team, err
}

return team, err
}

// DeleteTeam ...
func (c *teamsController) DeleteTeam(ctx context.Context, id uuid.UUID) error {
return c.db.DeleteTeam(ctx, id)
func (c *teamsController) DeleteTeam(ctx context.Context, cmd DeleteTeamCommand) error {
team := models.Team{
Team: &authz.Team{
ID: cmd.ID,
},
}

return c.db.DeleteTeam(ctx, &team)
}

// GetTeam ...
func (c *teamsController) GetTeam(ctx context.Context, id uuid.UUID) (models.Team, error) {
return c.db.GetTeam(ctx, id)
func (c *teamsController) GetTeam(ctx context.Context, query GetTeamQuery) (models.Team, error) {
team := models.Team{
Team: &authz.Team{
ID: query.ID,
},
}

err := c.db.GetTeam(ctx, &team)
if err != nil {
return team, err
}

return team, nil
}

// ListTeams ...
func (c *teamsController) ListTeams(ctx context.Context, query ListTeamsQuery) (models.Pagination[models.Team], error) {
pagination := models.Pagination[models.Team]{
Offset: query.Offset,
Limit: query.Limit,
Search: query.Search,
}

err := c.db.ListTeams(ctx, &pagination)
if err != nil {
return pagination, err
}

return pagination, nil
}
19 changes: 19 additions & 0 deletions internal/api/controllers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ type CreateUserCommand struct {
Description string `json:"description"`
}

// DeleteUserCommand ...
type DeleteUserCommand struct {
UserID uuid.UUID `json:"user_id"`
}

// GetUserCredentialsQuery ...
type GetUserCredentialsQuery struct {
UserID uuid.UUID `json:"user_id"`
Expand Down Expand Up @@ -45,6 +50,8 @@ type UsersController interface {
GetUser(ctx context.Context, query GetUserQuery) (models.User, error)
// ListUsers retrieves a list of users.
ListUsers(ctx context.Context, query ListUsersQuery) (models.Pagination[models.User], error)
// DeleteUser deletes a user by its ID.
DeleteUser(ctx context.Context, cmd DeleteUserCommand) error
}

type UsersControllerImpl struct {
Expand Down Expand Up @@ -152,3 +159,15 @@ func (c *UsersControllerImpl) ListUsers(ctx context.Context, query ListUsersQuer

return results, nil
}

// DeleteUser ...
func (c *UsersControllerImpl) DeleteUser(ctx context.Context, cmd DeleteUserCommand) error {
user := models.User{ID: cmd.UserID}

err := c.db.DeleteUser(ctx, &user)
if err != nil {
return err
}

return nil
}
9 changes: 5 additions & 4 deletions internal/api/ports/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package ports
import (
"context"

"github.com/google/uuid"
"github.com/zeiss/typhoon/internal/api/models"
)

// Teams is the interface that wraps the methods to access data.
type Teams interface {
// CreateTeam creates a new team.
CreateTeam(ctx context.Context, team models.Team) (models.Team, error)
CreateTeam(ctx context.Context, team *models.Team) error
// GetTeam returns the team with the given id.
GetTeam(ctx context.Context, id uuid.UUID) (models.Team, error)
GetTeam(ctx context.Context, team *models.Team) error
// DeleteTeam deletes the team with the given id.
DeleteTeam(ctx context.Context, id uuid.UUID) error
DeleteTeam(ctx context.Context, team *models.Team) error
// ListTeams returns all teams.
ListTeams(ctx context.Context, pagination *models.Pagination[models.Team]) error
}
86 changes: 86 additions & 0 deletions pkg/apis/dto/teams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package dto

import (
"github.com/zeiss/typhoon/internal/api/controllers"
"github.com/zeiss/typhoon/internal/api/models"
"github.com/zeiss/typhoon/internal/utils"
openapi "github.com/zeiss/typhoon/pkg/apis"
)

// FromCreateTeamRequest ...
func FromCreateTeamRequest(req openapi.CreateTeamRequestObject) controllers.CreateTeamCommand {
return controllers.CreateTeamCommand{
Name: req.Body.Name,
Description: utils.PtrStr(req.Body.Description),
}
}

// ToCreateTeamResponse ...
func ToCreateTeamResponse(team models.Team) openapi.CreateTeam201JSONResponse {
res := openapi.CreateTeam201JSONResponse{}
res.Id = utils.PtrUUID(team.ID)
res.Name = team.Name
res.CreatedAt = utils.PtrTime(team.CreatedAt)
res.UpdatedAt = utils.PtrTime(team.UpdatedAt)
res.DeletedAt = utils.PtrTime(team.DeletedAt.Time)

return res
}

// FromGetTeamRequest ...
func FromGetTeamRequest(req openapi.GetTeamRequestObject) controllers.GetTeamQuery {
return controllers.GetTeamQuery{
ID: req.TeamId,
}
}

// ToGetTeamResponse ...
func ToGetTeamResponse(team models.Team) openapi.GetTeam200JSONResponse {
res := openapi.GetTeam200JSONResponse{}
res.Id = utils.PtrUUID(team.ID)
res.Name = team.Name
res.Description = team.Description
res.CreatedAt = utils.PtrTime(team.CreatedAt)
res.UpdatedAt = utils.PtrTime(team.UpdatedAt)
res.DeletedAt = utils.PtrTime(team.DeletedAt.Time)

return res
}

// FromDeleteTeamRequest ...
func FromDeleteTeamRequest(req openapi.DeleteTeamRequestObject) controllers.DeleteTeamCommand {
return controllers.DeleteTeamCommand{
ID: req.TeamId,
}
}

// ToDeleteTeamResponse ...
func ToDeleteTeamResponse() openapi.DeleteTeam204Response {
return openapi.DeleteTeam204Response{}
}

// FromListTeamsRequest ...
func FromListTeamsRequest(req openapi.ListTeamsRequestObject) controllers.ListTeamsQuery {
return controllers.ListTeamsQuery{}
}

// ToListTeamsResponse ...
func ToListTeamsResponse(pagination models.Pagination[models.Team]) openapi.ListTeams200JSONResponse {
res := openapi.ListTeams200JSONResponse{}
res.Limit = utils.PtrInt(pagination.Limit)
res.Offset = utils.PtrInt(pagination.Offset)

teams := make([]openapi.Team, 0, len(pagination.Rows))
for _, team := range pagination.Rows {
teams = append(teams, openapi.Team{
Id: utils.PtrUUID(team.ID),
Name: team.Name,
CreatedAt: utils.PtrTime(team.CreatedAt),
UpdatedAt: utils.PtrTime(team.UpdatedAt),
DeletedAt: utils.PtrTime(team.DeletedAt.Time),
})
}
res.Results = &teams

return res
}

0 comments on commit 263a4f1

Please sign in to comment.