aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Models/Http/Common.cs
blob: 2735e43ca46bc38775fa270e9f1b56da6de85244 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using Microsoft.Extensions.Localization;
using Timeline.Helpers;

namespace Timeline.Models.Http
{
    public class CommonResponse
    {
        public static CommonResponse InvalidModel(string message)
        {
            return new CommonResponse(ErrorCodes.Http.Common.InvalidModel, message);
        }

        public static CommonResponse MissingContentType()
        {
            return new CommonResponse(ErrorCodes.Http.Common.Header.Missing_ContentType, "Header Content-Type is required.");
        }

        public static CommonResponse MissingContentLength()
        {
            return new CommonResponse(ErrorCodes.Http.Common.Header.Missing_ContentLength, "Header Content-Length is missing or of bad format.");
        }

        public static CommonResponse ZeroContentLength()
        {
            return new CommonResponse(ErrorCodes.Http.Common.Header.Zero_ContentLength, "Header Content-Length must not be 0.");
        }

        public static CommonResponse BadIfNonMatch()
        {
            return new CommonResponse(ErrorCodes.Http.Common.Header.BadFormat_IfNonMatch, "Header If-Non-Match is of bad format.");
        }

        public CommonResponse()
        {

        }

        public CommonResponse(int code, string message)
        {
            Code = code;
            Message = message;
        }

        public int Code { get; set; }
        public string? Message { get; set; }
    }

    public class CommonDataResponse<T> : CommonResponse
    {
        public CommonDataResponse()
        {

        }

        public CommonDataResponse(int code, string message, T data)
            : base(code, message)
        {
            Data = data;
        }

        public T Data { get; set; } = default!;
    }

    public static class CommonPutResponse
    {
        public class ResponseData
        {
            public ResponseData(bool create)
            {
                Create = create;
            }

            public bool Create { get; set; }
        }

        internal static CommonDataResponse<ResponseData> Create(IStringLocalizerFactory localizerFactory)
        {
            var localizer = localizerFactory.Create("Http.Common");
            return new CommonDataResponse<ResponseData>(0, localizer["ResponsePutCreate"], new ResponseData(true));
        }

        internal static CommonDataResponse<ResponseData> Modify(IStringLocalizerFactory localizerFactory)
        {
            var localizer = localizerFactory.Create("Http.Common");
            return new CommonDataResponse<ResponseData>(0, localizer["ResponsePutModify"], new ResponseData(false));

        }
    }

    public static class CommonDeleteResponse
    {
        public class ResponseData
        {
            public ResponseData(bool delete)
            {
                Delete = delete;
            }

            public bool Delete { get; set; }
        }

        internal static CommonDataResponse<ResponseData> Delete(IStringLocalizerFactory localizerFactory)
        {
            var localizer = localizerFactory.Create("Http.Common");
            return new CommonDataResponse<ResponseData>(0, localizer["ResponseDeleteDelete"], new ResponseData(true));
        }

        internal static CommonDataResponse<ResponseData> NotExist(IStringLocalizerFactory localizerFactory)
        {
            var localizer = localizerFactory.Create("Http.Common");
            return new CommonDataResponse<ResponseData>(0, localizer["ResponseDeleteNotExist"], new ResponseData(false));
        }
    }
}