aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Services/EntityType.cs
blob: 5111904f4b98d76fe2b1e1d1f93519847b77d46c (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
46
47
48
49
50
51
52
53
using System;
using System.Reflection;

namespace Timeline.Services
{
    public class EntityType : IEquatable<EntityType>
    {
        public EntityType(string name)
        {
            Name = name;
        }

        public string Name { get; }

        public int NotExistErrorCode
        {
            get
            {
                var field = typeof(ErrorCodes.NotExist).GetField(Name, BindingFlags.Public | BindingFlags.Static);
                if (field is not null) return (int)field.GetRawConstantValue()!;
                return ErrorCodes.NotExist.Default;
            }
        }

        public int ConflictErrorCode
        {
            get
            {
                var field = typeof(ErrorCodes.Conflict).GetField(Name, BindingFlags.Public | BindingFlags.Static);
                if (field is not null) return (int)field.GetRawConstantValue()!;
                return ErrorCodes.Conflict.Default;
            }
        }

        public bool Equals(EntityType? other)
        {
            if (other is null)
                return false;

            return other.Name.Equals(other.Name);
        }

        public override bool Equals(object? obj)
        {
            return Equals(obj as EntityType);
        }

        public override int GetHashCode()
        {
            return Name.GetHashCode();
        }
    }
}