summaryrefslogtreecommitdiff
path: root/Validations/UniqueTier.cs
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2022-11-24 16:49:48 -0600
committerHombreLaser <sebastian-440@live.com>2022-11-24 16:49:48 -0600
commitbc6c7c8a8071eb5c5f8877247dde5dfab9f5e4f8 (patch)
tree55d7d77f44af8bce714be132c22f34d7d347c770 /Validations/UniqueTier.cs
parent52239a499dfff1fb4344419ee732d447f7fb3347 (diff)
Añadido modelo Prize
Diffstat (limited to 'Validations/UniqueTier.cs')
-rw-r--r--Validations/UniqueTier.cs21
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