From e127d34d095b8d77577e647bf330f8dc2a8f5ffc Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 8 Dec 2020 15:56:50 +0200 Subject: Added initial implementation of User class --- Models/Classes/User.cs | 72 +++++++++++++++++++++++++++++++++++++++++++++++--- Models/Models.csproj | 5 ++++ 2 files changed, 74 insertions(+), 3 deletions(-) (limited to 'Models') diff --git a/Models/Classes/User.cs b/Models/Classes/User.cs index 97f79b7..2549f81 100644 --- a/Models/Classes/User.cs +++ b/Models/Classes/User.cs @@ -1,7 +1,73 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using Microsoft.AspNetCore.Identity; +using System.Collections.Generic; + namespace Models.Classes { - public class User + [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; } + + 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) || (canBeDigit && !Char.IsDigit(character))) + throw new ArgumentException($"{name} contains invalid characters."); + } + } + + private static void ValidateURL(string urlValue) { + // Throws an error is URL is invalid + Uri validatedUri; + Uri.TryCreate(urlValue, UriKind.Absolute, out validatedUri); + } } -} \ No newline at end of file +} diff --git a/Models/Models.csproj b/Models/Models.csproj index abb6514..e51f9c6 100644 --- a/Models/Models.csproj +++ b/Models/Models.csproj @@ -5,4 +5,9 @@ Models + + + + + -- cgit v1.2.3