summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2022-11-28 17:49:34 -0600
committerHombreLaser <sebastian-440@live.com>2022-11-28 17:49:34 -0600
commit5fe81d9515d71dad1e37f7ee3262a44c52e2599e (patch)
tree3a366584c0b60c0e0e2bd7d53992094d1cf5fc50
parent9149e64398e7b9f7b29bcf68db7eff2ce5f8169c (diff)
Añadido validador de límite para premios
-rw-r--r--Controllers/RafflesController.cs23
-rw-r--r--Forms/PrizeDTO.cs8
-rw-r--r--Forms/PrizeForm.cs1
-rw-r--r--Policies/CorrectTokenHandler.cs2
-rw-r--r--Profiles/PrizeProfile.cs3
-rw-r--r--Services/IRaffleService.cs1
-rw-r--r--Services/RaffleService.cs9
-rw-r--r--Services/UserAccountService.cs7
-rw-r--r--Validations/MaximumWinners.cs27
9 files changed, 74 insertions, 7 deletions
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<ActionResult<IEnumerable<RaffleDTO>>> Index([FromQuery] string name = "") {
var result = await _service.GetRaffles(name);
-
+
return Ok(_mapper.Map<IEnumerable<RaffleDTO>>(result));
}
[HttpGet("{id:int}")]
- public async Task<ActionResult<RaffleDTO>> Show(long id) {
+ public async Task<ActionResult<RaffleDTO>> Show(long id) {
var raffle = await _service.GetRaffle(id);
if(raffle == null)
@@ -76,8 +76,11 @@ namespace BackendPIA.Controllers {
IEnumerable<int> available_tickets = from number in Enumerable.Range(1, 54) select number;
IEnumerable<int> 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<ActionResult<IEnumerable<WinnerDTO>>> 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<IEnumerable<WinnerDTO>>(result));
}
+
+ [HttpGet("{id:int}/prizes")]
+ public async Task<ActionResult<IEnumerable<PrizeDTO>>> 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<IEnumerable<PrizeDTO>>(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<PrizeForm, Prize>();
CreateMap<JsonPatchDocument<PrizeForm>, JsonPatchDocument<Prize>>();
- CreateMap<Operation<PrizeForm>, Operation<Prize>>(); 
+ CreateMap<Operation<PrizeForm>, Operation<Prize>>();
+ CreateMap<Prize, PrizeDTO>();
}
}
} \ 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<IEnumerable<int>> GetTakenTickets(long id);
public IEnumerable<Ticket> GetRaffleTickets(long id);
public Task<IEnumerable<RaffleWinner>> 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<Prize> 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<UserAccount> _manager;
+ private readonly ApplicationDbContext _context;
- public UserAccountService(UserManager<UserAccount> manager) {
+ public UserAccountService(UserManager<UserAccount> manager, ApplicationDbContext context) {
_manager = manager;
+ _context = context;
}
public async Task<IdentityResult> CreateUserAccount(UserAccount user, string password, string role) {
@@ -22,6 +24,9 @@ namespace BackendPIA.Services {
public async Task<UserAccount> 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