✨
This commit is contained in:
95
StopShopping.Api/Routes/User.cs
Normal file
95
StopShopping.Api/Routes/User.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using StopShopping.Services;
|
||||
using StopShopping.Services.Extensions;
|
||||
using StopShopping.Services.Models.Req;
|
||||
using StopShopping.Services.Models.Resp;
|
||||
|
||||
namespace StopShopping.Api.Routes;
|
||||
|
||||
public static class User
|
||||
{
|
||||
public static RouteGroupBuilder MapUser(this RouteGroupBuilder routes)
|
||||
{
|
||||
routes.MapPost("/user/signup", SignUpAsync)
|
||||
.AllowAnonymous().WithTags(OpenApiTags.用户.ToString());
|
||||
|
||||
routes.MapPost("/user/signin", SignInAsync)
|
||||
.AllowAnonymous().WithTags(OpenApiTags.用户.ToString());
|
||||
|
||||
routes.MapPost("/user/changepassword", ChangePasswordAsync)
|
||||
.WithTags(OpenApiTags.用户.ToString());
|
||||
|
||||
routes.MapGet("/user/info", GetUserAsync)
|
||||
.WithTags(OpenApiTags.用户.ToString());
|
||||
|
||||
routes.MapPost("/user/edit", EditUserAsync)
|
||||
.WithTags(OpenApiTags.用户.ToString());
|
||||
|
||||
return routes;
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse> SignUpAsync(
|
||||
SignUpParams model,
|
||||
IUserService userService)
|
||||
{
|
||||
await userService.SignUpAsync(model);
|
||||
|
||||
return ApiResponse.Succed();
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse<SignInUser>> SignInAsync(
|
||||
SignInParams model,
|
||||
IUserService userService,
|
||||
HttpContext httpContext,
|
||||
IWebHostEnvironment env,
|
||||
IOptions<AppOptions> options)
|
||||
{
|
||||
var result = await userService.SignInAsync(model);
|
||||
var resp = new ApiResponse<SignInUser>
|
||||
{
|
||||
IsSucced = result.IsSucced,
|
||||
Data = result.User,
|
||||
Message = result.Message
|
||||
};
|
||||
if (result.IsSucced)
|
||||
{
|
||||
httpContext.Response.Cookies.AppendRefreshToken(
|
||||
env,
|
||||
options.Value,
|
||||
TimeSpan.FromSeconds(result.RefreshToken!.ExpiresIn),
|
||||
result.RefreshToken.Token!
|
||||
);
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse> ChangePasswordAsync(
|
||||
ChangePasswordParams model,
|
||||
IUserService userService,
|
||||
HttpContext httpContext,
|
||||
IAccessTokenService accessTokenService
|
||||
)
|
||||
{
|
||||
var resp = await userService.ChangePasswordAsync(model);
|
||||
|
||||
if (resp.IsSucced)
|
||||
await Common.SignOutAsync(httpContext, accessTokenService);
|
||||
|
||||
return resp;
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse<Services.Models.Resp.User>> GetUserAsync(
|
||||
IUserService userService
|
||||
)
|
||||
{
|
||||
return await userService.GetUserInfoAsync();
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse> EditUserAsync(
|
||||
EditUserParams model,
|
||||
IUserService userService
|
||||
)
|
||||
{
|
||||
return await userService.EditAsync(model);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user