From 6a85255c78a448256fab68cd361983ffc85f2b03 Mon Sep 17 00:00:00 2001 From: transtrike Date: Fri, 11 Dec 2020 14:35:39 +0200 Subject: Renamed Models to Data --- Data/Models/Classes/User.cs | 93 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 Data/Models/Classes/User.cs (limited to 'Data/Models/Classes/User.cs') diff --git a/Data/Models/Classes/User.cs b/Data/Models/Classes/User.cs new file mode 100644 index 0000000..525d725 --- /dev/null +++ b/Data/Models/Classes/User.cs @@ -0,0 +1,93 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using Microsoft.AspNetCore.Identity; +using System.Collections.Generic; + +namespace Data.Models.Classes +{ + [Table("Users")] + public class User : IdentityUser + { + private string _firstName; + private string _lastName; + private string _profilePicture; + + [Required] + [Range(3, 50)] + [Display(Name = "Username")] + public override string UserName + { + get => base.UserName; + set + { + ValidateString("Username", 3, 50, value, true); + base.UserName = value; + } + } + + [Required] + [Range(3, 30)] + public string FirstName + { + get => this._firstName; + set + { + ValidateString("FirstName", 3, 30, value, false); + this._firstName = value; + } + } + + [Required] + [Range(3, 30)] + public string LastName + { + get => this._lastName; + set + { + ValidateString("LastName", 3, 30, value, false); + this._lastName = value; + } + } + + public string ProfilePicture + { + get => this._profilePicture; + set + { + ValidateURL(value); + this._profilePicture = value; + } + } + + // public List Friends { get; set; } + + /// + /// Throws an argument exception if the given value is not composed only of letters, and if specified, also of digits. + /// Does nothing otherwise. + /// + private static void ValidateString(string name, int minLength, int maxLength, string value, bool canBeDigit) + { + if (value.Length < minLength || value.Length > maxLength) + throw new ArgumentException($"{name} length cannot be less than {minLength} and more than {maxLength}."); + + foreach (char character in value) // more efficient than Linq + { + if (!Char.IsLetter(character)) + if (!(Char.IsDigit(character) && canBeDigit)) + throw new ArgumentException($"{name} contains invalid characters."); + } + } + + /// + /// Throws an exception if the absolute url isn't valid. + /// Does nothing otherwise. + /// + private static void ValidateURL(string urlValue) + { + // Throws an error is URL is invalid + Uri validatedUri; + Uri.TryCreate(urlValue, UriKind.Absolute, out validatedUri); + } + } +} -- cgit v1.2.3