From ccfe519bc75a95bae4f8f99b0b73a90c367f6272 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Wed, 23 Nov 2022 17:56:41 -0600 Subject: AƱadido controlador de tickets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Logics/CreateTicketLogic.cs | 69 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Logics/CreateTicketLogic.cs (limited to 'Logics') diff --git a/Logics/CreateTicketLogic.cs b/Logics/CreateTicketLogic.cs new file mode 100644 index 0000000..df64a1a --- /dev/null +++ b/Logics/CreateTicketLogic.cs @@ -0,0 +1,69 @@ +using AutoMapper; +using BackendPIA.Services; +using BackendPIA.Models; +using BackendPIA.Forms; + +namespace BackendPIA.Logics { + public class CreateTicketLogic { + private readonly ITicketService _ticket_service; + private readonly ApplicationDbContext _context; + private readonly UserAccount _user; + private readonly long _raffle_id; + private readonly IMapper _mapper; + private readonly TicketForm _form; + public string? ErrorMessage { get; set; } + public Ticket? Created { get; set; } + + public CreateTicketLogic(ITicketService service, ApplicationDbContext context, IMapper mapper, TicketForm form, UserAccount user, long raffle_id) { + _ticket_service = service; + _mapper = mapper; + _form = form; + _user = user; + _raffle_id = raffle_id; + _context = context; + } + + public async Task Call() { + // Check if the user exists. + if(_user == null) + return false; + + // Check if the given raffle exists- + if(!_context.Raffles.Any(r => r.Id == _raffle_id)) { + ErrorMessage = "The raffle doesn't exist."; + + return false; + } + + // Check if the user already has a ticket for the given raffle. + if(_context.Tickets.Where(t => t.RaffleId == _raffle_id).Where(t => t.UserAccountId == _user.Id).Count() > 0) { + ErrorMessage = $"There's already a ticket for {_user.UserName}."; + + return false; + } + + // Check if the number has been taken. + if(_context.Tickets.Where(t => t.RaffleId == _raffle_id).Where(t => t.Number == _form.Number).Count() > 0) { + ErrorMessage = $"There's already a registered ticket with the number {_form.Number}."; + + return false; + } + + var ticket = _mapper.Map(_form); + ticket.RaffleId = _raffle_id; + + // Check if the given raffle has reached the ticket limit. + if(_context.Tickets.Where(t => t.RaffleId == _raffle_id).Count() >= 54) { + ErrorMessage = $"The raffle with id {_raffle_id} has reached the ticket limit"; + + return false; + } + + ticket.UserAccountId = _user.Id; + ticket.IsWinner = false; + Created = await _ticket_service.CreateTicket(ticket); + + return true; + } + } +} \ No newline at end of file -- cgit v1.2.3