Files
evening_detective_server/internal/app/tickets.go
T
2026-09-18 16:14:56 +07:00

66 lines
2.2 KiB
Go

package app
import (
"context"
"evening_detective_server/internal/modules/processor_jwt"
"evening_detective_server/internal/modules/roles"
"evening_detective_server/internal/services/ticket_service"
"evening_detective_server/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
)
func (s *server) GetTicket(ctx context.Context, req *proto.GetTicketReq) (*proto.GetTicketRsp, error) {
_ = grpc.SetHeader(ctx, metadata.Pairs("Cache-Control", "no-store"))
if req == nil {
return &proto.GetTicketRsp{Error: ticket_service.ErrTicketNotFound.Error()}, nil
}
page, err := s.ticketService.Get(ctx, req.Token)
if err != nil {
return &proto.GetTicketRsp{Error: ticket_service.ErrTicketNotFound.Error()}, nil
}
result := &proto.TicketPage{
Id: int32(page.ID),
GameName: page.GameName,
StartAt: timestamppb.New(page.StartAt),
Status: page.Status,
GameStatus: page.GameStatus,
}
if page.Location != nil {
result.Location = *page.Location
}
if page.UsedAt != nil {
result.UsedAt = timestamppb.New(*page.UsedAt)
}
return &proto.GetTicketRsp{Ticket: result}, nil
}
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
}