summaryrefslogtreecommitdiff
path: root/Validations
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 /Validations
parent9149e64398e7b9f7b29bcf68db7eff2ce5f8169c (diff)
Añadido validador de límite para premios
Diffstat (limited to 'Validations')
-rw-r--r--Validations/MaximumWinners.cs27
1 files changed, 27 insertions, 0 deletions
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