generated from VLADIMIR/template
36 lines
1.2 KiB
Go
36 lines
1.2 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
|
|
"evening_detective_server/internal/modules/processor_jwt"
|
|
"evening_detective_server/internal/modules/roles"
|
|
"evening_detective_server/proto"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
func (s *server) IssueGameTickets(ctx context.Context, req *proto.IssueGameTicketsReq) (*proto.IssueGameTicketsRsp, error) {
|
|
claims, ok := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
|
if !ok || claims == nil {
|
|
return nil, status.Error(codes.Unauthenticated, "необходима авторизация")
|
|
}
|
|
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
|
return nil, status.Error(codes.PermissionDenied, "permission denied")
|
|
}
|
|
if req == nil || req.GameId <= 0 {
|
|
return nil, status.Error(codes.InvalidArgument, "некорректный идентификатор игры")
|
|
}
|
|
|
|
tickets, err := s.ticketService.Issue(ctx, int(req.GameId), int(req.Count))
|
|
if err != nil {
|
|
return &proto.IssueGameTicketsRsp{Error: err.Error()}, nil
|
|
}
|
|
|
|
result := make([]*proto.Ticket, 0, len(tickets))
|
|
for _, ticket := range tickets {
|
|
result = append(result, &proto.Ticket{Id: int32(ticket.ID), Url: ticket.URL})
|
|
}
|
|
return &proto.IssueGameTicketsRsp{Tickets: result}, nil
|
|
}
|