diff --git a/api/requests.http b/api/requests.http index 057427d..b02fba2 100644 --- a/api/requests.http +++ b/api/requests.http @@ -33,3 +33,15 @@ X-Password: pass { "place": "A-1" } + +### Выдача приложения + +POST http://localhost:8090/teams/1/applications + +{ + "applications": [ + { + "id": 10 + } + ] +} diff --git a/internal/services/mappers.go b/internal/services/mappers.go index d74721c..c4b0c5a 100644 --- a/internal/services/mappers.go +++ b/internal/services/mappers.go @@ -70,3 +70,17 @@ func mapApplicationToProtoApplication(application *models.Application) *proto.Ap State: application.State, } } + +func mapProtoApplicationsToApplications(items []*proto.Application) []*models.Application { + res := make([]*models.Application, 0, len(items)) + for _, item := range items { + res = append(res, mapProtoApplicationToApplication(item)) + } + return res +} + +func mapProtoApplicationToApplication(items *proto.Application) *models.Application { + return &models.Application{ + ID: items.Id, + } +} diff --git a/internal/services/repository.go b/internal/services/repository.go index 5b0469e..6795e4e 100644 --- a/internal/services/repository.go +++ b/internal/services/repository.go @@ -90,9 +90,9 @@ func (r *Repository) GetActions(ctx context.Context, teamId int64) ([]*models.Ac return actions, nil } -func (r *Repository) AddActions(ctx context.Context, actions []*models.Action) error { +func (r *Repository) AddActions(ctx context.Context, teamId int64, actions []*models.Action) error { for _, action := range actions { - _, err := r.db.Exec("insert into actions (place, teamId) values ($1, $2)", action.Place, action.TeamID) + _, err := r.db.Exec("insert into actions (place, teamId) values ($1, $2)", action.Place, teamId) if err != nil { return err } @@ -152,3 +152,13 @@ func (r *Repository) GetApplications(ctx context.Context, teamId int64, state st } return applications, nil } + +func (r *Repository) GiveApplications(ctx context.Context, teamId int64, applications []*models.Application) error { + for _, application := range applications { + _, err := r.db.Exec("update applications set state = \"gave\" where teamId = $1 and id = $2", teamId, application.ID) + if err != nil { + return err + } + } + return nil +} diff --git a/internal/services/services.go b/internal/services/services.go index a0a6e3b..5ab7d8b 100644 --- a/internal/services/services.go +++ b/internal/services/services.go @@ -30,7 +30,11 @@ func NewServices( } func (s *Services) GiveApplications(ctx context.Context, req *proto.GiveApplicationsReq) (*proto.GiveApplicationsRsp, error) { - panic("unimplemented") + applications := mapProtoApplicationsToApplications(req.Applications) + if err := s.repository.GiveApplications(ctx, req.TeamId, applications); err != nil { + return nil, status.Errorf(codes.Internal, err.Error()) + } + return &proto.GiveApplicationsRsp{}, nil } func (s *Services) GameStop(ctx context.Context, req *proto.GameStopReq) (*proto.GameStopRsp, error) { @@ -57,7 +61,7 @@ func (s *Services) AddAction(ctx context.Context, req *proto.AddActionReq) (*pro Applications: mapStoryApplicationsToApplications(place.Applications), }, } - if err := s.repository.AddActions(ctx, actions); err != nil { + if err := s.repository.AddActions(ctx, team.ID, actions); err != nil { return nil, status.Errorf(codes.Internal, err.Error()) } if err := s.repository.AddApplications(ctx, actions); err != nil {