diff options
Diffstat (limited to 'Validations')
-rw-r--r-- | Validations/UniqueTier.cs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/Validations/UniqueTier.cs b/Validations/UniqueTier.cs new file mode 100644 index 0000000..c91db1c --- /dev/null +++ b/Validations/UniqueTier.cs @@ -0,0 +1,21 @@ +using System.ComponentModel.DataAnnotations; +using BackendPIA.Models; +using BackendPIA.Forms; + +namespace BackendPIA.Validations { + public class UniqueTier : ValidationAttribute { + public string GetErrorMessage(object? value) { + return $"The given raffle already has a {value}-Tier."; + } + + protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) { + var prize = (PrizeForm) validationContext.ObjectInstance; + var db_context = (ApplicationDbContext) validationContext.GetService(typeof(ApplicationDbContext)); + + if(db_context.Prizes.Where(p => p.RaffleId == prize.RaffleId).Where(p => p.Tier == (int) value).Any()) + return new ValidationResult(GetErrorMessage(value)); + + return ValidationResult.Success; + } + } +}
\ No newline at end of file |