Advertisement
Guest User

Untitled

a guest
Sep 28th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 KB | None | 0 0
  1. namespace UsersDatabase.Models
  2. {
  3.     using System;
  4.     using System.Linq;
  5.     using System.Text.RegularExpressions;
  6.  
  7.     public class User
  8.     {
  9.         private string username;
  10.         private string password;
  11.         private string email;
  12.         private byte[] profilePicture;
  13.         private int age;
  14.         public User(string username, string password, string email)
  15.         {
  16.             this.Username = username;
  17.             this.Password = password;
  18.             this.Email = email;
  19.         }
  20.         public int Id { get; set; }
  21.         public DateTime RegisteredOn { get; set; }
  22.         public DateTime LastTimeLoggedIn { get; set; }
  23.         public bool IsDeleted { get; set; }
  24.  
  25.         public string Username
  26.         {
  27.             get { return this.username; }
  28.             set
  29.             {
  30.                 if (value.Length < 4 || value.Length > 30)
  31.                 {
  32.                     throw new ArgumentException($"{nameof(this.Username)} can be between 4 and 30 symbols!");
  33.                 }
  34.  
  35.                 this.username = value;
  36.             }
  37.         }
  38.  
  39.         public string Password
  40.         {
  41.             get { return this.password; }
  42.             set
  43.             {
  44.                 var isContainsSpecialSymbol = false;
  45.                 var specialSymbols = "(!@#$%^&*()_+<>?".ToCharArray();
  46.  
  47.                 foreach (var specialSymbol in specialSymbols)
  48.                 {
  49.                     if (value.Contains(specialSymbol))
  50.                     {
  51.                         isContainsSpecialSymbol = true;
  52.                         break;
  53.                     }
  54.                 }
  55.  
  56.                 if (value.Length < 6 || value.Length > 50)
  57.                 {
  58.                     throw new ArgumentException($"{nameof(this.Password)} can be between 6 and 50 symbols!");
  59.                 }
  60.  
  61.                 if (!(value.Any(char.IsLower) && value.Any(char.IsUpper) && value.Any(char.IsDigit) && isContainsSpecialSymbol))
  62.                 {
  63.                     throw new ArgumentException($"{nameof(this.Password)} can be contains at least 1 lowercase letter, uppercase letter, digit and special symbol (!, @, #, $, %, ^, &, *, (, ), _, +, <, >, ?)");
  64.                 }
  65.  
  66.                 this.password = value;
  67.             }
  68.         }
  69.  
  70.         public string Email
  71.         {
  72.             get { return this.email; }
  73.             set
  74.             {
  75.                 string pattern = @"^[a-zA-Z0-9]+[.-_a-zA-Z-0-9]*[a-zA-Z0-9]+@[a-zA-Z]+\.[a-zA-Z]+$";
  76.                 Regex regex = new Regex(pattern);
  77.                 if (!regex.IsMatch(value))
  78.                 {
  79.                     throw new ArgumentException($"The format of {nameof(this.email)} is not correct!");
  80.                 }
  81.  
  82.                 this.email = value;
  83.             }
  84.         }
  85.  
  86.         public byte[] ProfilePicture
  87.         {
  88.             get { return this.profilePicture; }
  89.             set
  90.             {
  91.                 if (value.Length > 1024 * 1024)
  92.                 {
  93.                     throw new ArgumentException($"Max size of profile picture is 1MB!");
  94.                 }
  95.  
  96.                 this.profilePicture = value;
  97.             }
  98.         }
  99.  
  100.         public int Age
  101.         {
  102.             get { return this.age; }
  103.             set
  104.             {
  105.                 if (value < 1 || value > 120)
  106.                 {
  107.                     throw new ArgumentException($"{nameof(this.Age)} can be between 1 and 120!");
  108.                 }
  109.  
  110.                 this.age = value;
  111.             }
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement