Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code cleanup #15

Merged
merged 4 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public async Task<IActionResult> UploadAsync([FromBody] UidAndItems uidAndItems)
public async Task<IActionResult> DeleteAsync([FromQuery(Name = "Uid")] string uid)
{
int count = await gachaLogService.DeleteGachaItemsAsync(this.GetUserId(), uid).ConfigureAwait(false);
return Response<int>.Success($"删除了 {count} 条记录", "ServerGachaLogServiceDeleteEntrySucceed", count);
return Response<int>.Success($"删除了 {count} 条记录", ServerKeys.ServerGachaLogServiceDeleteEntrySucceed, count);
}

private IActionResult GetGachaLogStatistics<T>(string name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public async Task<IActionResult> RegisterAsync([FromBody] PassportRequest reques

PassportResult result = await passportService.RegisterAsync(passport).ConfigureAwait(false);
return result.Success
? Response<string>.Success("注册成功", result.LocalizationKey!, result.Token)
: Model.Response.Response.Fail(ReturnCode.RegisterFail, result.Message);
? Response<string>.Success(result.Message, result.LocalizationKey!, result.Token)
: Model.Response.Response.Fail(ReturnCode.RegisterFail, result.Message, result.LocalizationKey!);
}

[Authorize]
Expand Down Expand Up @@ -124,7 +124,7 @@ public async Task<IActionResult> CancelRegistrationAsync([FromBody] PassportRequ

return result.Success
? Model.Response.Response.Success(result.Message, result.LocalizationKey!)
: Model.Response.Response.Fail(ReturnCode.CancelFail, result.Message);
: Model.Response.Response.Fail(ReturnCode.CancelFail, result.Message, result.LocalizationKey!);
}

[HttpPost("ResetPassword")]
Expand All @@ -146,7 +146,7 @@ public async Task<IActionResult> ResetPasswordAsync([FromBody] PassportRequest r
PassportResult result = await passportService.ResetPasswordAsync(passport).ConfigureAwait(false);

return result.Success
? Response<string>.Success("密码设置成功", result.LocalizationKey!, result.Token)
? Response<string>.Success(result.Message, result.LocalizationKey!, result.Token)
: Model.Response.Response.Fail(ReturnCode.RegisterFail, result.Message, result.LocalizationKey!);
}

Expand All @@ -162,7 +162,7 @@ public async Task<IActionResult> LoginAsync([FromBody] PassportRequest request)
PassportResult result = await passportService.LoginAsync(passport).ConfigureAwait(false);

return result.Success
? Response<string>.Success("登录成功", result.LocalizationKey!, result.Token)
? Response<string>.Success(result.Message, result.LocalizationKey!, result.Token)
: Model.Response.Response.Fail(ReturnCode.LoginFail, result.Message, result.LocalizationKey!);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public async Task<IActionResult> ProcessUploadRecordAsync([FromBody] SimpleRecor
RecordUploadResult.OkWithGachaLogExtented => Model.Response.Response.Success("数据提交成功,获赠 3 天祈愿记录上传服务时长", ServerKeys.ServerRecordUploadSuccessAndGachaLogServiceTimeExtended),
RecordUploadResult.OkWithGachaLogNoSuchUser => Model.Response.Response.Success("数据提交成功,但不存在该胡桃账号", ServerKeys.ServerRecordUploadSuccessButNoSuchUser),

RecordUploadResult.ComputingStatistics => Model.Response.Response.Fail(ReturnCode.ComputingStatistics, "正在计算统计数据", "ServerRecordComputingStatistics"),
RecordUploadResult.UidBanned => Model.Response.Response.Fail(ReturnCode.BannedUid, "Uid 已被数据库封禁", "ServerRecordBannedUid"),
RecordUploadResult.NotCurrentSchedule => Model.Response.Response.Fail(ReturnCode.NotCurrentSchedule, "非当前深渊数据", "ServerRecordNotCurrentSchedule"),
RecordUploadResult.InvalidData => Model.Response.Response.Fail(ReturnCode.InvalidUploadData, "无效的提交数据", "ServerRecordInvalidData"),
RecordUploadResult.ConcurrencyNotSupported => Model.Response.Response.Fail(ReturnCode.PreviousRequestNotCompleted, "该UID的请求尚在处理", "ServerRecordPreviousRequestNotCompleted"),
RecordUploadResult.ConcurrencyStateErrorAdd or RecordUploadResult.ConcurrencyStateErrorRemove => Model.Response.Response.Fail(ReturnCode.InternalStateException, "提交状态异常", "ServerRecordInternalException"),
RecordUploadResult.ComputingStatistics => Model.Response.Response.Fail(ReturnCode.ComputingStatistics, "正在计算统计数据", ServerKeys.ServerRecordComputingStatistics),
RecordUploadResult.UidBanned => Model.Response.Response.Fail(ReturnCode.BannedUid, "Uid 已被数据库封禁", ServerKeys.ServerRecordBannedUid),
RecordUploadResult.NotCurrentSchedule => Model.Response.Response.Fail(ReturnCode.NotCurrentSchedule, "非当前深渊数据", ServerKeys.ServerRecordNotCurrentSchedule),
RecordUploadResult.InvalidData => Model.Response.Response.Fail(ReturnCode.InvalidUploadData, "无效的提交数据", ServerKeys.ServerRecordInvalidData),
RecordUploadResult.ConcurrencyNotSupported => Model.Response.Response.Fail(ReturnCode.PreviousRequestNotCompleted, "该UID的请求尚在处理", ServerKeys.ServerRecordPreviousRequestNotCompleted),
RecordUploadResult.ConcurrencyStateErrorAdd or RecordUploadResult.ConcurrencyStateErrorRemove => Model.Response.Response.Fail(ReturnCode.InternalStateException, "提交状态异常", ServerKeys.ServerRecordInternalException),
_ => Model.Response.Response.Success("数据提交成功"),
};
}
Expand All @@ -61,12 +61,12 @@ public async Task<IActionResult> CheckUidUploadedAsync([FromQuery(Name = "Uid")]
{
if (recordService.IsStatisticsServiceWorking())
{
return Model.Response.Response.Fail(ReturnCode.ComputingStatistics, "正在计算统计数据", "ServerRecordComputingStatistics2");
return Model.Response.Response.Fail(ReturnCode.ComputingStatistics, "正在计算统计数据", ServerKeys.ServerRecordComputingStatistics2);
}

if (!int.TryParse(uid, out _) || uid.Length != 9)
{
return Model.Response.Response.Fail(ReturnCode.InvalidQueryString, $"{uid}不是合法的uid", "ServerRecordInvalidUid");
return Model.Response.Response.Fail(ReturnCode.InvalidQueryString, $"{uid}不是合法的uid", ServerKeys.ServerRecordInvalidUid);
}

bool uploaded = await recordService.HaveUidUploadedAsync(uid).ConfigureAwait(false);
Expand All @@ -78,12 +78,12 @@ public async Task<IActionResult> RankAsync([FromQuery(Name = "Uid")] string uid)
{
if (recordService.IsStatisticsServiceWorking())
{
return Model.Response.Response.Fail(ReturnCode.ComputingStatistics, "正在计算统计数据", "ServerRecordComputingStatistics2");
return Model.Response.Response.Fail(ReturnCode.ComputingStatistics, "正在计算统计数据", ServerKeys.ServerRecordComputingStatistics2);
}

if (!int.TryParse(uid, out _) || uid.Length != 9)
{
return Model.Response.Response.Fail(ReturnCode.InvalidQueryString, $"{uid}不是合法的uid", "ServerRecordInvalidUid");
return Model.Response.Response.Fail(ReturnCode.InvalidQueryString, $"{uid}不是合法的uid", ServerKeys.ServerRecordInvalidUid);
}

Rank rank = await rankService.RetriveRankAsync(uid).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Snap.Hutao.Server.Controller;

public static class ServerKeys
{
public const string ServerGachaLogServiceDeleteEntrySucceed = "ServerGachaLogServiceDeleteEntrySucceed";
public const string ServerGachaLogServiceInsufficientRecordSlot = "ServerGachaLogServiceInsufficientRecordSlot";
public const string ServerGachaLogServiceInsufficientTime = "ServerGachaLogServiceInsufficientTime";
public const string ServerGachaLogServiceInvalidGachaLogData = "ServerGachaLogServiceInvalidGachaLogData";
Expand All @@ -26,6 +27,14 @@ public static class ServerKeys
public const string ServerPassportVerifyRequestSuccess = "ServerPassportVerifyRequestSuccess";
public const string ServerPassportVerifyRequestNotCurrentUser = "ServerPassportVerifyRequestNotCurrentUser";
public const string ServerPassportVerifyRequestUserAlreadyExisted = "ServerPassportVerifyRequestUserAlreadyExisted";
public const string ServerRecordBannedUid = "ServerRecordBannedUid";
public const string ServerRecordComputingStatistics = "ServerRecordComputingStatistics";
public const string ServerRecordComputingStatistics2 = "ServerRecordComputingStatistics2";
public const string ServerRecordInternalException = "ServerRecordInternalException";
public const string ServerRecordInvalidData = "ServerRecordInvalidData";
public const string ServerRecordInvalidUid = "ServerRecordInvalidUid";
public const string ServerRecordNotCurrentSchedule = "ServerRecordNotCurrentSchedule";
public const string ServerRecordPreviousRequestNotCompleted = "ServerRecordPreviousRequestNotCompleted";
public const string ServerRecordUploadSuccessAndGachaLogServiceTimeExtended = "ServerRecordUploadSuccessAndGachaLogServiceTimeExtended";
public const string ServerRecordUploadSuccessButNoPassport = "ServerRecordUploadSuccessButNoPassport";
public const string ServerRecordUploadSuccessButNoSuchUser = "ServerRecordUploadSuccessButNoSuchUser";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

using Microsoft.AspNetCore.Authorization.Policy;
using Snap.Hutao.Server.Controller;
using Snap.Hutao.Server.Model.Response;

namespace Snap.Hutao.Server.Service.Authorization;
Expand All @@ -27,6 +28,6 @@ private static async Task ForbidAsync(HttpContext context)
context.Response.StatusCode = StatusCodes.Status200OK;
}

await context.Response.WriteAsJsonAsync(Response.Fail(ReturnCode.LoginFail, "请先登录或注册胡桃账号", "ServerPassportLoginRequired")).ConfigureAwait(false);
await context.Response.WriteAsJsonAsync(Response.Fail(ReturnCode.LoginFail, "请先登录或注册胡桃账号", ServerKeys.ServerPassportLoginRequired)).ConfigureAwait(false);
}
}