From 5fe81d9515d71dad1e37f7ee3262a44c52e2599e Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Mon, 28 Nov 2022 17:49:34 -0600 Subject: Añadido validador de límite para premios MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/RafflesController.cs | 23 +++++++++++++++++++---- Forms/PrizeDTO.cs | 8 ++++++++ Forms/PrizeForm.cs | 1 + Policies/CorrectTokenHandler.cs | 2 +- Profiles/PrizeProfile.cs | 3 ++- Services/IRaffleService.cs | 1 + Services/RaffleService.cs | 9 +++++++++ Services/UserAccountService.cs | 7 ++++++- Validations/MaximumWinners.cs | 27 +++++++++++++++++++++++++++ 9 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 Forms/PrizeDTO.cs create mode 100644 Validations/MaximumWinners.cs diff --git a/Controllers/RafflesController.cs b/Controllers/RafflesController.cs index 4b25519..0da75d5 100644 --- a/Controllers/RafflesController.cs +++ b/Controllers/RafflesController.cs @@ -24,12 +24,12 @@ namespace BackendPIA.Controllers { [HttpGet] public async Task>> Index([FromQuery] string name = "") { var result = await _service.GetRaffles(name); - + return Ok(_mapper.Map>(result)); } [HttpGet("{id:int}")] - public async Task> Show(long id) { + public async Task> Show(long id) { var raffle = await _service.GetRaffle(id); if(raffle == null) @@ -76,8 +76,11 @@ namespace BackendPIA.Controllers { IEnumerable available_tickets = from number in Enumerable.Range(1, 54) select number; IEnumerable taken_tickets = await _service.GetTakenTickets(id); + if(!_service.RaffleExists(id)) + return NotFound(new NotFoundError(404, $"The raffle with id {id} doesn't exist.")); + if(!taken_tickets.Any()) - return NotFound(new NotFoundError(404, $"The raffle with id {id} doesn't exist or doesn't have any tickets.")); + return Ok(new { Numbers = available_tickets }); return Ok(new { Numbers = available_tickets.Except(taken_tickets) }); } @@ -87,7 +90,7 @@ namespace BackendPIA.Controllers { public async Task>> Play(long id) { RafflePlayLogic logic = new RafflePlayLogic(_game_service, _service, id); bool result = await logic.Call(); - + if(!result) return BadRequest(new { ErrorMessage = logic.ErrorMessage }); @@ -109,5 +112,17 @@ namespace BackendPIA.Controllers { return Ok(_mapper.Map>(result)); } + + [HttpGet("{id:int}/prizes")] + public async Task>> GetPrizes(long id) { + var raffle = await _service.GetRaffle(id); + + if(raffle == null) + return NotFound(new NotFoundError(404, $"The raffle with id {id} doesn't exist.")); + + var prizes = _service.GetRaffleTickets(id); + + return Ok(_mapper.Map>(prizes)); + } } } \ No newline at end of file diff --git a/Forms/PrizeDTO.cs b/Forms/PrizeDTO.cs new file mode 100644 index 0000000..6741567 --- /dev/null +++ b/Forms/PrizeDTO.cs @@ -0,0 +1,8 @@ +namespace BackendPIA.Forms { + public class PrizeDTO { + public long Id { get; set; } + public string Name { get; set; } + public int Tier { get; set; } + public string Category { get; set; } + } +} \ No newline at end of file diff --git a/Forms/PrizeForm.cs b/Forms/PrizeForm.cs index bc955f8..42074ad 100644 --- a/Forms/PrizeForm.cs +++ b/Forms/PrizeForm.cs @@ -7,6 +7,7 @@ namespace BackendPIA.Forms { [Required] [ForeignKey("RaffleId")] [IsNotClosed] + [MaximumWinners] public long RaffleId { get; set; } [Required] public string? Name { get; set; } diff --git a/Policies/CorrectTokenHandler.cs b/Policies/CorrectTokenHandler.cs index 2356b56..e372852 100644 --- a/Policies/CorrectTokenHandler.cs +++ b/Policies/CorrectTokenHandler.cs @@ -14,7 +14,7 @@ namespace BackendPIA.Policies { protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, CorrectTokenRequirement requirement) { if(context.Resource is HttpContext httpContext) { var claims = context.User.Claims.Where(c => c.Type.Contains("email")); - if(claims != null) { + if(claims.Any()) { var user = _manager.FindByEmailAsync(claims.First().Value).Result; if(user != null) { diff --git a/Profiles/PrizeProfile.cs b/Profiles/PrizeProfile.cs index 1a40062..1da00a9 100644 --- a/Profiles/PrizeProfile.cs +++ b/Profiles/PrizeProfile.cs @@ -9,7 +9,8 @@ namespace BackendPIA.Profiles { public PrizeProfile() { CreateMap(); CreateMap, JsonPatchDocument>(); - CreateMap, Operation>();  + CreateMap, Operation>(); + CreateMap(); } } } \ No newline at end of file diff --git a/Services/IRaffleService.cs b/Services/IRaffleService.cs index 2ee8b10..5102af2 100644 --- a/Services/IRaffleService.cs +++ b/Services/IRaffleService.cs @@ -10,5 +10,6 @@ namespace BackendPIA.Services { public Task> GetTakenTickets(long id); public IEnumerable GetRaffleTickets(long id); public Task> GetRaffleWinners(long id); + public bool RaffleExists(long id); } } \ No newline at end of file diff --git a/Services/RaffleService.cs b/Services/RaffleService.cs index 5d1901f..bbc20f4 100644 --- a/Services/RaffleService.cs +++ b/Services/RaffleService.cs @@ -1,5 +1,6 @@ using Microsoft.EntityFrameworkCore; using BackendPIA.Models; +using BackendPIA.Forms; namespace BackendPIA.Services { public class RaffleService : IRaffleService { @@ -65,5 +66,13 @@ namespace BackendPIA.Services { return await _context.RaffleWinners.Include(rw => rw.UserAccount).Include(rw => rw.Raffle).Include(rw => rw.Prize) .Where(rw => rw.RaffleId == id).ToListAsync(); } + + public bool RaffleExists(long id) { + return _context.Raffles.Any(r => r.Id == id); + } + + public IEnumerable GetRafflePrizes(long id) { + return _context.Prizes.Where(p => p.RaffleId == id).ToList(); + } } } \ No newline at end of file diff --git a/Services/UserAccountService.cs b/Services/UserAccountService.cs index eccaff3..2ae3240 100644 --- a/Services/UserAccountService.cs +++ b/Services/UserAccountService.cs @@ -5,9 +5,11 @@ using BackendPIA.Forms; namespace BackendPIA.Services { public class UserAccountService : IUserAccountService { private readonly UserManager _manager; + private readonly ApplicationDbContext _context; - public UserAccountService(UserManager manager) { + public UserAccountService(UserManager manager, ApplicationDbContext context) { _manager = manager; + _context = context; } public async Task CreateUserAccount(UserAccount user, string password, string role) { @@ -22,6 +24,9 @@ namespace BackendPIA.Services { public async Task GetUserAccount(string email) { var result = await _manager.FindByEmailAsync(email); + if(result != null) + result.Tickets = _context.Tickets.Where(t => t.Owner == result).ToList(); + return result; } } diff --git a/Validations/MaximumWinners.cs b/Validations/MaximumWinners.cs new file mode 100644 index 0000000..06a467a --- /dev/null +++ b/Validations/MaximumWinners.cs @@ -0,0 +1,27 @@ +using System.ComponentModel.DataAnnotations; +using BackendPIA.Models; + +namespace BackendPIA.Validations { + public class MaximumWinners : ValidationAttribute { + public string GetMaximumWinnersMessage(object? value) { + return $"The given raffle has reached the prize limit."; + } + + public string GetNullRaffleErrorMessage(object? value) { + return $"The raffle with id {value} doesn't exist"; + } + + protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) { + var db_context = (ApplicationDbContext) validationContext.GetService(typeof(ApplicationDbContext)); + var raffle = db_context.Raffles.Find((long) value); + + if(raffle == null) + return new ValidationResult(GetNullRaffleErrorMessage(value)); + + if(db_context.Prizes.Where(p => p.RaffleId == (long) value).Count() >= raffle.Winners) + return new ValidationResult(GetMaximumWinnersMessage(value)); + + return ValidationResult.Success; + } + } +} \ No newline at end of file -- cgit v1.2.3