diff options
author | 杨宇千 <crupest@outlook.com> | 2019-08-22 15:29:03 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-22 15:29:03 +0800 |
commit | 11f01c56b4ea1dbb09d04258bec89f800c6ee2b6 (patch) | |
tree | af83f8596b4fa78713733c0db6b4b6d1695d0ff0 /Timeline/Models | |
parent | fd95f9abc017575b13a31dd16ac72ef663e984d6 (diff) | |
parent | 96c18fb2e17c94ff04094608c705db087400f510 (diff) | |
download | timeline-11f01c56b4ea1dbb09d04258bec89f800c6ee2b6.tar.gz timeline-11f01c56b4ea1dbb09d04258bec89f800c6ee2b6.tar.bz2 timeline-11f01c56b4ea1dbb09d04258bec89f800c6ee2b6.zip |
Merge pull request #48 from crupest/user-details
Add user details.
Diffstat (limited to 'Timeline/Models')
-rw-r--r-- | Timeline/Models/UserDetail.cs | 43 | ||||
-rw-r--r-- | Timeline/Models/Validation/UserDetailValidator.cs | 116 |
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;
+ }
+ }
+ }
+}
|