Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace UsersDatabase.Models
- {
- using System;
- using System.Linq;
- using System.Text.RegularExpressions;
- public class User
- {
- private string username;
- private string password;
- private string email;
- private byte[] profilePicture;
- private int age;
- public User(string username, string password, string email)
- {
- this.Username = username;
- this.Password = password;
- this.Email = email;
- }
- public int Id { get; set; }
- public DateTime RegisteredOn { get; set; }
- public DateTime LastTimeLoggedIn { get; set; }
- public bool IsDeleted { get; set; }
- public string Username
- {
- get { return this.username; }
- set
- {
- if (value.Length < 4 || value.Length > 30)
- {
- throw new ArgumentException($"{nameof(this.Username)} can be between 4 and 30 symbols!");
- }
- this.username = value;
- }
- }
- public string Password
- {
- get { return this.password; }
- set
- {
- var isContainsSpecialSymbol = false;
- var specialSymbols = "(!@#$%^&*()_+<>?".ToCharArray();
- foreach (var specialSymbol in specialSymbols)
- {
- if (value.Contains(specialSymbol))
- {
- isContainsSpecialSymbol = true;
- break;
- }
- }
- if (value.Length < 6 || value.Length > 50)
- {
- throw new ArgumentException($"{nameof(this.Password)} can be between 6 and 50 symbols!");
- }
- if (!(value.Any(char.IsLower) && value.Any(char.IsUpper) && value.Any(char.IsDigit) && isContainsSpecialSymbol))
- {
- throw new ArgumentException($"{nameof(this.Password)} can be contains at least 1 lowercase letter, uppercase letter, digit and special symbol (!, @, #, $, %, ^, &, *, (, ), _, +, <, >, ?)");
- }
- this.password = value;
- }
- }
- public string Email
- {
- get { return this.email; }
- set
- {
- string pattern = @"^[a-zA-Z0-9]+[.-_a-zA-Z-0-9]*[a-zA-Z0-9]+@[a-zA-Z]+\.[a-zA-Z]+$";
- Regex regex = new Regex(pattern);
- if (!regex.IsMatch(value))
- {
- throw new ArgumentException($"The format of {nameof(this.email)} is not correct!");
- }
- this.email = value;
- }
- }
- public byte[] ProfilePicture
- {
- get { return this.profilePicture; }
- set
- {
- if (value.Length > 1024 * 1024)
- {
- throw new ArgumentException($"Max size of profile picture is 1MB!");
- }
- this.profilePicture = value;
- }
- }
- public int Age
- {
- get { return this.age; }
- set
- {
- if (value < 1 || value > 120)
- {
- throw new ArgumentException($"{nameof(this.Age)} can be between 1 and 120!");
- }
- this.age = value;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement