summaryrefslogtreecommitdiff
path: root/Validations/IsNotClosed.cs
blob: 3277356f46f4d6b5b62924f1d4727a6449fd5563 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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;
        }
    }
}