aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Models
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-08-22 15:29:03 +0800
committerGitHub <noreply@github.com>2019-08-22 15:29:03 +0800
commit3716d2431de08194d3a107ef640febc47c3ee72a (patch)
treeaf83f8596b4fa78713733c0db6b4b6d1695d0ff0 /Timeline/Models
parenta585c6e35829e9f2b4b0b8ce8c6b395e5ea84f2c (diff)
parent968f9688dd3ff7cae6f66af0e69bb03392311c88 (diff)
downloadtimeline-3716d2431de08194d3a107ef640febc47c3ee72a.tar.gz
timeline-3716d2431de08194d3a107ef640febc47c3ee72a.tar.bz2
timeline-3716d2431de08194d3a107ef640febc47c3ee72a.zip
Merge pull request #48 from crupest/user-details
Add user details.
Diffstat (limited to 'Timeline/Models')
-rw-r--r--Timeline/Models/UserDetail.cs43
-rw-r--r--Timeline/Models/Validation/UserDetailValidator.cs116
2 files changed, 159 insertions, 0 deletions
diff --git a/Timeline/Models/UserDetail.cs b/Timeline/Models/UserDetail.cs
new file mode 100644
index 00000000..86866d8b
--- /dev/null
+++ b/Timeline/Models/UserDetail.cs
@@ -0,0 +1,43 @@
+using System.ComponentModel.DataAnnotations;
+using Timeline.Entities;
+using Timeline.Models.Validation;
+
+namespace Timeline.Models
+{
+ public class UserDetail
+ {
+ [MaxLength(10)]
+ public string Nickname { get; set; }
+
+ [ValidateWith(typeof(UserDetailValidators.QQValidator))]
+ public string QQ { get; set; }
+
+ [ValidateWith(typeof(UserDetailValidators.EMailValidator))]
+ public string EMail { get; set; }
+
+ [ValidateWith(typeof(UserDetailValidators.PhoneNumberValidator))]
+ public string PhoneNumber { get; set; }
+
+ public string Description { get; set; }
+
+ private static string CoerceEmptyToNull(string value)
+ {
+ if (string.IsNullOrEmpty(value))
+ return null;
+ else
+ return value;
+ }
+
+ public static UserDetail From(UserDetailEntity entity)
+ {
+ return new UserDetail
+ {
+ Nickname = CoerceEmptyToNull(entity.Nickname),
+ QQ = CoerceEmptyToNull(entity.QQ),
+ EMail = CoerceEmptyToNull(entity.EMail),
+ PhoneNumber = CoerceEmptyToNull(entity.PhoneNumber),
+ Description = CoerceEmptyToNull(entity.Description)
+ };
+ }
+ }
+}
diff --git a/Timeline/Models/Validation/UserDetailValidator.cs b/Timeline/Models/Validation/UserDetailValidator.cs
new file mode 100644
index 00000000..19c82edb
--- /dev/null
+++ b/Timeline/Models/Validation/UserDetailValidator.cs
@@ -0,0 +1,116 @@
+using System;
+using System.Net.Mail;
+
+namespace Timeline.Models.Validation
+{
+ public abstract class OptionalStringValidator : IValidator
+ {
+ public bool Validate(object value, out string message)
+ {
+ if (value == null)
+ {
+ message = ValidationConstants.SuccessMessage;
+ return true;
+ }
+
+ if (value is string s)
+ {
+ if (s.Length == 0)
+ {
+ message = ValidationConstants.SuccessMessage;
+ return true;
+ }
+ return DoValidate(s, out message);
+ }
+ else
+ {
+ message = "Value is not of type string.";
+ return false;
+ }
+ }
+
+ protected abstract bool DoValidate(string value, out string message);
+ }
+
+ public static class UserDetailValidators
+ {
+
+ public class QQValidator : OptionalStringValidator
+ {
+ protected override bool DoValidate(string value, out string message)
+ {
+ if (value.Length < 5)
+ {
+ message = "QQ is too short.";
+ return false;
+ }
+
+ if (value.Length > 11)
+ {
+ message = "QQ is too long.";
+ return false;
+ }
+
+ foreach (var c in value)
+ {
+ if (!char.IsDigit(c))
+ {
+ message = "QQ must only contain digit.";
+ return false;
+ }
+ }
+
+ message = ValidationConstants.SuccessMessage;
+ return true;
+ }
+ }
+
+ public class EMailValidator : OptionalStringValidator
+ {
+ protected override bool DoValidate(string value, out string message)
+ {
+ if (value.Length > 50)
+ {
+ message = "E-Mail is too long.";
+ return false;
+ }
+
+ try
+ {
+ var _ = new MailAddress(value);
+ }
+ catch (FormatException)
+ {
+ message = "The format of E-Mail is bad.";
+ return false;
+ }
+ message = ValidationConstants.SuccessMessage;
+ return true;
+ }
+ }
+
+ public class PhoneNumberValidator : OptionalStringValidator
+ {
+ protected override bool DoValidate(string value, out string message)
+ {
+ if (value.Length > 14)
+ {
+ message = "Phone number is too long.";
+ return false;
+ }
+
+ foreach (var c in value)
+ {
+ if (!char.IsDigit(c))
+ {
+ message = "Phone number can only contain digit.";
+ return false;
+ }
+ }
+
+ message = ValidationConstants.SuccessMessage;
+ return true;
+ }
+ }
+ }
+}