generated from VLADIMIR/template
94 lines
3.2 KiB
Go
94 lines
3.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.UsedBy != nil {
|
|
result.UsedBy = *page.UsedBy
|
|
}
|
|
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) AcceptTicket(ctx context.Context, req *proto.AcceptTicketReq) (*proto.AcceptTicketRsp, error) {
|
|
claims, ok := processor_jwt.ClaimsFromContext(ctx)
|
|
if !ok || claims == nil || (!roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author)) {
|
|
return nil, status.Error(codes.PermissionDenied, "permission denied")
|
|
}
|
|
if req == nil {
|
|
return &proto.AcceptTicketRsp{Error: ticket_service.ErrTicketNotFound.Error()}, nil
|
|
}
|
|
page, err := s.ticketService.Accept(ctx, req.Token, claims.UserID)
|
|
if err != nil {
|
|
return &proto.AcceptTicketRsp{Error: err.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)
|
|
}
|
|
if page.UsedBy != nil {
|
|
result.UsedBy = *page.UsedBy
|
|
}
|
|
return &proto.AcceptTicketRsp{Ticket: result}, nil
|
|
}
|
|
|
|
func (s *server) IssueGameTickets(ctx context.Context, req *proto.IssueGameTicketsReq) (*proto.IssueGameTicketsRsp, error) {
|
|
claims, ok := processor_jwt.ClaimsFromContext(ctx)
|
|
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
|
|
}
|