aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Models/UserDetail.cs
blob: 302e3bb129de844563728ed215603b4ffce2d8f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.ComponentModel.DataAnnotations;
using Timeline.Entities;
using Timeline.Models.Validation;
using Newtonsoft.Json;

namespace Timeline.Models
{
    public class UserDetail
    {
        [MaxLength(10)]
        public string? Nickname { get; set; }

        [ValidateWith(typeof(UserDetailValidators.QQValidator))]
        [JsonProperty(PropertyName = "qq")]
        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)
            };
        }
    }
}