diff --git a/Communications/SimApiBaseRequest.cs b/Communications/SimApiBaseRequest.cs
index b6ec9f0..ba55ccd 100644
--- a/Communications/SimApiBaseRequest.cs
+++ b/Communications/SimApiBaseRequest.cs
@@ -1,39 +1,23 @@
-using System;
-
-namespace SimApi.Communications
+namespace SimApi.Communications
{
///
/// 只有ID的请求
///
- public class SimApiIdOnlyRequest
- {
- public int Id { get; set; }
- }
+ public record SimApiIdOnlyRequest(int Id);
///
/// 只有ID的请求(字符串)
///
- public class SimApiStringIdOnlyRequest
- {
- public string Id { get; set; }
- }
+ public record SimApiStringIdOnlyRequest(string Id);
///
/// 动态类型单字段请求
///
///
- public class SimApiOneFieldRequest
- {
- public T Data { get; set; }
- }
+ public record SimApiOneFieldRequest(T Data);
///
/// 基础分页请求
///
- public class SimApiBasePageRequest
- {
- public int Page { get; set; }
-
- public int Count { get; set; }
- }
+ public record SimApiBasePageRequest(int Page, int Count);
}
\ No newline at end of file
diff --git a/Communications/SimApiBaseResponse.cs b/Communications/SimApiBaseResponse.cs
index 773eef5..00b70f1 100644
--- a/Communications/SimApiBaseResponse.cs
+++ b/Communications/SimApiBaseResponse.cs
@@ -7,82 +7,38 @@ namespace SimApi.Communications
///
/// 基础相应
///
- public class SimApiBaseResponse
+ public record SimApiBaseResponse(int Code = 200, string Message = "成功")
{
- ///
- /// 错误代码
- ///
- public int Code { get; set; }
-
- ///
- /// 错误描述
- ///
- public string Message { get; set; }
-
///
/// 默认错误代码对应提示信息
///
- private readonly Dictionary MsgBox = new Dictionary()
+ private static readonly Dictionary MsgBox = new()
{
- {200, "成功"},
- {204, "没有数据"},
- {400, "参数错误"},
- {401, "需要登录"},
- {403, "无权访问"},
- {404, "接口不存在"},
- {500, "服务器错误"}
+ {
+ 200, "成功"
+ },
+ {
+ 204, "没有数据"
+ },
+ {
+ 400, "参数错误"
+ },
+ {
+ 401, "需要登录"
+ },
+ {
+ 403, "无权访问"
+ },
+ {
+ 404, "接口不存在"
+ },
+ {
+ 500, "服务器错误"
+ }
};
- ///
- /// 返回一个成功的空结果
- ///
- public SimApiBaseResponse()
+ public SimApiBaseResponse(int code) : this(code, MsgBox.ContainsKey(code) ? MsgBox[code] : "未知错误")
{
- SetCode(200);
- }
-
-
- ///
- /// 返回指定代码的描述
- ///
- /// 错误代码
- public SimApiBaseResponse(int code)
- {
- SetCode(code);
- }
-
- ///
- /// 返回指定代码的结果,并自定义提示信息
- ///
- /// 错误代码
- /// 错误信息
- public SimApiBaseResponse(int code, string message)
- {
- SetCodeMsg(code, message);
- }
-
- ///
- /// 设置响应结果的错误代码
- ///
- /// 错误代码
- public void SetCode(int code)
- {
- Code = code;
- Message = MsgBox.ContainsKey(code) ? MsgBox[code] : "未知错误";
- }
-
- ///
- /// 设置响应结果的错误代码和错误描述
- ///
- /// 错误代码
- /// 错误描述
- public void SetCodeMsg(int code, string message)
- {
- SetCode(code);
- if (!string.IsNullOrEmpty(message))
- {
- Message = message;
- }
}
///
@@ -95,7 +51,10 @@ namespace SimApi.Communications
{
IgnoreReadOnlyProperties = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
- Converters = {new JsonStringEnumConverter()}
+ Converters =
+ {
+ new JsonStringEnumConverter()
+ }
});
}
}
@@ -104,32 +63,14 @@ namespace SimApi.Communications
/// 动态内容分页
///
///
- public class SimApiBasePageResponse : SimApiBaseResponse
- {
- ///
- /// 动态内容列表
- ///
- public T List { get; set; }
-
- //当前页码
- public int Page { get; set; }
-
- //每页条数
- public int Count { get; set; }
-
- //总计条数
- public int Total { get; set; }
- }
+ public record SimApiBasePageResponse
+ (T List, int Page = 1, int Count = 1, int Total = 1, int Code = 200,
+ string Message = "成功") : SimApiBaseResponse(Code, Message);
///
/// 动态Data返回
///
///
- public class SimApiBaseResponse : SimApiBaseResponse
- {
- ///
- /// 动态内容列表
- ///
- public T Data { get; set; }
- }
+ public record SimApiBaseResponse(T Data, int Code = 200, string Message = "成功") : SimApiBaseResponse(Code,
+ Message);
}
\ No newline at end of file
diff --git a/Communications/SimApiLoginItem.cs b/Communications/SimApiLoginItem.cs
index 17bdda2..7552735 100644
--- a/Communications/SimApiLoginItem.cs
+++ b/Communications/SimApiLoginItem.cs
@@ -1,16 +1,7 @@
-using System;
-
-namespace SimApi.Communications
+namespace SimApi.Communications
{
///
/// 登录信息中间件
///
- public class SimApiLoginItem
- {
- //登录用户的ID
- public string Id { get; set; }
-
- //登录用户来源
- public string[] Type { get; set; }
- }
+ public record SimApiLoginItem(string Id, string[] Type);
}
\ No newline at end of file
diff --git a/Controllers/SimApiBaseController.cs b/Controllers/SimApiBaseController.cs
index e0d7e96..b126469 100644
--- a/Controllers/SimApiBaseController.cs
+++ b/Controllers/SimApiBaseController.cs
@@ -29,13 +29,11 @@ namespace SimApi.Controllers
{
if (!context.ModelState.IsValid)
{
- foreach (var item in context.ModelState.Values)
+ foreach (var item in context.ModelState.Values.Where(item =>
+ item.ValidationState == ModelValidationState.Invalid))
{
- if (item.ValidationState == ModelValidationState.Invalid)
- {
- Error(400, item.Errors.First().ErrorMessage);
- break;
- }
+ Error(400, item.Errors.First().ErrorMessage);
+ break;
}
}
}
@@ -83,7 +81,7 @@ namespace SimApi.Controllers
///
protected SimApiBaseResponse UploadFile()
{
- return new SimApiBaseResponse();
+ return new SimApiBaseResponse(null);
}
}
}
\ No newline at end of file
diff --git a/Controllers/SimApiCommonController.cs b/Controllers/SimApiCommonController.cs
index 48a90d5..3e48f55 100644
--- a/Controllers/SimApiCommonController.cs
+++ b/Controllers/SimApiCommonController.cs
@@ -25,9 +25,7 @@ namespace SimApi.Controllers
[ApiExplorerSettings(IgnoreApi = true)]
public SimApiBaseResponse ExceptionHandler(int code)
{
- var response = new SimApiBaseResponse();
- response.SetCode(code);
- return response;
+ return new SimApiBaseResponse(code);
}
///
@@ -38,10 +36,7 @@ namespace SimApi.Controllers
public SimApiBaseResponse CheckLogin()
{
ErrorWhenNull(LoginInfo, 401);
- return new SimApiBaseResponse
- {
- Data = LoginInfo.Id
- };
+ return new SimApiBaseResponse(LoginInfo.Id);
}
///
diff --git a/Helpers/SimApiAuth.cs b/Helpers/SimApiAuth.cs
index 6a8762c..f5ec70d 100644
--- a/Helpers/SimApiAuth.cs
+++ b/Helpers/SimApiAuth.cs
@@ -25,7 +25,10 @@ namespace SimApi.Helpers
///
public string Login(string id, string type = "user", string token = null)
{
- return Login(id, new[] { type }, token);
+ return Login(id, new[]
+ {
+ type
+ }, token);
}
///
@@ -36,15 +39,8 @@ namespace SimApi.Helpers
///
public string Login(string id, string[] type, string uuid = null)
{
- if (uuid == null)
- {
- uuid = Guid.NewGuid().ToString();
- }
- var loginItem = new SimApiLoginItem
- {
- Id = id,
- Type = type
- };
+ uuid ??= Guid.NewGuid().ToString();
+ var loginItem = new SimApiLoginItem(id, type);
Cache.SetString(uuid, JsonSerializer.Serialize(loginItem));
return uuid;
}
@@ -60,6 +56,5 @@ namespace SimApi.Helpers
Cache.Remove(uuid);
}
}
-
}
-}
+}
\ No newline at end of file
diff --git a/Middlewares/SimApiExceptionMiddleware.cs b/Middlewares/SimApiExceptionMiddleware.cs
index 74b0ce7..75f8695 100644
--- a/Middlewares/SimApiExceptionMiddleware.cs
+++ b/Middlewares/SimApiExceptionMiddleware.cs
@@ -47,14 +47,9 @@ namespace SimApi.Middlewares
}
catch (SimApiException ex)
{
- if (string.IsNullOrEmpty(ex.Message))
- {
- response.SetCode(ex.Code);
- }
- else
- {
- response.SetCodeMsg(ex.Code, ex.Message);
- }
+ response = string.IsNullOrEmpty(ex.Message)
+ ? new SimApiBaseResponse(ex.Code)
+ : new SimApiBaseResponse(ex.Code, ex.Message);
ErrorResponse(context, response);
}
@@ -62,7 +57,7 @@ namespace SimApi.Middlewares
{
Log.LogError(ex.Message);
Log.LogError(ex.StackTrace);
- response.SetCodeMsg(500, ex.Message);
+ response = new SimApiBaseResponse(500, ex.Message);
ErrorResponse(context, response);
}
}
diff --git a/Models/SimApiBaseModel.cs b/Models/SimApiBaseModel.cs
index 38fb09c..2cbcfbc 100644
--- a/Models/SimApiBaseModel.cs
+++ b/Models/SimApiBaseModel.cs
@@ -32,15 +32,12 @@ namespace SimApi.Models
x.PropertyType
})
.ToDictionary(x => x.Name, x => x.PropertyType);
- foreach (var sp in sourceProps)
+ foreach (var sp in sourceProps.Where(sp =>
+ targetProps.ContainsKey(sp.Key) && sp.Value == targetProps[sp.Key] &&
+ (!MapperIgnoreField.Contains(sp.Key) || mapAll)))
{
- //检查源对象不为空的属性是否在目标对象中存在
- if (targetProps.ContainsKey(sp.Key) && sp.Value == targetProps[sp.Key] &&
- (!MapperIgnoreField.Contains(sp.Key) || mapAll))
- {
- GetType().GetProperty(sp.Key)?
- .SetValue(this, source.GetType().GetProperty(sp.Key)?.GetValue(source));
- }
+ GetType().GetProperty(sp.Key)?
+ .SetValue(this, source.GetType().GetProperty(sp.Key)?.GetValue(source));
}
UpdateTime();
@@ -64,15 +61,12 @@ namespace SimApi.Models
x.PropertyType
})
.ToDictionary(x => x.Name, x => x.PropertyType);
- foreach (var sp in sourceProps)
+ foreach (var sp in sourceProps.Where(sp =>
+ targetProps.ContainsKey(sp.Key) && sp.Value == targetProps[sp.Key] &&
+ mapFields.Contains(sp.Key)))
{
- //检查源对象不为空的属性是否在目标对象中存在
- if (targetProps.ContainsKey(sp.Key) && sp.Value == targetProps[sp.Key] &&
- mapFields.Contains(sp.Key))
- {
- GetType().GetProperty(sp.Key)?
- .SetValue(this, source.GetType().GetProperty(sp.Key)?.GetValue(source));
- }
+ GetType().GetProperty(sp.Key)?
+ .SetValue(this, source.GetType().GetProperty(sp.Key)?.GetValue(source));
}
UpdateTime();