summaryrefslogtreecommitdiff
path: root/Validations/MaximumWinners.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Validations/MaximumWinners.cs')
-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