72 lines
1.7 KiB
C#
72 lines
1.7 KiB
C#
using api.Interfaces;
|
|
using models.Core;
|
|
using models.Enumerations;
|
|
using models.Response;
|
|
|
|
namespace api.Services;
|
|
|
|
public class TicketManager : ITicketManager
|
|
{
|
|
public void SaveMintedTicket(Ticket ticket)
|
|
{
|
|
new data.Tickets.Save().Execute(ticket);
|
|
}
|
|
|
|
public TicketSearch SearchTicket(Guid ticketId)
|
|
{
|
|
var ticket = new data.Tickets.Find().Execute(ticketId);
|
|
|
|
if (ticket == null)
|
|
{
|
|
throw new Exception("Ticket not found");
|
|
}
|
|
|
|
var @event = new data.Events.Find().Execute(ticket.EventId);
|
|
|
|
if (@event == null)
|
|
{
|
|
throw new Exception("Event not found");
|
|
}
|
|
|
|
var result = new TicketSearch
|
|
{
|
|
Type = ticket.Type,
|
|
Validity = DetermineValidity(@event)
|
|
};
|
|
|
|
return result;
|
|
}
|
|
|
|
public MintResponse GetMintResponse(Guid ticketId)
|
|
{
|
|
var ticket = new data.Tickets.Find().Execute(ticketId);
|
|
|
|
if (ticket == null)
|
|
{
|
|
throw new Exception("Ticket not found");
|
|
}
|
|
|
|
return new MintResponse()
|
|
{
|
|
Ticket = ticket,
|
|
Details = new data.Events.GetDetails().Execute(ticket.EventId),
|
|
};
|
|
}
|
|
|
|
private static TicketValidity DetermineValidity(Event @event)
|
|
{
|
|
if (@event.Date.Day != DateTime.Today.Day) return TicketValidity.Invalid;
|
|
|
|
if (@event.Date.Day < DateTime.Now.Day)
|
|
{
|
|
return TicketValidity.Expired;
|
|
}
|
|
|
|
if (DateTime.Now.Hour < @event.Date.Hour - 2)
|
|
{
|
|
return TicketValidity.Early;
|
|
}
|
|
|
|
return DateTime.Now.Hour > @event.Date.Hour + 5 ? TicketValidity.Expired : TicketValidity.Valid;
|
|
}
|
|
} |