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 --- Validations/MaximumWinners.cs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Validations/MaximumWinners.cs (limited to 'Validations/MaximumWinners.cs') 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