diff options
Diffstat (limited to 'Timeline/Models/Http/Common.cs')
-rw-r--r-- | Timeline/Models/Http/Common.cs | 160 |
1 files changed, 124 insertions, 36 deletions
diff --git a/Timeline/Models/Http/Common.cs b/Timeline/Models/Http/Common.cs index a72f187c..39ddddd9 100644 --- a/Timeline/Models/Http/Common.cs +++ b/Timeline/Models/Http/Common.cs @@ -1,76 +1,164 @@ +using Microsoft.Extensions.Localization;
+using Timeline.Helpers;
+
namespace Timeline.Models.Http
{
public class CommonResponse
{
- public static class ErrorCodes
+ internal static CommonResponse InvalidModel(string message)
+ {
+ return new CommonResponse(ErrorCodes.Http.Common.InvalidModel, message);
+ }
+
+ public CommonResponse()
{
- /// <summary>
- /// Used when the model is invaid.
- /// For example a required field is null.
- /// </summary>
- public const int InvalidModel = -100;
- public const int Header_Missing_ContentType = -111;
- public const int Header_Missing_ContentLength = -112;
- public const int Header_Zero_ContentLength = -113;
- public const int Header_BadFormat_IfNonMatch = -114;
}
- public static CommonResponse InvalidModel(string message)
+ public CommonResponse(int code, string message)
{
- return new CommonResponse(ErrorCodes.InvalidModel, message);
+ Code = code;
+ Message = message;
}
- public static CommonResponse MissingContentType()
+ public int Code { get; set; }
+ public string? Message { get; set; }
+ }
+
+ internal static class HeaderErrorResponse
+ {
+ internal static CommonResponse MissingContentType(IStringLocalizerFactory localizerFactory)
{
- return new CommonResponse(ErrorCodes.Header_Missing_ContentType, "Header Content-Type is required.");
+ var localizer = localizerFactory.Create("Models.Http.Common");
+ return new CommonResponse(ErrorCodes.Http.Common.Header.Missing_ContentType, localizer["HeaderMissingContentType"]);
}
- public static CommonResponse MissingContentLength()
+ internal static CommonResponse MissingContentLength(IStringLocalizerFactory localizerFactory)
{
- return new CommonResponse(ErrorCodes.Header_Missing_ContentLength, "Header Content-Length is missing or of bad format.");
+ var localizer = localizerFactory.Create("Models.Http.Common");
+ return new CommonResponse(ErrorCodes.Http.Common.Header.Missing_ContentLength, localizer["HeaderMissingContentLength"]);
}
- public static CommonResponse ZeroContentLength()
+ internal static CommonResponse ZeroContentLength(IStringLocalizerFactory localizerFactory)
{
- return new CommonResponse(ErrorCodes.Header_Zero_ContentLength, "Header Content-Length must not be 0.");
+ var localizer = localizerFactory.Create("Models.Http.Common");
+ return new CommonResponse(ErrorCodes.Http.Common.Header.Zero_ContentLength, localizer["HeaderZeroContentLength"]);
}
- public static CommonResponse BadIfNonMatch()
+ internal static CommonResponse BadIfNonMatch(IStringLocalizerFactory localizerFactory)
{
- return new CommonResponse(ErrorCodes.Header_BadFormat_IfNonMatch, "Header If-Non-Match is of bad format.");
+ var localizer = localizerFactory.Create("Models.Http.Common");
+ return new CommonResponse(ErrorCodes.Http.Common.Header.BadFormat_IfNonMatch, localizer["HeaderBadIfNonMatch"]);
}
+ }
- public CommonResponse()
+ internal static class ContentErrorResponse
+ {
+ internal static CommonResponse TooBig(IStringLocalizerFactory localizerFactory, string maxLength)
+ {
+ var localizer = localizerFactory.Create("Models.Http.Common");
+ return new CommonResponse(ErrorCodes.Http.Common.Content.TooBig, localizer["ContentTooBig", maxLength]);
+ }
+
+ internal static CommonResponse UnmatchedLength_Smaller(IStringLocalizerFactory localizerFactory)
+ {
+ var localizer = localizerFactory.Create("Models.Http.Common");
+ return new CommonResponse(ErrorCodes.Http.Common.Content.UnmatchedLength_Smaller, localizer["ContentUnmatchedLengthSmaller"]);
+ }
+ internal static CommonResponse UnmatchedLength_Bigger(IStringLocalizerFactory localizerFactory)
+ {
+ var localizer = localizerFactory.Create("Models.Http.Common");
+ return new CommonResponse(ErrorCodes.Http.Common.Content.UnmatchedLength_Bigger, localizer["ContentUnmatchedLengthBigger"]);
+ }
+ }
+
+
+ public class CommonDataResponse<T> : CommonResponse
+ {
+ public CommonDataResponse()
{
}
- public CommonResponse(int code, string message)
+ public CommonDataResponse(int code, string message, T data)
+ : base(code, message)
{
- Code = code;
- Message = message;
+ Data = data;
}
- public int Code { get; set; }
- public string Message { get; set; }
+ public T Data { get; set; } = default!;
}
- public static class CommonPutResponse
+ public class CommonPutResponse : CommonDataResponse<CommonPutResponse.ResponseData>
{
- public const int CreatedCode = 0;
- public const int ModifiedCode = 1;
+ public class ResponseData
+ {
+ public ResponseData(bool create)
+ {
+ Create = create;
+ }
- public static CommonResponse Created { get; } = new CommonResponse(CreatedCode, "A new item is created.");
- public static CommonResponse Modified { get; } = new CommonResponse(ModifiedCode, "An existent item is modified.");
+ public bool Create { get; set; }
+ }
+
+ public CommonPutResponse()
+ {
+
+ }
+
+ public CommonPutResponse(int code, string message, bool create)
+ : base(code, message, new ResponseData(create))
+ {
+
+ }
+
+ internal static CommonPutResponse Create(IStringLocalizerFactory localizerFactory)
+ {
+ var localizer = localizerFactory.Create("Models.Http.Common");
+ return new CommonPutResponse(0, localizer["PutCreate"], true);
+ }
+
+ internal static CommonPutResponse Modify(IStringLocalizerFactory localizerFactory)
+ {
+ var localizer = localizerFactory.Create("Models.Http.Common");
+ return new CommonPutResponse(0, localizer["PutModify"], false);
+
+ }
}
- public static class CommonDeleteResponse
+ public class CommonDeleteResponse : CommonDataResponse<CommonDeleteResponse.ResponseData>
{
- public const int DeletedCode = 0;
- public const int NotExistsCode = 1;
+ public class ResponseData
+ {
+ public ResponseData(bool delete)
+ {
+ Delete = delete;
+ }
- public static CommonResponse Deleted { get; } = new CommonResponse(DeletedCode, "An existent item is deleted.");
- public static CommonResponse NotExists { get; } = new CommonResponse(NotExistsCode, "The item does not exist.");
+ public bool Delete { get; set; }
+ }
+
+ public CommonDeleteResponse()
+ {
+
+ }
+
+ public CommonDeleteResponse(int code, string message, bool delete)
+ : base(code, message, new ResponseData(delete))
+ {
+
+ }
+
+ internal static CommonDeleteResponse Delete(IStringLocalizerFactory localizerFactory)
+ {
+ var localizer = localizerFactory.Create("Models.Http.Common");
+ return new CommonDeleteResponse(0, localizer["DeleteDelete"], true);
+ }
+
+ internal static CommonDeleteResponse NotExist(IStringLocalizerFactory localizerFactory)
+ {
+ var localizer = localizerFactory.Create("Models.Models.Http.Common");
+ return new CommonDeleteResponse(0, localizer["DeleteNotExist"], false);
+ }
}
}
|