summaryrefslogtreecommitdiff
path: root/Logics
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2022-11-23 17:56:41 -0600
committerHombreLaser <sebastian-440@live.com>2022-11-23 17:56:41 -0600
commitccfe519bc75a95bae4f8f99b0b73a90c367f6272 (patch)
treec5ffaeed2fb03a5fbe92781e7e4f8823d7d233e0 /Logics
parent8ea8f4f9a9bbb6db9abe1294163d2444d1e5da7b (diff)
Añadido controlador de tickets
Diffstat (limited to 'Logics')
-rw-r--r--Logics/CreateTicketLogic.cs69
1 files changed, 69 insertions, 0 deletions
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<bool> 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<Ticket>(_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