✨
This commit is contained in:
44
StopShopping.Api/Routes/Admin.cs
Normal file
44
StopShopping.Api/Routes/Admin.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
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 Admin
|
||||
{
|
||||
public static RouteGroupBuilder MapAdmin(this RouteGroupBuilder routes)
|
||||
{
|
||||
routes.MapPost("/admin/signin", SignInAsync)
|
||||
.AllowAnonymous().WithTags(OpenApiTags.管理员.ToString());
|
||||
|
||||
return routes;
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse<SignInAdmin>> SignInAsync(
|
||||
SignInParams model,
|
||||
IUserService userService,
|
||||
HttpContext httpContext,
|
||||
IWebHostEnvironment env,
|
||||
IOptions<AppOptions> options)
|
||||
{
|
||||
var result = await userService.SignInAdminAsync(model);
|
||||
var resp = new ApiResponse<SignInAdmin>
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
60
StopShopping.Api/Routes/Category.cs
Normal file
60
StopShopping.Api/Routes/Category.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using StopShopping.Services;
|
||||
using StopShopping.Services.Models.Req;
|
||||
using StopShopping.Services.Models.Resp;
|
||||
|
||||
namespace StopShopping.Api.Routes;
|
||||
|
||||
public static class Category
|
||||
{
|
||||
public static RouteGroupBuilder MapCategoryCommon(this RouteGroupBuilder routes)
|
||||
{
|
||||
routes.MapGet("/category/list", GetTree)
|
||||
.WithTags(OpenApiTags.分类.ToString())
|
||||
.AllowAnonymous();
|
||||
|
||||
return routes;
|
||||
}
|
||||
|
||||
public static RouteGroupBuilder MapCategory(this RouteGroupBuilder routes)
|
||||
{
|
||||
routes.MapPost("/category/edit", EditCategoryAsync)
|
||||
.WithTags(OpenApiTags.分类.ToString());
|
||||
|
||||
routes.MapPost("/category/resort", ResortCategoryAsync)
|
||||
.WithTags(OpenApiTags.分类.ToString());
|
||||
|
||||
routes.MapPost("/category/delete", DeleteCategoryAsync)
|
||||
.WithTags(OpenApiTags.分类.ToString());
|
||||
|
||||
return routes;
|
||||
}
|
||||
|
||||
private static ApiResponse<List<Services.Models.Resp.Category>> GetTree(
|
||||
ICategoryService categoryService
|
||||
)
|
||||
{
|
||||
return categoryService.GetCategoriesTree();
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse<Services.Models.Resp.Category>> EditCategoryAsync(
|
||||
EditCategoryParams model,
|
||||
ICategoryService categoryService)
|
||||
{
|
||||
return await categoryService.EditCategoryAsync(model);
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse> ResortCategoryAsync(
|
||||
ResortCategoryParams model,
|
||||
ICategoryService categoryService)
|
||||
{
|
||||
return await categoryService.ResortCategoryAsync(model);
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse> DeleteCategoryAsync(
|
||||
CategoryIdParams model,
|
||||
ICategoryService categoryService
|
||||
)
|
||||
{
|
||||
return await categoryService.DeleteCategoryAsync(model);
|
||||
}
|
||||
}
|
||||
87
StopShopping.Api/Routes/Common.cs
Normal file
87
StopShopping.Api/Routes/Common.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using Microsoft.AspNetCore.Antiforgery;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
using StopShopping.Services;
|
||||
using StopShopping.Services.Models.Req;
|
||||
using StopShopping.Services.Models.Resp;
|
||||
|
||||
namespace StopShopping.Api.Routes;
|
||||
|
||||
public static class Common
|
||||
{
|
||||
public static RouteGroupBuilder MapCommon(this RouteGroupBuilder routes)
|
||||
{
|
||||
routes.MapPost("/common/upload", UploadAsync)
|
||||
.WithTags(OpenApiTags.公用.ToString());
|
||||
|
||||
routes.MapPost("/common/refreshtoken", RefreshTokenAsync)
|
||||
.AllowAnonymous()
|
||||
.Produces<ApiResponse<AccessToken>>()
|
||||
.WithTags(OpenApiTags.公用.ToString());
|
||||
|
||||
routes.MapPost("/common/signout", SignOutAsync)
|
||||
.AllowAnonymous().WithTags(OpenApiTags.公用.ToString());
|
||||
|
||||
routes.MapPost("/common/antiforgery-token", AntiForgeryToken)
|
||||
.WithTags(OpenApiTags.公用.ToString());
|
||||
|
||||
return routes;
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse<FileUpload>> UploadAsync(
|
||||
[FromForm] UploadParams payload,
|
||||
IFileService fileService,
|
||||
HttpContext httpContext)
|
||||
{
|
||||
return await fileService.UploadFileAsync(payload);
|
||||
}
|
||||
|
||||
private static ApiResponse<AntiForgeryToken> AntiForgeryToken(
|
||||
HttpContext httpContext,
|
||||
IAntiforgery antiforgery)
|
||||
{
|
||||
var antiforgeryToken = antiforgery.GetAndStoreTokens(httpContext);
|
||||
|
||||
return new ApiResponse<AntiForgeryToken>(new AntiForgeryToken
|
||||
{
|
||||
Token = antiforgeryToken.RequestToken,
|
||||
HeaderName = antiforgeryToken.HeaderName
|
||||
});
|
||||
}
|
||||
private static async Task<IResult> RefreshTokenAsync(
|
||||
HttpContext httpContext,
|
||||
IAccessTokenService accessTokenService)
|
||||
{
|
||||
var refreshToken = httpContext.Request.Cookies[HttpExtensions.REFRESH_TOKEN_COOKIE_KEY];
|
||||
if (string.IsNullOrWhiteSpace(refreshToken))
|
||||
return Results.Unauthorized();
|
||||
|
||||
var accessToken = await accessTokenService.GenerateAccessTokenAsync(refreshToken);
|
||||
if (null == accessToken)
|
||||
return Results.Unauthorized();
|
||||
|
||||
return Results.Ok(new ApiResponse<AccessToken>(accessToken));
|
||||
}
|
||||
|
||||
public static async Task<ApiResponse> SignOutAsync(
|
||||
HttpContext httpContext,
|
||||
IAccessTokenService accessTokenService)
|
||||
{
|
||||
var accessTokenHeader = httpContext.Request.Headers[HeaderNames.Authorization];
|
||||
if (accessTokenHeader.Count != 0)
|
||||
{
|
||||
var accessToken = accessTokenHeader.First()!.Split(" ").Last();
|
||||
if (!string.IsNullOrWhiteSpace(accessToken))
|
||||
await accessTokenService.AddAccessTokenBlacklistAsync(accessToken);
|
||||
}
|
||||
|
||||
var refreshToken = httpContext.Request.Cookies[HttpExtensions.REFRESH_TOKEN_COOKIE_KEY];
|
||||
if (!string.IsNullOrWhiteSpace(refreshToken))
|
||||
{
|
||||
await accessTokenService.RevokeRefreshTokenAsync(refreshToken);
|
||||
httpContext.Response.Cookies.Delete(HttpExtensions.REFRESH_TOKEN_COOKIE_KEY);
|
||||
}
|
||||
|
||||
return ApiResponse.Succed();
|
||||
}
|
||||
}
|
||||
34
StopShopping.Api/Routes/District.cs
Normal file
34
StopShopping.Api/Routes/District.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using StopShopping.Services;
|
||||
using StopShopping.Services.Models.Req;
|
||||
using StopShopping.Services.Models.Resp;
|
||||
|
||||
namespace StopShopping.Api.Routes;
|
||||
|
||||
public static class District
|
||||
{
|
||||
public static RouteGroupBuilder MapDistrict(this RouteGroupBuilder routes)
|
||||
{
|
||||
routes.MapGet("/district/top3level", GetTop3LevelDistrictsAsync)
|
||||
.WithTags(OpenApiTags.地址.ToString());
|
||||
|
||||
routes.MapGet("/district/children", GetChildrenDistricts)
|
||||
.WithTags(OpenApiTags.地址.ToString());
|
||||
|
||||
return routes;
|
||||
}
|
||||
|
||||
private static ApiResponse<List<Services.Models.Resp.District>> GetChildrenDistricts(
|
||||
[AsParameters] DistrictParentIdParams model,
|
||||
IDistrictService districtService
|
||||
)
|
||||
{
|
||||
return districtService.GetChildren(model);
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse<List<Services.Models.Resp.District>>> GetTop3LevelDistrictsAsync(
|
||||
IDistrictService districtService
|
||||
)
|
||||
{
|
||||
return await districtService.GetTop3LevelDistrictsAsync();
|
||||
}
|
||||
}
|
||||
61
StopShopping.Api/Routes/Product.cs
Normal file
61
StopShopping.Api/Routes/Product.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using StopShopping.Services;
|
||||
using StopShopping.Services.Models.Req;
|
||||
using StopShopping.Services.Models.Resp;
|
||||
|
||||
namespace StopShopping.Api.Routes;
|
||||
|
||||
/// <summary>
|
||||
/// 商品相关路由
|
||||
/// </summary>
|
||||
public static class Product
|
||||
{
|
||||
public static RouteGroupBuilder MapProduct(this RouteGroupBuilder routes)
|
||||
{
|
||||
routes.MapGet("/product/list", SearchProductsAsync)
|
||||
.WithTags(OpenApiTags.商品.ToString());
|
||||
|
||||
routes.MapGet("/product/detail", Detail)
|
||||
.WithTags(OpenApiTags.商品.ToString());
|
||||
|
||||
routes.MapPost("/product/edit", EditAsync)
|
||||
.WithTags(OpenApiTags.商品.ToString());
|
||||
|
||||
routes.MapPost("/product/delete", DeleteAsync)
|
||||
.WithTags(OpenApiTags.商品.ToString());
|
||||
|
||||
return routes;
|
||||
}
|
||||
|
||||
private static async
|
||||
Task<ApiResponse<PagedResult<Services.Models.Resp.Product>>>
|
||||
SearchProductsAsync(
|
||||
[AsParameters] ProductSearchParms model,
|
||||
IProductService productService
|
||||
)
|
||||
{
|
||||
return await productService.SearchAsync(model);
|
||||
}
|
||||
|
||||
private static ApiResponse<ProductInfo> Detail(
|
||||
[AsParameters] ProductIdParams model,
|
||||
IProductService productService)
|
||||
{
|
||||
return productService.Detail(model);
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse> EditAsync(
|
||||
EditProductParams model,
|
||||
IProductService productService
|
||||
)
|
||||
{
|
||||
return await productService.EditAsync(model);
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse> DeleteAsync(
|
||||
ProductIdParams model,
|
||||
IProductService productService
|
||||
)
|
||||
{
|
||||
return await productService.DeleteAsync(model);
|
||||
}
|
||||
}
|
||||
45
StopShopping.Api/Routes/Reply.cs
Normal file
45
StopShopping.Api/Routes/Reply.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using StopShopping.Services;
|
||||
using StopShopping.Services.Models.Req;
|
||||
using StopShopping.Services.Models.Resp;
|
||||
|
||||
namespace StopShopping.Api.Routes;
|
||||
|
||||
public static class Reply
|
||||
{
|
||||
public static RouteGroupBuilder MapReply(this RouteGroupBuilder routes)
|
||||
{
|
||||
routes.MapPost("/reply/post", SubmitReplyAsync)
|
||||
.WithTags(OpenApiTags.竞标.ToString());
|
||||
|
||||
routes.MapGet("/reply/list", ListAsync)
|
||||
.WithTags(OpenApiTags.竞标.ToString());
|
||||
|
||||
routes.MapGet("/reply/orders", OrderSearchAsync)
|
||||
.WithTags(OpenApiTags.竞标.ToString());
|
||||
|
||||
return routes;
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse> SubmitReplyAsync(
|
||||
ReplyParams model,
|
||||
IReplyService replyService)
|
||||
{
|
||||
return await replyService.ReplyAsync(model);
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse<List<Services.Models.Resp.Reply>>> ListAsync(
|
||||
[AsParameters] RequestIdParams model,
|
||||
IReplyService replyService
|
||||
)
|
||||
{
|
||||
return await replyService.GetRepliesAsync(model);
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse<PagedResult<Services.Models.Resp.Request>>> OrderSearchAsync(
|
||||
[AsParameters] RequestSearchWithStatusParams model,
|
||||
IRequestService requestService
|
||||
)
|
||||
{
|
||||
return await requestService.RequestOrderSearchAsync(model);
|
||||
}
|
||||
}
|
||||
57
StopShopping.Api/Routes/Request.cs
Normal file
57
StopShopping.Api/Routes/Request.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using StopShopping.Services;
|
||||
using StopShopping.Services.Models.Req;
|
||||
using StopShopping.Services.Models.Resp;
|
||||
|
||||
namespace StopShopping.Api.Routes;
|
||||
|
||||
public static class Request
|
||||
{
|
||||
public static RouteGroupBuilder MapRequest(this RouteGroupBuilder routes)
|
||||
{
|
||||
routes.MapPost("/request/publish", PublishRequestAsync)
|
||||
.WithTags(OpenApiTags.需求.ToString());
|
||||
|
||||
routes.MapGet("/request/search", SearchAsync)
|
||||
.WithTags(OpenApiTags.需求.ToString())
|
||||
.AllowAnonymous();
|
||||
|
||||
routes.MapGet("/request/orders", OrderSearchAsync)
|
||||
.WithTags(OpenApiTags.需求.ToString());
|
||||
|
||||
routes.MapPost("/request/delete", DeleteRequestAsync)
|
||||
.WithTags(OpenApiTags.需求.ToString());
|
||||
|
||||
return routes;
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse> PublishRequestAsync(
|
||||
CreateRequestParams model,
|
||||
IRequestService requestService)
|
||||
{
|
||||
return await requestService.PublishRequestAsync(model);
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse<PagedResult<Services.Models.Resp.Request>>> SearchAsync(
|
||||
[AsParameters] RequestSearchParams model,
|
||||
IRequestService requestService
|
||||
)
|
||||
{
|
||||
return await requestService.SearchAsync(model);
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse<PagedResult<Services.Models.Resp.Request>>> OrderSearchAsync(
|
||||
[AsParameters] RequestSearchWithStatusParams model,
|
||||
IRequestService requestService
|
||||
)
|
||||
{
|
||||
return await requestService.RequestOrderSearchAsync(model);
|
||||
}
|
||||
|
||||
private static async Task<ApiResponse> DeleteRequestAsync(
|
||||
RequestIdParams model,
|
||||
IRequestService requestService
|
||||
)
|
||||
{
|
||||
return await requestService.DeleteRequestAsync(model);
|
||||
}
|
||||
}
|
||||
45
StopShopping.Api/Routes/Root.cs
Normal file
45
StopShopping.Api/Routes/Root.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
|
||||
using Scalar.AspNetCore;
|
||||
using StopShopping.Services.Models;
|
||||
|
||||
namespace StopShopping.Api.Routes;
|
||||
|
||||
/// <summary>
|
||||
/// 其他路由从RouteGroupBuilder扩展并添加到MapGroup之后
|
||||
/// </summary>
|
||||
public static class Root
|
||||
{
|
||||
public static void MapRoutes(WebApplication app)
|
||||
{
|
||||
app.MapGroup("")
|
||||
.MapUser()
|
||||
.MapProduct()
|
||||
.MapRequest()
|
||||
.MapReply()
|
||||
.MapDistrict()
|
||||
.WithDescription("用户端调用")
|
||||
.RequireAuthorization(policy => policy.RequireRole(SystemRoles.User.ToString()));
|
||||
app.MapGroup("")
|
||||
.MapCommon()
|
||||
.MapCategoryCommon()
|
||||
.WithDescription("公共调用")
|
||||
.RequireAuthorization();
|
||||
app.MapGroup("")
|
||||
.MapAdmin()
|
||||
.MapCategory()
|
||||
.WithDescription("管理端调用")
|
||||
.RequireAuthorization(policy => policy.RequireRole(SystemRoles.Admin.ToString()));
|
||||
}
|
||||
}
|
||||
|
||||
public enum OpenApiTags
|
||||
{
|
||||
用户,
|
||||
分类,
|
||||
商品,
|
||||
需求,
|
||||
竞标,
|
||||
地址,
|
||||
管理员,
|
||||
公用,
|
||||
}
|
||||
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