diff options
Diffstat (limited to 'Validations/IsNotClosed.cs')
-rw-r--r-- | Validations/IsNotClosed.cs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/Validations/IsNotClosed.cs b/Validations/IsNotClosed.cs new file mode 100644 index 0000000..3277356 --- /dev/null +++ b/Validations/IsNotClosed.cs @@ -0,0 +1,28 @@ +using System.ComponentModel.DataAnnotations; +using BackendPIA.Models; +using BackendPIA.Forms; + +namespace BackendPIA.Validations { + public class IsNotClosed : ValidationAttribute { + public string GetIsClosedErrorMessage(object? value) { + return $"The given raffle is already closed."; + } + + 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(raffle.IsClosed) + return new ValidationResult(GetIsClosedErrorMessage(value)); + + return ValidationResult.Success; + } + } +}
\ No newline at end of file |