feat(币种): 支持CNY/USD/HKD,添加用户默认本位币、币种枚举和入参校验
This commit is contained in:
parent
fac4eb5e0f
commit
1ec23bef3d
@ -67,6 +67,51 @@ public class PortfolioController : ControllerBase
|
||||
|
||||
_logger.LogInformation("Request to create portfolio");
|
||||
|
||||
// 1. 获取用户默认币种
|
||||
var db = _databaseService.GetDb();
|
||||
var user = db.Queryable<User>().Where(u => u.Id == userId).First();
|
||||
if (user == null)
|
||||
{
|
||||
return NotFound(new ApiResponse<CreatePortfolioResponse>
|
||||
{
|
||||
code = AssetManager.Models.StatusCodes.NotFound,
|
||||
data = null,
|
||||
message = "用户不存在"
|
||||
});
|
||||
}
|
||||
|
||||
// 2. 校验组合币种,如果没传则用用户默认币种
|
||||
if (string.IsNullOrEmpty(request.currency))
|
||||
{
|
||||
request.currency = user.DefaultCurrency;
|
||||
}
|
||||
if (!CurrencyHelper.IsSupported(request.currency))
|
||||
{
|
||||
return BadRequest(new ApiResponse<CreatePortfolioResponse>
|
||||
{
|
||||
code = StatusCodes.BadRequest,
|
||||
data = null,
|
||||
message = $"不支持的组合币种: {request.currency},支持的币种: {string.Join(", ", CurrencyHelper.GetSupportedCurrencies())}"
|
||||
});
|
||||
}
|
||||
|
||||
// 3. 校验持仓币种
|
||||
if (request.stocks != null)
|
||||
{
|
||||
foreach (var stock in request.stocks)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(stock.currency) && !CurrencyHelper.IsSupported(stock.currency))
|
||||
{
|
||||
return BadRequest(new ApiResponse<CreatePortfolioResponse>
|
||||
{
|
||||
code = StatusCodes.BadRequest,
|
||||
data = null,
|
||||
message = $"不支持的持仓币种: {stock.currency}(标的: {stock.code}),支持的币种: {string.Join(", ", CurrencyHelper.GetSupportedCurrencies())}"
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var response = _portfolioService.CreatePortfolio(request, userId);
|
||||
|
||||
_logger.LogInformation("Portfolio created successfully");
|
||||
@ -302,6 +347,32 @@ public class PortfolioController : ControllerBase
|
||||
|
||||
_logger.LogInformation("Request to create transaction");
|
||||
|
||||
// 校验交易币种
|
||||
if (!string.IsNullOrEmpty(request.currency) && !CurrencyHelper.IsSupported(request.currency))
|
||||
{
|
||||
return BadRequest(new ApiResponse<CreateTransactionResponse>
|
||||
{
|
||||
code = StatusCodes.BadRequest,
|
||||
data = null,
|
||||
message = $"不支持的交易币种: {request.currency},支持的币种: {string.Join(", ", CurrencyHelper.GetSupportedCurrencies())}"
|
||||
});
|
||||
}
|
||||
|
||||
// 校验资产类型
|
||||
if (!string.IsNullOrEmpty(request.assetType))
|
||||
{
|
||||
var validAssetTypes = new[] { "Stock", "Crypto" };
|
||||
if (!validAssetTypes.Contains(request.assetType, StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
return BadRequest(new ApiResponse<CreateTransactionResponse>
|
||||
{
|
||||
code = StatusCodes.BadRequest,
|
||||
data = null,
|
||||
message = $"不支持的资产类型: {request.assetType},支持的类型: {string.Join(", ", validAssetTypes)}"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var response = _portfolioService.CreateTransaction(request, userId);
|
||||
|
||||
_logger.LogInformation("Transaction created successfully");
|
||||
|
||||
@ -68,7 +68,8 @@ public class UserController : ControllerBase
|
||||
MemberLevel = user.MemberLevel,
|
||||
RunningDays = user.RunningDays,
|
||||
Avatar = user.Avatar,
|
||||
Email = user.Email
|
||||
Email = user.Email,
|
||||
DefaultCurrency = user.DefaultCurrency
|
||||
};
|
||||
|
||||
_logger.LogInformation("User info retrieved successfully");
|
||||
@ -206,8 +207,22 @@ public class UserController : ControllerBase
|
||||
});
|
||||
}
|
||||
|
||||
// 校验币种
|
||||
if (!string.IsNullOrEmpty(request.DefaultCurrency) && !CurrencyHelper.IsSupported(request.DefaultCurrency))
|
||||
{
|
||||
return BadRequest(new ApiResponse<UpdateUserResponse>
|
||||
{
|
||||
code = StatusCodes.BadRequest,
|
||||
data = null,
|
||||
message = $"不支持的币种: {request.DefaultCurrency},支持的币种: {string.Join(", ", CurrencyHelper.GetSupportedCurrencies())}"
|
||||
});
|
||||
}
|
||||
|
||||
// 更新用户信息
|
||||
user.UserName = request.UserName;
|
||||
if (!string.IsNullOrEmpty(request.Avatar)) user.Avatar = request.Avatar;
|
||||
if (!string.IsNullOrEmpty(request.Email)) user.Email = request.Email;
|
||||
if (!string.IsNullOrEmpty(request.DefaultCurrency)) user.DefaultCurrency = request.DefaultCurrency;
|
||||
user.UpdatedAt = DateTime.Now;
|
||||
db.Updateable(user).ExecuteCommand();
|
||||
|
||||
|
||||
@ -44,6 +44,12 @@ public class User
|
||||
[SugarColumn(ColumnName = "member_level", Length = 50)]
|
||||
public string MemberLevel { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 用户默认本位币 (CNY/USD/HKD)
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "default_currency", Length = 10)]
|
||||
public string DefaultCurrency { get; set; } = "CNY";
|
||||
|
||||
/// <summary>
|
||||
/// 连续运行天数
|
||||
/// </summary>
|
||||
|
||||
49
AssetManager.Models/Currency.cs
Normal file
49
AssetManager.Models/Currency.cs
Normal file
@ -0,0 +1,49 @@
|
||||
namespace AssetManager.Models;
|
||||
|
||||
/// <summary>
|
||||
/// 支持的币种枚举
|
||||
/// </summary>
|
||||
public enum Currency
|
||||
{
|
||||
/// <summary>
|
||||
/// 人民币
|
||||
/// </summary>
|
||||
CNY = 0,
|
||||
|
||||
/// <summary>
|
||||
/// 美元
|
||||
/// </summary>
|
||||
USD = 1,
|
||||
|
||||
/// <summary>
|
||||
/// 港币
|
||||
/// </summary>
|
||||
HKD = 2
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 币种辅助类
|
||||
/// </summary>
|
||||
public static class CurrencyHelper
|
||||
{
|
||||
private static readonly HashSet<string> _supportedCurrencies = new(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
"CNY", "USD", "HKD"
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// 检查币种是否支持
|
||||
/// </summary>
|
||||
public static bool IsSupported(string currency)
|
||||
{
|
||||
return !string.IsNullOrEmpty(currency) && _supportedCurrencies.Contains(currency);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取支持的币种列表
|
||||
/// </summary>
|
||||
public static List<string> GetSupportedCurrencies()
|
||||
{
|
||||
return new List<string> { "CNY", "USD", "HKD" };
|
||||
}
|
||||
}
|
||||
@ -7,6 +7,15 @@ public class UserInfoResponse
|
||||
public int RunningDays { get; set; }
|
||||
public string Avatar { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string DefaultCurrency { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateUserRequest
|
||||
{
|
||||
public string UserName { get; set; }
|
||||
public string Avatar { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string DefaultCurrency { get; set; }
|
||||
}
|
||||
|
||||
public class UserStatsResponse
|
||||
|
||||
Loading…
Reference in New Issue
Block a user