✨
This commit is contained in:
33
StopShopping.Services/Extensions/AppOptions.cs
Normal file
33
StopShopping.Services/Extensions/AppOptions.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
namespace StopShopping.Services.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// 业务配置
|
||||
/// </summary>
|
||||
public record AppOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// .bjbj.me
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public string CookieDomain { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// 域名,http(s)://www.xxx.xx
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public string DomainPath { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// anti-forgery 请求头
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public string CSRFHeaderName { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// anti-forgery cookie's name
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public string CSRFCookieName { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// 跨域站点
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public string[] CorsAllowedOrigins { get; set; } = [];
|
||||
}
|
||||
49
StopShopping.Services/Extensions/EnumExtensions.cs
Normal file
49
StopShopping.Services/Extensions/EnumExtensions.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using StopShopping.Services.Models;
|
||||
|
||||
namespace StopShopping.Services.Extensions;
|
||||
|
||||
public static class EnumExtensions
|
||||
{
|
||||
public static string GetTargetDirectory(this UploadScences uploadScences)
|
||||
{
|
||||
return uploadScences switch
|
||||
{
|
||||
UploadScences.Avatar => "avatar",
|
||||
UploadScences.Product => "product",
|
||||
UploadScences.Category => "category",
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(uploadScences))
|
||||
};
|
||||
}
|
||||
|
||||
public static char GetValue(this UserRoles userRoles)
|
||||
{
|
||||
return userRoles switch
|
||||
{
|
||||
UserRoles.Seller => 's',
|
||||
UserRoles.Buyer => 'c',
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(userRoles))
|
||||
};
|
||||
}
|
||||
|
||||
public static UserRoles ToUserRoles(this char userRole)
|
||||
{
|
||||
return userRole switch
|
||||
{
|
||||
's' => UserRoles.Seller,
|
||||
'c' => UserRoles.Buyer,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(userRole), "valid: 'c','s'")
|
||||
};
|
||||
}
|
||||
|
||||
public static bool CanDelete(this RequestStatus requestStatus)
|
||||
{
|
||||
return requestStatus == RequestStatus.Publish
|
||||
|| requestStatus == RequestStatus.Replied;
|
||||
}
|
||||
|
||||
public static bool CanReply(this RequestStatus requestStatus)
|
||||
{
|
||||
return requestStatus == RequestStatus.Publish
|
||||
|| requestStatus == RequestStatus.Replied;
|
||||
}
|
||||
}
|
||||
11
StopShopping.Services/Extensions/ServiceException.cs
Normal file
11
StopShopping.Services/Extensions/ServiceException.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace System;
|
||||
|
||||
/// <summary>
|
||||
/// 业务异常
|
||||
/// </summary>
|
||||
public class ServiceException : ApplicationException
|
||||
{
|
||||
public ServiceException() : base() { }
|
||||
|
||||
public ServiceException(string? message) : base(message) { }
|
||||
}
|
||||
45
StopShopping.Services/Extensions/ServicesExtensions.cs
Normal file
45
StopShopping.Services/Extensions/ServicesExtensions.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using FileSignatures;
|
||||
using FileSignatures.Formats;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using StopShopping.EF;
|
||||
using StopShopping.Services;
|
||||
using StopShopping.Services.Extensions;
|
||||
using StopShopping.Services.Implementions;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
public static class ServicesExtensions
|
||||
{
|
||||
public static IServiceCollection AddServices(
|
||||
this IServiceCollection services,
|
||||
Action<DbContextOptionsBuilder> dbContextOptions,
|
||||
IConfiguration appOptions,
|
||||
IConfiguration openPlatformOptions)
|
||||
{
|
||||
services.AddDbContext<StopShoppingContext>(dbContextOptions);
|
||||
|
||||
services.Configure<AppOptions>(appOptions);
|
||||
|
||||
var imageFormats = FileFormatLocator.GetFormats().OfType<Image>();
|
||||
var imageInspector = new FileFormatInspector(imageFormats);
|
||||
services.AddSingleton<IFileFormatInspector>(imageInspector);
|
||||
|
||||
services.AddSingleton<ICipherService, CipherService>();
|
||||
services.AddSingleton<ISerialNoGenerator, NanoidSerialNoGenerator>();
|
||||
|
||||
services.AddScoped<IDistrictService, DistrictService>();
|
||||
services.AddScoped<IClaimsService, ClaimsService>();
|
||||
services.AddScoped<IFileService, FileService>();
|
||||
services.AddScoped<IAccessTokenService, AccessTokenService>();
|
||||
services.AddScoped<IUserService, UserService>();
|
||||
services.AddScoped<ICategoryService, CategoryService>();
|
||||
services.AddScoped<IProductService, ProductService>();
|
||||
services.AddScoped<IRequestService, RequestService>();
|
||||
services.AddScoped<IReplyService, ReplyService>();
|
||||
|
||||
services.AddOpenPlatformServices(openPlatformOptions);
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
14
StopShopping.Services/Extensions/SystemExtensions.cs
Normal file
14
StopShopping.Services/Extensions/SystemExtensions.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace System;
|
||||
|
||||
public static class SystemExtensions
|
||||
{
|
||||
public static string ToFormatted(this DateTime dateTime)
|
||||
{
|
||||
return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
|
||||
public static string ToFormatted(this DateOnly date)
|
||||
{
|
||||
return date.ToString("yyyy-MM-dd");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user