From 04e417b04c5c4a5361bfe946bfdd459810aba899 Mon Sep 17 00:00:00 2001 From: Nobir Date: Mon, 18 Jul 2022 05:00:34 +0600 Subject: [PATCH] [Added] check email and username unique validation --- Models/UniqueEmailValidation.cs | 28 ++++++++++++++++++++++++++++ Models/UniqueUsernameValidation.cs | 29 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 Models/UniqueEmailValidation.cs create mode 100644 Models/UniqueUsernameValidation.cs diff --git a/Models/UniqueEmailValidation.cs b/Models/UniqueEmailValidation.cs new file mode 100644 index 0000000..4652c49 --- /dev/null +++ b/Models/UniqueEmailValidation.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.ComponentModel.DataAnnotations; +using ERPSystemTimologio.EF; + +namespace ERPSystemTimologio.Models +{ + public class UniqueEmailValidation : ValidationAttribute + { + protected override ValidationResult IsValid(object value, ValidationContext validationContext) + { + if(value != null) + { + var db = new TimologioEntities(); + + int isExists = db.Users.Where(u => u.Email.Equals(value.ToString())).Count(); + if (isExists > 0) + { + return new ValidationResult("The email is already is registered"); + } + } + + return ValidationResult.Success; + } + } +} \ No newline at end of file diff --git a/Models/UniqueUsernameValidation.cs b/Models/UniqueUsernameValidation.cs new file mode 100644 index 0000000..9c6a6b8 --- /dev/null +++ b/Models/UniqueUsernameValidation.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.ComponentModel.DataAnnotations; +using ERPSystemTimologio.EF; + +namespace ERPSystemTimologio.Models +{ + public class UniqueUsernameValidation : ValidationAttribute + { + protected override ValidationResult IsValid(object value, ValidationContext validationContext) + { + + if(value != null) + { + var db = new TimologioEntities(); + + int isExists = db.Users.Where(u => u.Username.Equals(value.ToString())).Count(); + if (isExists > 0) + { + return new ValidationResult("The username is already is registered"); + } + } + + return ValidationResult.Success; + } + } +} \ No newline at end of file