➕ feat(优化): 数据库连接优化;启动项目时如果不存在自动创建数据库;删除不必要内容;将数据库改为SqlServer
This commit is contained in:
commit
f00cd37feb
|
@ -1,15 +1,23 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||||
<data-source source="LOCAL" name="bunny" uuid="1a151f4f-7b33-40a5-b425-02b84edb60e6">
|
<data-source source="LOCAL" name="bunny" uuid="b6cf8a1e-027c-4a20-8eff-d18024c58281">
|
||||||
<driver-ref>sqlite.xerial</driver-ref>
|
<driver-ref>sqlite.xerial</driver-ref>
|
||||||
<synchronize>true</synchronize>
|
<synchronize>true</synchronize>
|
||||||
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
|
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
|
||||||
<jdbc-url>jdbc:sqlite:D:\MyFolder\Bunny\Bunny-cli\CSharp\CSharp-Single-EFCore\Bunny.WebApi\bunny.db</jdbc-url>
|
<jdbc-url>jdbc:sqlite:D:\Project\web\Bunny-Cli\template\CSharp\CSharp-Single-EFCore\Bunny.WebApi\Database\bunny.db</jdbc-url>
|
||||||
<jdbc-additional-properties>
|
<jdbc-additional-properties>
|
||||||
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
|
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
|
||||||
</jdbc-additional-properties>
|
</jdbc-additional-properties>
|
||||||
<working-dir>$ProjectFileDir$</working-dir>
|
<working-dir>$ProjectFileDir$</working-dir>
|
||||||
|
<libraries>
|
||||||
|
<library>
|
||||||
|
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.45.1/org/xerial/sqlite-jdbc/3.45.1.0/sqlite-jdbc-3.45.1.0.jar</url>
|
||||||
|
</library>
|
||||||
|
<library>
|
||||||
|
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.45.1/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar</url>
|
||||||
|
</library>
|
||||||
|
</libraries>
|
||||||
</data-source>
|
</data-source>
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
|
@ -57,11 +57,10 @@ public class AppSettings
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static T GetAppConfig<T>(string key, T? defaultValue = default)
|
public static T? GetAppConfig<T>(string key, T? defaultValue = default)
|
||||||
{
|
{
|
||||||
var setting = (T)Convert.ChangeType(Configuration[key], typeof(T));
|
return (T)Convert.ChangeType(Configuration[key], typeof(T))!;
|
||||||
var value = setting;
|
;
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -71,7 +70,7 @@ public class AppSettings
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string GetConfig(string key)
|
public static string GetConfig(string key)
|
||||||
{
|
{
|
||||||
return Configuration[key];
|
return Configuration[key]!;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -82,6 +81,6 @@ public class AppSettings
|
||||||
/// <returns>节点类型实例</returns>
|
/// <returns>节点类型实例</returns>
|
||||||
public static T Get<T>(string key)
|
public static T Get<T>(string key)
|
||||||
{
|
{
|
||||||
return Configuration.GetSection(key).Get<T>();
|
return Configuration.GetSection(key).Get<T>()!;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -10,14 +10,14 @@
|
||||||
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0"/>
|
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0"/>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.6"/>
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.6"/>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5"/>
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5"/>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.7">
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.7"/>
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8"/>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.7">
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.8">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Minio" Version="6.0.3"/>
|
<PackageReference Include="Minio" Version="6.0.3"/>
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
|
||||||
|
|
|
@ -1,104 +0,0 @@
|
||||||
using Bunny.Common.Context;
|
|
||||||
using Bunny.Dao.Entity.Base;
|
|
||||||
using Bunny.Dao.Entity.System;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
|
|
||||||
namespace Bunny.Common.Connect;
|
|
||||||
|
|
||||||
public class EfCoreContext : DbContext
|
|
||||||
{
|
|
||||||
public EfCoreContext()
|
|
||||||
{
|
|
||||||
// DbPath = @"D:\MyFolder\Bunny\Bunny-cli\Template\CSharp\CSharp-Single-EFCore\Bunny.WebApi\Database\bunny.db";
|
|
||||||
var dataBaseConnection = AppSettings.GetAppConfig<string>("DataBase:DataBaseConnection");
|
|
||||||
DbPath = dataBaseConnection;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string DbPath { get; }
|
|
||||||
|
|
||||||
public DbSet<Blog> Blogs { get; set; }
|
|
||||||
public DbSet<Post> Posts { get; set; }
|
|
||||||
public DbSet<Product> Products { get; set; }
|
|
||||||
public DbSet<User> Users { get; set; }
|
|
||||||
|
|
||||||
protected override void OnConfiguring(DbContextOptionsBuilder options)
|
|
||||||
{
|
|
||||||
options.UseSqlite($"Data Source={DbPath}");
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
||||||
{
|
|
||||||
// 添加过滤器
|
|
||||||
modelBuilder.Entity<BaseEntity>()
|
|
||||||
.HasQueryFilter(e => !e.IsDeleted);
|
|
||||||
|
|
||||||
// 默认值为false,表示未删除
|
|
||||||
modelBuilder.Entity<BaseEntity>()
|
|
||||||
.Property(e => e.IsDeleted)
|
|
||||||
.HasDefaultValue(false);
|
|
||||||
|
|
||||||
base.OnModelCreating(modelBuilder);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override int SaveChanges()
|
|
||||||
{
|
|
||||||
SetSystemField();
|
|
||||||
return base.SaveChanges();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override int SaveChanges(bool acceptAllChangesOnSuccess)
|
|
||||||
{
|
|
||||||
SetSystemField();
|
|
||||||
return base.SaveChanges(acceptAllChangesOnSuccess);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new())
|
|
||||||
{
|
|
||||||
SetSystemField();
|
|
||||||
return base.SaveChangesAsync(cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess,
|
|
||||||
CancellationToken cancellationToken = new())
|
|
||||||
{
|
|
||||||
SetSystemField();
|
|
||||||
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 系统字段赋值
|
|
||||||
/// </summary>
|
|
||||||
private void SetSystemField()
|
|
||||||
{
|
|
||||||
foreach (var item in ChangeTracker.Entries())
|
|
||||||
if (item.Entity is BaseEntity entity)
|
|
||||||
switch (item.State)
|
|
||||||
{
|
|
||||||
//添加操作
|
|
||||||
case EntityState.Added:
|
|
||||||
{
|
|
||||||
if (entity.Id == string.Empty) entity.Id = Guid.NewGuid().ToString();
|
|
||||||
entity.CreateTime = DateTime.Now;
|
|
||||||
entity.UpdateTime = DateTime.Now;
|
|
||||||
entity.CreateUserId = BaseContext.GetUserId()!.Value;
|
|
||||||
entity.UpdateUserId = BaseContext.GetUserId()!.Value;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
//修改操作
|
|
||||||
case EntityState.Modified:
|
|
||||||
entity.UpdateTime = DateTime.Now;
|
|
||||||
entity.UpdateUserId = BaseContext.GetUserId()!.Value;
|
|
||||||
break;
|
|
||||||
case EntityState.Detached:
|
|
||||||
break;
|
|
||||||
case EntityState.Unchanged:
|
|
||||||
break;
|
|
||||||
case EntityState.Deleted:
|
|
||||||
entity.UpdateTime = DateTime.Now;
|
|
||||||
entity.UpdateUserId = BaseContext.GetUserId()!.Value;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new ArgumentOutOfRangeException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -3,33 +3,33 @@
|
||||||
public class BaseContext
|
public class BaseContext
|
||||||
{
|
{
|
||||||
// 使用Lazy初始化确保线程安全
|
// 使用Lazy初始化确保线程安全
|
||||||
public static readonly ThreadLocal<long?> UserId = new();
|
public static readonly ThreadLocal<long?>? UserId = null;
|
||||||
public static readonly ThreadLocal<string?> Token = new();
|
public static readonly ThreadLocal<string?>? Token = null;
|
||||||
|
|
||||||
// 用户id相关
|
// 用户id相关
|
||||||
public static long? GetUserId()
|
public static long? GetUserId()
|
||||||
{
|
{
|
||||||
return UserId.Value;
|
return UserId?.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetUserId(long? userId)
|
public static void SetUserId(long? userId)
|
||||||
{
|
{
|
||||||
UserId.Value = userId;
|
UserId!.Value = userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string? GetToken()
|
public static string? GetToken()
|
||||||
{
|
{
|
||||||
return Token.Value;
|
return Token?.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SetToken(string token)
|
public static void SetToken(string token)
|
||||||
{
|
{
|
||||||
Token.Value = token;
|
Token!.Value = token;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void RemoveUser()
|
public static void RemoveUser()
|
||||||
{
|
{
|
||||||
Token.Value = null;
|
if (Token != null) Token.Value = null;
|
||||||
UserId.Value = null;
|
if (UserId != null) UserId.Value = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,123 @@
|
||||||
|
using Bunny.Common.Interceptor;
|
||||||
|
using Bunny.Dao.Entity.Base;
|
||||||
|
using Bunny.Dao.Entity.System;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Bunny.Common.Context;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// EfCore 连接内容
|
||||||
|
/// dotnet tool install --global dotnet-ef
|
||||||
|
/// dotnet add package Microsoft.EntityFrameworkCore.Design
|
||||||
|
/// dotnet ef migrations add InitialCreate
|
||||||
|
/// dotnet ef database update
|
||||||
|
/// </summary>
|
||||||
|
public class EfCoreContext : DbContext
|
||||||
|
{
|
||||||
|
public EfCoreContext()
|
||||||
|
{
|
||||||
|
// var dataBaseConnection = AppSettings.GetAppConfig<string>("DataBase:DataBaseConnection");
|
||||||
|
const string dataBaseConnection =
|
||||||
|
"192.168.3.98;Initial Catalog=BunnyDemo;TrustServerCertificate=True;Persist Security Info=True;User ID=sa;Password=abc1234.";
|
||||||
|
DbPath = $"Data Source={dataBaseConnection}";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 连接路径
|
||||||
|
private string DbPath { get; }
|
||||||
|
public DbSet<Blog> Blogs { get; set; }
|
||||||
|
public DbSet<Post> Posts { get; set; }
|
||||||
|
public DbSet<Product> Products { get; set; }
|
||||||
|
public DbSet<User> Users { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 配置数据库上下文选项,指定数据库连接字符串
|
||||||
|
/// </summary>
|
||||||
|
protected override void OnConfiguring(DbContextOptionsBuilder options)
|
||||||
|
{
|
||||||
|
options.UseSqlServer(DbPath).AddInterceptors(new TransactionInterceptor());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 在创建模型时,为所有继承自BaseEntity的实体类型添加软删除查询过滤器
|
||||||
|
/// </summary>
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
|
||||||
|
if (typeof(BaseEntity).IsAssignableFrom(entityType.ClrType))
|
||||||
|
entityType.AddSoftDeleteQueryFilter();
|
||||||
|
|
||||||
|
base.OnModelCreating(modelBuilder);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 重写SaveChanges方法,在保存更改之前设置系统字段
|
||||||
|
/// </summary>
|
||||||
|
public override int SaveChanges()
|
||||||
|
{
|
||||||
|
SetSystemField();
|
||||||
|
return base.SaveChanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 重写SaveChanges方法,在保存更改之前设置系统字段
|
||||||
|
/// </summary>
|
||||||
|
public override int SaveChanges(bool acceptAllChangesOnSuccess)
|
||||||
|
{
|
||||||
|
SetSystemField();
|
||||||
|
return base.SaveChanges(acceptAllChangesOnSuccess);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步保存更改,设置系统字段
|
||||||
|
/// </summary>
|
||||||
|
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new())
|
||||||
|
{
|
||||||
|
SetSystemField();
|
||||||
|
return base.SaveChangesAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步保存更改,设置系统字段
|
||||||
|
/// </summary>
|
||||||
|
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess,
|
||||||
|
CancellationToken cancellationToken = new())
|
||||||
|
{
|
||||||
|
SetSystemField();
|
||||||
|
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 系统字段赋值
|
||||||
|
/// </summary>
|
||||||
|
private void SetSystemField()
|
||||||
|
{
|
||||||
|
foreach (var item in ChangeTracker.Entries())
|
||||||
|
if (item.Entity is BaseEntity entity)
|
||||||
|
{
|
||||||
|
var userId = BaseContext.GetUserId();
|
||||||
|
switch (item.State)
|
||||||
|
{
|
||||||
|
// 添加操作
|
||||||
|
case EntityState.Added:
|
||||||
|
{
|
||||||
|
if (entity.Id == Guid.Empty) entity.Id = Guid.NewGuid();
|
||||||
|
entity.CreateTime = DateTime.Now;
|
||||||
|
entity.UpdateTime = DateTime.Now;
|
||||||
|
// 判断用户Id是否为空
|
||||||
|
if (userId != null)
|
||||||
|
{
|
||||||
|
entity.CreateUserId = userId!.Value;
|
||||||
|
entity.UpdateUserId = userId!.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// 修改操作
|
||||||
|
case EntityState.Modified:
|
||||||
|
entity.UpdateTime = DateTime.Now;
|
||||||
|
if (userId != null) entity.UpdateUserId = userId!.Value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,7 +2,7 @@
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
using Bunny.Dao.Model.Constant;
|
using Bunny.Dao.Model.Constant;
|
||||||
|
|
||||||
namespace Bunny.Common.Configuration;
|
namespace Bunny.Common.Context;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 自定义Json转换器,用于将DateTime类型转换为JSON格式
|
/// 自定义Json转换器,用于将DateTime类型转换为JSON格式
|
|
@ -1,7 +1,7 @@
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Minio;
|
using Minio;
|
||||||
|
|
||||||
namespace Bunny.Common.Connect;
|
namespace Bunny.Common.Context;
|
||||||
|
|
||||||
public static class MinioContext
|
public static class MinioContext
|
||||||
{
|
{
|
|
@ -1,11 +1,11 @@
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using StackExchange.Redis;
|
using StackExchange.Redis;
|
||||||
|
|
||||||
namespace Bunny.Common.Connect;
|
namespace Bunny.Common.Context;
|
||||||
|
|
||||||
public static class RedisContext
|
public static class RedisContext
|
||||||
{
|
{
|
||||||
public static IDatabase RedisDatabase;
|
public static IDatabase? RedisDatabase;
|
||||||
private static readonly EndPointCollection EndPointCollection = new();
|
private static readonly EndPointCollection EndPointCollection = new();
|
||||||
|
|
||||||
public static void AddRedisContext(this WebApplicationBuilder builder)
|
public static void AddRedisContext(this WebApplicationBuilder builder)
|
|
@ -0,0 +1,36 @@
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection;
|
||||||
|
using Bunny.Dao.Entity.Base;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata;
|
||||||
|
|
||||||
|
namespace Bunny.Common.Context;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 软删除过滤器
|
||||||
|
/// </summary>
|
||||||
|
public static class SoftDeleteQueryExtension
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 软删除过滤器
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="entityData"></param>
|
||||||
|
public static void AddSoftDeleteQueryFilter(this IMutableEntityType entityData)
|
||||||
|
{
|
||||||
|
var methodToCall = typeof(SoftDeleteQueryExtension)
|
||||||
|
.GetMethod(nameof(GetSoftDeleteFilter), BindingFlags.NonPublic | BindingFlags.Static)
|
||||||
|
?.MakeGenericMethod(entityData.ClrType);
|
||||||
|
var filter = methodToCall?.Invoke(null, []);
|
||||||
|
entityData.SetQueryFilter((LambdaExpression)filter!);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 软删除
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TEntity"></typeparam>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static LambdaExpression GetSoftDeleteFilter<TEntity>() where TEntity : BaseEntity
|
||||||
|
{
|
||||||
|
Expression<Func<TEntity, bool>> filter = x => !x.IsDeleted;
|
||||||
|
return filter;
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
using Bunny.Dao.Model.Result;
|
using Bunny.Dao.Model.Result;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Mvc.Filters;
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
|
|
||||||
namespace Bunny.Common.Filter;
|
namespace Bunny.Common.Filter;
|
||||||
|
|
||||||
|
@ -14,9 +15,10 @@ public class ValidateModelStateAttribute : ActionFilterAttribute
|
||||||
{
|
{
|
||||||
if (actionContext.ModelState.IsValid) return;
|
if (actionContext.ModelState.IsValid) return;
|
||||||
|
|
||||||
// 异常返回结果包装 也可以自定自定义返回类型
|
// 表单验证失败
|
||||||
var errorMessage = actionContext.ModelState.Values.First().Errors.First().ErrorMessage;
|
var errorMessage = actionContext.ModelState.Values.First().Errors.First().ErrorMessage;
|
||||||
var result = Result<string>.Error(HttpStatusCode.BadRequest, errorMessage);
|
var result =
|
||||||
|
Result<ModelStateDictionary>.Error(HttpStatusCode.BadRequest, actionContext.ModelState, errorMessage);
|
||||||
|
|
||||||
// 重新复写返回结果
|
// 重新复写返回结果
|
||||||
actionContext.Result = new ObjectResult(result);
|
actionContext.Result = new ObjectResult(result);
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
using Bunny.Common.Exception;
|
||||||
|
using Bunny.Dao.Model.Constant;
|
||||||
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||||
|
|
||||||
|
namespace Bunny.Common.Interceptor;
|
||||||
|
|
||||||
|
public class TransactionInterceptor : SaveChangesInterceptor
|
||||||
|
{
|
||||||
|
public override InterceptionResult<int> SavingChanges(DbContextEventData eventData, InterceptionResult<int> result)
|
||||||
|
{
|
||||||
|
if (eventData.Context!.Database.CurrentTransaction != null) return result;
|
||||||
|
using var transaction = eventData.Context.Database.BeginTransaction();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 调用基类方法继续保存更改
|
||||||
|
result = base.SavingChanges(eventData, result);
|
||||||
|
|
||||||
|
// 如果保存更改成功,则提交事务
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch (System.Exception)
|
||||||
|
{
|
||||||
|
// 如果保存更改失败,则回滚事务
|
||||||
|
transaction.Rollback();
|
||||||
|
throw new BunnyException(ExceptionConstant.DataBaseError);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,173 +0,0 @@
|
||||||
// <auto-generated />
|
|
||||||
using System;
|
|
||||||
using Bunny.Common.Connect;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
||||||
using Microsoft.EntityFrameworkCore.Migrations;
|
|
||||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|
||||||
|
|
||||||
#nullable disable
|
|
||||||
|
|
||||||
namespace Bunny.Common.Migrations
|
|
||||||
{
|
|
||||||
[DbContext(typeof(EfCoreContext))]
|
|
||||||
[Migration("20240809011221_InitialCreate")]
|
|
||||||
partial class InitialCreate
|
|
||||||
{
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
|
||||||
{
|
|
||||||
#pragma warning disable 612, 618
|
|
||||||
modelBuilder.HasAnnotation("ProductVersion", "8.0.7");
|
|
||||||
|
|
||||||
modelBuilder.Entity("Bunny.Dao.Models.System.Blog", b =>
|
|
||||||
{
|
|
||||||
b.Property<string>("Id")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<DateTime>("CreateTime")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<long>("CreateUserId")
|
|
||||||
.HasColumnType("INTEGER");
|
|
||||||
|
|
||||||
b.Property<DateTime>("UpdateTime")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<long>("UpdateUserId")
|
|
||||||
.HasColumnType("INTEGER");
|
|
||||||
|
|
||||||
b.Property<string>("Url")
|
|
||||||
.IsRequired()
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.HasKey("Id");
|
|
||||||
|
|
||||||
b.ToTable("Blogs");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("Bunny.Dao.Models.System.Post", b =>
|
|
||||||
{
|
|
||||||
b.Property<string>("Id")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<int>("BlogId")
|
|
||||||
.HasColumnType("INTEGER");
|
|
||||||
|
|
||||||
b.Property<string>("BlogId1")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<string>("Content")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<DateTime>("CreateTime")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<long>("CreateUserId")
|
|
||||||
.HasColumnType("INTEGER");
|
|
||||||
|
|
||||||
b.Property<int>("PostId")
|
|
||||||
.HasColumnType("INTEGER");
|
|
||||||
|
|
||||||
b.Property<string>("Title")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<DateTime>("UpdateTime")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<long>("UpdateUserId")
|
|
||||||
.HasColumnType("INTEGER");
|
|
||||||
|
|
||||||
b.HasKey("Id");
|
|
||||||
|
|
||||||
b.HasIndex("BlogId1");
|
|
||||||
|
|
||||||
b.ToTable("Posts");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("Bunny.Dao.Models.System.Product", b =>
|
|
||||||
{
|
|
||||||
b.Property<string>("Id")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<DateTime>("CreateTime")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<long>("CreateUserId")
|
|
||||||
.HasColumnType("INTEGER");
|
|
||||||
|
|
||||||
b.Property<string>("CustomerId")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<string>("CustomerName")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<string>("PackageType")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<DateTime>("UpdateTime")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<long>("UpdateUserId")
|
|
||||||
.HasColumnType("INTEGER");
|
|
||||||
|
|
||||||
b.HasKey("Id");
|
|
||||||
|
|
||||||
b.ToTable("Products");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("Bunny.Dao.Models.System.User", b =>
|
|
||||||
{
|
|
||||||
b.Property<string>("Id")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<string>("CompanyCode")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<DateTime>("CreateTime")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<long>("CreateUserId")
|
|
||||||
.HasColumnType("INTEGER");
|
|
||||||
|
|
||||||
b.Property<string>("DeptCode")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<string>("Email")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<string>("Password")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<string>("Phone")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<string>("QrCode")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<DateTime>("UpdateTime")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<long>("UpdateUserId")
|
|
||||||
.HasColumnType("INTEGER");
|
|
||||||
|
|
||||||
b.Property<string>("UserName")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.HasKey("Id");
|
|
||||||
|
|
||||||
b.ToTable("Users");
|
|
||||||
});
|
|
||||||
|
|
||||||
modelBuilder.Entity("Bunny.Dao.Models.System.Post", b =>
|
|
||||||
{
|
|
||||||
b.HasOne("Bunny.Dao.Models.System.Blog", "Blog")
|
|
||||||
.WithMany()
|
|
||||||
.HasForeignKey("BlogId1");
|
|
||||||
|
|
||||||
b.Navigation("Blog");
|
|
||||||
});
|
|
||||||
#pragma warning restore 612, 618
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,117 +0,0 @@
|
||||||
using System;
|
|
||||||
using Microsoft.EntityFrameworkCore.Migrations;
|
|
||||||
|
|
||||||
#nullable disable
|
|
||||||
|
|
||||||
namespace Bunny.Common.Migrations
|
|
||||||
{
|
|
||||||
/// <inheritdoc />
|
|
||||||
public partial class InitialCreate : Migration
|
|
||||||
{
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void Up(MigrationBuilder migrationBuilder)
|
|
||||||
{
|
|
||||||
migrationBuilder.CreateTable(
|
|
||||||
name: "Blogs",
|
|
||||||
columns: table => new
|
|
||||||
{
|
|
||||||
Id = table.Column<string>(type: "TEXT", nullable: false),
|
|
||||||
Url = table.Column<string>(type: "TEXT", nullable: false),
|
|
||||||
CreateTime = table.Column<DateTime>(type: "TEXT", nullable: false),
|
|
||||||
UpdateTime = table.Column<DateTime>(type: "TEXT", nullable: false),
|
|
||||||
CreateUserId = table.Column<long>(type: "INTEGER", nullable: false),
|
|
||||||
UpdateUserId = table.Column<long>(type: "INTEGER", nullable: false)
|
|
||||||
},
|
|
||||||
constraints: table =>
|
|
||||||
{
|
|
||||||
table.PrimaryKey("PK_Blogs", x => x.Id);
|
|
||||||
});
|
|
||||||
|
|
||||||
migrationBuilder.CreateTable(
|
|
||||||
name: "Products",
|
|
||||||
columns: table => new
|
|
||||||
{
|
|
||||||
Id = table.Column<string>(type: "TEXT", nullable: false),
|
|
||||||
CustomerId = table.Column<string>(type: "TEXT", nullable: true),
|
|
||||||
CustomerName = table.Column<string>(type: "TEXT", nullable: true),
|
|
||||||
PackageType = table.Column<string>(type: "TEXT", nullable: true),
|
|
||||||
CreateTime = table.Column<DateTime>(type: "TEXT", nullable: false),
|
|
||||||
UpdateTime = table.Column<DateTime>(type: "TEXT", nullable: false),
|
|
||||||
CreateUserId = table.Column<long>(type: "INTEGER", nullable: false),
|
|
||||||
UpdateUserId = table.Column<long>(type: "INTEGER", nullable: false)
|
|
||||||
},
|
|
||||||
constraints: table =>
|
|
||||||
{
|
|
||||||
table.PrimaryKey("PK_Products", x => x.Id);
|
|
||||||
});
|
|
||||||
|
|
||||||
migrationBuilder.CreateTable(
|
|
||||||
name: "Users",
|
|
||||||
columns: table => new
|
|
||||||
{
|
|
||||||
Id = table.Column<string>(type: "TEXT", nullable: false),
|
|
||||||
UserName = table.Column<string>(type: "TEXT", nullable: true),
|
|
||||||
Password = table.Column<string>(type: "TEXT", nullable: true),
|
|
||||||
Email = table.Column<string>(type: "TEXT", nullable: true),
|
|
||||||
Phone = table.Column<string>(type: "TEXT", nullable: true),
|
|
||||||
QrCode = table.Column<string>(type: "TEXT", nullable: true),
|
|
||||||
CompanyCode = table.Column<string>(type: "TEXT", nullable: true),
|
|
||||||
DeptCode = table.Column<string>(type: "TEXT", nullable: true),
|
|
||||||
CreateTime = table.Column<DateTime>(type: "TEXT", nullable: false),
|
|
||||||
UpdateTime = table.Column<DateTime>(type: "TEXT", nullable: false),
|
|
||||||
CreateUserId = table.Column<long>(type: "INTEGER", nullable: false),
|
|
||||||
UpdateUserId = table.Column<long>(type: "INTEGER", nullable: false)
|
|
||||||
},
|
|
||||||
constraints: table =>
|
|
||||||
{
|
|
||||||
table.PrimaryKey("PK_Users", x => x.Id);
|
|
||||||
});
|
|
||||||
|
|
||||||
migrationBuilder.CreateTable(
|
|
||||||
name: "Posts",
|
|
||||||
columns: table => new
|
|
||||||
{
|
|
||||||
Id = table.Column<string>(type: "TEXT", nullable: false),
|
|
||||||
PostId = table.Column<int>(type: "INTEGER", nullable: false),
|
|
||||||
Title = table.Column<string>(type: "TEXT", nullable: true),
|
|
||||||
Content = table.Column<string>(type: "TEXT", nullable: true),
|
|
||||||
BlogId = table.Column<int>(type: "INTEGER", nullable: false),
|
|
||||||
BlogId1 = table.Column<string>(type: "TEXT", nullable: true),
|
|
||||||
CreateTime = table.Column<DateTime>(type: "TEXT", nullable: false),
|
|
||||||
UpdateTime = table.Column<DateTime>(type: "TEXT", nullable: false),
|
|
||||||
CreateUserId = table.Column<long>(type: "INTEGER", nullable: false),
|
|
||||||
UpdateUserId = table.Column<long>(type: "INTEGER", nullable: false)
|
|
||||||
},
|
|
||||||
constraints: table =>
|
|
||||||
{
|
|
||||||
table.PrimaryKey("PK_Posts", x => x.Id);
|
|
||||||
table.ForeignKey(
|
|
||||||
name: "FK_Posts_Blogs_BlogId1",
|
|
||||||
column: x => x.BlogId1,
|
|
||||||
principalTable: "Blogs",
|
|
||||||
principalColumn: "Id");
|
|
||||||
});
|
|
||||||
|
|
||||||
migrationBuilder.CreateIndex(
|
|
||||||
name: "IX_Posts_BlogId1",
|
|
||||||
table: "Posts",
|
|
||||||
column: "BlogId1");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void Down(MigrationBuilder migrationBuilder)
|
|
||||||
{
|
|
||||||
migrationBuilder.DropTable(
|
|
||||||
name: "Posts");
|
|
||||||
|
|
||||||
migrationBuilder.DropTable(
|
|
||||||
name: "Products");
|
|
||||||
|
|
||||||
migrationBuilder.DropTable(
|
|
||||||
name: "Users");
|
|
||||||
|
|
||||||
migrationBuilder.DropTable(
|
|
||||||
name: "Blogs");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,186 @@
|
||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using Bunny.Common.Context;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Bunny.Common.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(EfCoreContext))]
|
||||||
|
[Migration("20240902054644_InitialCreate")]
|
||||||
|
partial class InitialCreate
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasAnnotation("ProductVersion", "8.0.8")
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||||
|
|
||||||
|
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity("Bunny.Dao.Entity.System.Blog", b =>
|
||||||
|
{
|
||||||
|
b.Property<string>("Id")
|
||||||
|
.HasColumnType("nvarchar(450)");
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<long>("CreateUserId")
|
||||||
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<DateTime>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<long>("UpdateUserId")
|
||||||
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
|
b.Property<string>("Url")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("varchar(600)")
|
||||||
|
.HasColumnName("BlogUrl");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Blog");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Bunny.Dao.Entity.System.Post", b =>
|
||||||
|
{
|
||||||
|
b.Property<string>("Id")
|
||||||
|
.HasColumnType("nvarchar(450)");
|
||||||
|
|
||||||
|
b.Property<string>("BlogId")
|
||||||
|
.HasColumnType("nvarchar(450)");
|
||||||
|
|
||||||
|
b.Property<string>("Content")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<long>("CreateUserId")
|
||||||
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Title")
|
||||||
|
.HasMaxLength(600)
|
||||||
|
.HasColumnType("nvarchar(600)");
|
||||||
|
|
||||||
|
b.Property<DateTime>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<long>("UpdateUserId")
|
||||||
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("BlogId");
|
||||||
|
|
||||||
|
b.ToTable("Posts");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Bunny.Dao.Entity.System.Product", b =>
|
||||||
|
{
|
||||||
|
b.Property<string>("Id")
|
||||||
|
.HasColumnType("nvarchar(450)");
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<long>("CreateUserId")
|
||||||
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
|
b.Property<string>("CustomerId")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("CustomerName")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("PackageType")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<DateTime>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<long>("UpdateUserId")
|
||||||
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Products");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Bunny.Dao.Entity.System.User", b =>
|
||||||
|
{
|
||||||
|
b.Property<string>("Id")
|
||||||
|
.HasColumnType("nvarchar(450)");
|
||||||
|
|
||||||
|
b.Property<string>("CompanyCode")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<long>("CreateUserId")
|
||||||
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
|
b.Property<string>("DeptCode")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("Email")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Password")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("Phone")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("QrCode")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<DateTime>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<long>("UpdateUserId")
|
||||||
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
|
b.Property<string>("UserName")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Users");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Bunny.Dao.Entity.System.Post", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("Bunny.Dao.Entity.System.Blog", "Blog")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("BlogId");
|
||||||
|
|
||||||
|
b.Navigation("Blog");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,119 @@
|
||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Bunny.Common.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class InitialCreate : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "Blog",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<string>(type: "nvarchar(450)", nullable: false),
|
||||||
|
BlogUrl = table.Column<string>(type: "varchar(600)", nullable: false),
|
||||||
|
CreateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||||
|
UpdateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||||
|
CreateUserId = table.Column<long>(type: "bigint", nullable: false),
|
||||||
|
UpdateUserId = table.Column<long>(type: "bigint", nullable: false),
|
||||||
|
IsDeleted = table.Column<bool>(type: "bit", nullable: false)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_Blog", x => x.Id);
|
||||||
|
});
|
||||||
|
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "Products",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<string>(type: "nvarchar(450)", nullable: false),
|
||||||
|
CustomerId = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
|
CustomerName = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
|
PackageType = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
|
CreateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||||
|
UpdateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||||
|
CreateUserId = table.Column<long>(type: "bigint", nullable: false),
|
||||||
|
UpdateUserId = table.Column<long>(type: "bigint", nullable: false),
|
||||||
|
IsDeleted = table.Column<bool>(type: "bit", nullable: false)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_Products", x => x.Id);
|
||||||
|
});
|
||||||
|
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "Users",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<string>(type: "nvarchar(450)", nullable: false),
|
||||||
|
UserName = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
|
Password = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
|
Email = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
|
Phone = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
|
QrCode = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
|
CompanyCode = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
|
DeptCode = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
|
CreateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||||
|
UpdateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||||
|
CreateUserId = table.Column<long>(type: "bigint", nullable: false),
|
||||||
|
UpdateUserId = table.Column<long>(type: "bigint", nullable: false),
|
||||||
|
IsDeleted = table.Column<bool>(type: "bit", nullable: false)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_Users", x => x.Id);
|
||||||
|
});
|
||||||
|
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "Posts",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<string>(type: "nvarchar(450)", nullable: false),
|
||||||
|
Title = table.Column<string>(type: "nvarchar(600)", maxLength: 600, nullable: true),
|
||||||
|
Content = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
|
BlogId = table.Column<string>(type: "nvarchar(450)", nullable: true),
|
||||||
|
CreateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||||
|
UpdateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||||
|
CreateUserId = table.Column<long>(type: "bigint", nullable: false),
|
||||||
|
UpdateUserId = table.Column<long>(type: "bigint", nullable: false),
|
||||||
|
IsDeleted = table.Column<bool>(type: "bit", nullable: false)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_Posts", x => x.Id);
|
||||||
|
table.ForeignKey(
|
||||||
|
name: "FK_Posts_Blog_BlogId",
|
||||||
|
column: x => x.BlogId,
|
||||||
|
principalTable: "Blog",
|
||||||
|
principalColumn: "Id");
|
||||||
|
});
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_Posts_BlogId",
|
||||||
|
table: "Posts",
|
||||||
|
column: "BlogId");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "Posts");
|
||||||
|
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "Products");
|
||||||
|
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "Users");
|
||||||
|
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "Blog");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,9 @@
|
||||||
// <auto-generated />
|
// <auto-generated />
|
||||||
using System;
|
using System;
|
||||||
using Bunny.Common.Connect;
|
using Bunny.Common.Context;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata;
|
||||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
|
||||||
#nullable disable
|
#nullable disable
|
||||||
|
@ -15,152 +16,164 @@ namespace Bunny.Common.Migrations
|
||||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
#pragma warning disable 612, 618
|
#pragma warning disable 612, 618
|
||||||
modelBuilder.HasAnnotation("ProductVersion", "8.0.7");
|
modelBuilder
|
||||||
|
.HasAnnotation("ProductVersion", "8.0.8")
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||||
|
|
||||||
modelBuilder.Entity("Bunny.Dao.Models.System.Blog", b =>
|
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity("Bunny.Dao.Entity.System.Blog", b =>
|
||||||
{
|
{
|
||||||
b.Property<string>("Id")
|
b.Property<string>("Id")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("nvarchar(450)");
|
||||||
|
|
||||||
b.Property<DateTime>("CreateTime")
|
b.Property<DateTime>("CreateTime")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
b.Property<long>("CreateUserId")
|
b.Property<long>("CreateUserId")
|
||||||
.HasColumnType("INTEGER");
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
b.Property<DateTime>("UpdateTime")
|
b.Property<DateTime>("UpdateTime")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
b.Property<long>("UpdateUserId")
|
b.Property<long>("UpdateUserId")
|
||||||
.HasColumnType("INTEGER");
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
b.Property<string>("Url")
|
b.Property<string>("Url")
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("varchar(600)")
|
||||||
|
.HasColumnName("BlogUrl");
|
||||||
|
|
||||||
b.HasKey("Id");
|
b.HasKey("Id");
|
||||||
|
|
||||||
b.ToTable("Blogs");
|
b.ToTable("Blog");
|
||||||
});
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("Bunny.Dao.Models.System.Post", b =>
|
modelBuilder.Entity("Bunny.Dao.Entity.System.Post", b =>
|
||||||
{
|
{
|
||||||
b.Property<string>("Id")
|
b.Property<string>("Id")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("nvarchar(450)");
|
||||||
|
|
||||||
b.Property<int>("BlogId")
|
b.Property<string>("BlogId")
|
||||||
.HasColumnType("INTEGER");
|
.HasColumnType("nvarchar(450)");
|
||||||
|
|
||||||
b.Property<string>("BlogId1")
|
|
||||||
.HasColumnType("TEXT");
|
|
||||||
|
|
||||||
b.Property<string>("Content")
|
b.Property<string>("Content")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
b.Property<DateTime>("CreateTime")
|
b.Property<DateTime>("CreateTime")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
b.Property<long>("CreateUserId")
|
b.Property<long>("CreateUserId")
|
||||||
.HasColumnType("INTEGER");
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
b.Property<int>("PostId")
|
b.Property<bool>("IsDeleted")
|
||||||
.HasColumnType("INTEGER");
|
.HasColumnType("bit");
|
||||||
|
|
||||||
b.Property<string>("Title")
|
b.Property<string>("Title")
|
||||||
.HasColumnType("TEXT");
|
.HasMaxLength(600)
|
||||||
|
.HasColumnType("nvarchar(600)");
|
||||||
|
|
||||||
b.Property<DateTime>("UpdateTime")
|
b.Property<DateTime>("UpdateTime")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
b.Property<long>("UpdateUserId")
|
b.Property<long>("UpdateUserId")
|
||||||
.HasColumnType("INTEGER");
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
b.HasKey("Id");
|
b.HasKey("Id");
|
||||||
|
|
||||||
b.HasIndex("BlogId1");
|
b.HasIndex("BlogId");
|
||||||
|
|
||||||
b.ToTable("Posts");
|
b.ToTable("Posts");
|
||||||
});
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("Bunny.Dao.Models.System.Product", b =>
|
modelBuilder.Entity("Bunny.Dao.Entity.System.Product", b =>
|
||||||
{
|
{
|
||||||
b.Property<string>("Id")
|
b.Property<string>("Id")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("nvarchar(450)");
|
||||||
|
|
||||||
b.Property<DateTime>("CreateTime")
|
b.Property<DateTime>("CreateTime")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
b.Property<long>("CreateUserId")
|
b.Property<long>("CreateUserId")
|
||||||
.HasColumnType("INTEGER");
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
b.Property<string>("CustomerId")
|
b.Property<string>("CustomerId")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
b.Property<string>("CustomerName")
|
b.Property<string>("CustomerName")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
b.Property<string>("PackageType")
|
b.Property<string>("PackageType")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
b.Property<DateTime>("UpdateTime")
|
b.Property<DateTime>("UpdateTime")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
b.Property<long>("UpdateUserId")
|
b.Property<long>("UpdateUserId")
|
||||||
.HasColumnType("INTEGER");
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
b.HasKey("Id");
|
b.HasKey("Id");
|
||||||
|
|
||||||
b.ToTable("Products");
|
b.ToTable("Products");
|
||||||
});
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("Bunny.Dao.Models.System.User", b =>
|
modelBuilder.Entity("Bunny.Dao.Entity.System.User", b =>
|
||||||
{
|
{
|
||||||
b.Property<string>("Id")
|
b.Property<string>("Id")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("nvarchar(450)");
|
||||||
|
|
||||||
b.Property<string>("CompanyCode")
|
b.Property<string>("CompanyCode")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
b.Property<DateTime>("CreateTime")
|
b.Property<DateTime>("CreateTime")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
b.Property<long>("CreateUserId")
|
b.Property<long>("CreateUserId")
|
||||||
.HasColumnType("INTEGER");
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
b.Property<string>("DeptCode")
|
b.Property<string>("DeptCode")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
b.Property<string>("Email")
|
b.Property<string>("Email")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
b.Property<string>("Password")
|
b.Property<string>("Password")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
b.Property<string>("Phone")
|
b.Property<string>("Phone")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
b.Property<string>("QrCode")
|
b.Property<string>("QrCode")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
b.Property<DateTime>("UpdateTime")
|
b.Property<DateTime>("UpdateTime")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
b.Property<long>("UpdateUserId")
|
b.Property<long>("UpdateUserId")
|
||||||
.HasColumnType("INTEGER");
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
b.Property<string>("UserName")
|
b.Property<string>("UserName")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
b.HasKey("Id");
|
b.HasKey("Id");
|
||||||
|
|
||||||
b.ToTable("Users");
|
b.ToTable("Users");
|
||||||
});
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("Bunny.Dao.Models.System.Post", b =>
|
modelBuilder.Entity("Bunny.Dao.Entity.System.Post", b =>
|
||||||
{
|
{
|
||||||
b.HasOne("Bunny.Dao.Models.System.Blog", "Blog")
|
b.HasOne("Bunny.Dao.Entity.System.Blog", "Blog")
|
||||||
.WithMany()
|
.WithMany()
|
||||||
.HasForeignKey("BlogId1");
|
.HasForeignKey("BlogId");
|
||||||
|
|
||||||
b.Navigation("Blog");
|
b.Navigation("Blog");
|
||||||
});
|
});
|
||||||
|
|
|
@ -31,9 +31,9 @@ public class EmailUtil
|
||||||
};
|
};
|
||||||
|
|
||||||
// 判断是否有抄送
|
// 判断是否有抄送
|
||||||
if (entity.CC != null)
|
if (entity.Cc != null)
|
||||||
{
|
{
|
||||||
var ccArray = entity.CC.Split(',');
|
var ccArray = entity.Cc.Split(',');
|
||||||
// 是抄送
|
// 是抄送
|
||||||
if (entity.IsBbc == false)
|
if (entity.IsBbc == false)
|
||||||
foreach (var ccAddress in ccArray)
|
foreach (var ccAddress in ccArray)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
using Bunny.Common.Connect;
|
using Bunny.Common.Context;
|
||||||
using Bunny.Common.Exception;
|
using Bunny.Common.Exception;
|
||||||
using Bunny.Dao.Model.Constant;
|
using Bunny.Dao.Model.Constant;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace Bunny.Dao.Dto.System;
|
||||||
|
|
||||||
|
public class BlogUpdateDto
|
||||||
|
{
|
||||||
|
[Required(ErrorMessage = "ID是必须的")] public Guid? Id { get; set; }
|
||||||
|
[Required(ErrorMessage = "Url是必填项")] public string? Url { get; init; }
|
||||||
|
public DateTime CreateTime { get; set; }
|
||||||
|
|
||||||
|
public DateTime UpdateTime { get; set; }
|
||||||
|
|
||||||
|
public long CreateUserId { get; set; }
|
||||||
|
|
||||||
|
public long UpdateUserId { get; set; }
|
||||||
|
|
||||||
|
public bool IsDeleted { get; set; }
|
||||||
|
|
||||||
|
public byte[]? Version { get; set; }
|
||||||
|
}
|
|
@ -1,8 +1,10 @@
|
||||||
namespace Bunny.Dao.Entity.Base;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace Bunny.Dao.Entity.Base;
|
||||||
|
|
||||||
public class BaseEntity
|
public class BaseEntity
|
||||||
{
|
{
|
||||||
public string? Id { get; set; }
|
[Key] public Guid? Id { get; set; }
|
||||||
|
|
||||||
public DateTime CreateTime { get; set; }
|
public DateTime CreateTime { get; set; }
|
||||||
|
|
||||||
|
@ -12,7 +14,7 @@ public class BaseEntity
|
||||||
|
|
||||||
public long UpdateUserId { get; set; }
|
public long UpdateUserId { get; set; }
|
||||||
|
|
||||||
public string? OperationMessage { get; set; }
|
|
||||||
|
|
||||||
public bool IsDeleted { get; set; }
|
public bool IsDeleted { get; set; }
|
||||||
|
|
||||||
|
[Timestamp] public byte[]? Version { get; set; }
|
||||||
}
|
}
|
|
@ -8,7 +8,7 @@ public abstract class EmailSendEntity
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// SMTP 服务器
|
/// SMTP 服务器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string SmtpService { get; set; }
|
public string? SmtpService { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// SMTP 服务器端口号
|
/// SMTP 服务器端口号
|
||||||
|
@ -18,32 +18,32 @@ public abstract class EmailSendEntity
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送者邮件
|
/// 发送者邮件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string SendEmail { get; set; }
|
public string? SendEmail { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送者密码
|
/// 发送者密码
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string SendEmailPassword { get; set; }
|
public string? SendEmailPassword { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 接受这邮件
|
/// 接受这邮件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string ReceiverEmail { get; set; }
|
public string? ReceiverEmail { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送邮件的主题
|
/// 发送邮件的主题
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Subject { get; set; }
|
public string? Subject { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送邮件的内容
|
/// 发送邮件的内容
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Body { get; set; }
|
public string? Body { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设置抄送
|
/// 设置抄送
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string? CC { get; set; }
|
public string? Cc { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 是否密送
|
/// 是否密送
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
using Bunny.Dao.Entity.Base;
|
using Bunny.Dao.Entity.Base;
|
||||||
|
|
||||||
namespace Bunny.Dao.Entity.System;
|
namespace Bunny.Dao.Entity.System;
|
||||||
|
|
||||||
|
[Table("Blog")]
|
||||||
public class Blog : BaseEntity
|
public class Blog : BaseEntity
|
||||||
{
|
{
|
||||||
[Required(ErrorMessage = "邮箱是必填项")] public string Url { get; set; }
|
[Required]
|
||||||
|
[Column("BlogUrl", TypeName = "varchar(600)")]
|
||||||
|
public string? Url { get; init; }
|
||||||
}
|
}
|
|
@ -1,13 +1,11 @@
|
||||||
using Bunny.Dao.Entity.Base;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using Bunny.Dao.Entity.Base;
|
||||||
|
|
||||||
namespace Bunny.Dao.Entity.System;
|
namespace Bunny.Dao.Entity.System;
|
||||||
|
|
||||||
public class Post : BaseEntity
|
public class Post : BaseEntity
|
||||||
{
|
{
|
||||||
public int PostId { get; set; }
|
[MaxLength(600)] public string? Title { get; init; }
|
||||||
public string? Title { get; set; }
|
public string? Content { get; init; }
|
||||||
public string? Content { get; set; }
|
public Blog? Blog { get; init; }
|
||||||
|
|
||||||
public int BlogId { get; set; }
|
|
||||||
public Blog? Blog { get; set; }
|
|
||||||
}
|
}
|
|
@ -14,6 +14,7 @@ public class ExceptionConstant
|
||||||
public const string AddDataIsEmptyException = "添加数据为空";
|
public const string AddDataIsEmptyException = "添加数据为空";
|
||||||
public const string DeleteIdIsNotEmptyException = "删除id不能为空";
|
public const string DeleteIdIsNotEmptyException = "删除id不能为空";
|
||||||
public const string ServerError = "服务器错误";
|
public const string ServerError = "服务器错误";
|
||||||
|
public const string DataBaseError = "数据库错误";
|
||||||
|
|
||||||
// 文章操作相关
|
// 文章操作相关
|
||||||
public const string DoLikeCommentNotExist = "点赞内容不存在";
|
public const string DoLikeCommentNotExist = "点赞内容不存在";
|
||||||
|
|
|
@ -168,7 +168,7 @@ public class Result<T>(HttpStatusCode code, T? data, string? message)
|
||||||
/// <param name="message">消息内容</param>
|
/// <param name="message">消息内容</param>
|
||||||
/// <param name="data">数据内容分</param>
|
/// <param name="data">数据内容分</param>
|
||||||
/// <returns>返回的数据内容</returns>
|
/// <returns>返回的数据内容</returns>
|
||||||
public static Result<T> Error(string message, T data)
|
public static Result<T> Error(T data, string message)
|
||||||
{
|
{
|
||||||
return new Result<T>(HttpStatusCode.InternalServerError, data, message);
|
return new Result<T>(HttpStatusCode.InternalServerError, data, message);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using Bunny.Dao.Entity.System;
|
using Bunny.Dao.Dto.System;
|
||||||
|
using Bunny.Dao.Entity.System;
|
||||||
using Bunny.Dao.Model.Result;
|
using Bunny.Dao.Model.Result;
|
||||||
|
|
||||||
namespace Bunny.Service.IService;
|
namespace Bunny.Service.IService;
|
||||||
|
@ -21,7 +22,7 @@ public interface IBlogService
|
||||||
/// 更新Blog内容
|
/// 更新Blog内容
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dto"></param>
|
/// <param name="dto"></param>
|
||||||
void UpdateBlog(Blog dto);
|
void UpdateBlog(BlogUpdateDto dto);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 删除BLog
|
/// 删除BLog
|
||||||
|
|
|
@ -1,17 +1,15 @@
|
||||||
using Bunny.Common.Connect;
|
using Bunny.Common.Context;
|
||||||
|
using Bunny.Common.Utils.Net;
|
||||||
|
using Bunny.Dao.Dto.System;
|
||||||
using Bunny.Dao.Entity.System;
|
using Bunny.Dao.Entity.System;
|
||||||
using Bunny.Dao.Model.Result;
|
using Bunny.Dao.Model.Result;
|
||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
|
|
||||||
namespace Bunny.Service.IService.Service;
|
namespace Bunny.Service.IService.Service;
|
||||||
|
|
||||||
public class BlogService : IBlogService
|
public class BlogService : IBlogService
|
||||||
{
|
{
|
||||||
private readonly EfCoreContext _dbContext;
|
[Inject] public required EfCoreContext DbContext { get; set; }
|
||||||
|
|
||||||
public BlogService(EfCoreContext dbContext)
|
|
||||||
{
|
|
||||||
_dbContext = dbContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 添加Blog
|
/// 添加Blog
|
||||||
|
@ -20,8 +18,8 @@ public class BlogService : IBlogService
|
||||||
/// <exception cref="NotImplementedException"></exception>
|
/// <exception cref="NotImplementedException"></exception>
|
||||||
public void AddBlog(Blog dto)
|
public void AddBlog(Blog dto)
|
||||||
{
|
{
|
||||||
_dbContext.Add(dto);
|
DbContext.Add(dto);
|
||||||
_dbContext.SaveChanges();
|
DbContext.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -30,17 +28,20 @@ public class BlogService : IBlogService
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public List<Blog> QueryBlog()
|
public List<Blog> QueryBlog()
|
||||||
{
|
{
|
||||||
return _dbContext.Blogs.ToList();
|
return DbContext.Blogs.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 更新Blog内容
|
/// 更新Blog内容
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dto"></param>
|
/// <param name="dto"></param>
|
||||||
public void UpdateBlog(Blog dto)
|
public void UpdateBlog(BlogUpdateDto dto)
|
||||||
{
|
{
|
||||||
_dbContext.Blogs.Update(dto);
|
var blog = new Blog();
|
||||||
_dbContext.SaveChanges();
|
NetUtil.CopyProperties(dto, blog);
|
||||||
|
|
||||||
|
DbContext.Blogs.Update(blog);
|
||||||
|
DbContext.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -49,9 +50,9 @@ public class BlogService : IBlogService
|
||||||
/// <param name="id"></param>
|
/// <param name="id"></param>
|
||||||
public void DeleteBlog(string id)
|
public void DeleteBlog(string id)
|
||||||
{
|
{
|
||||||
var blog = new Blog { Id = id };
|
var blog = new Blog { Id = Guid.NewGuid() };
|
||||||
_dbContext.Blogs.Remove(blog);
|
DbContext.Blogs.Remove(blog);
|
||||||
_dbContext.SaveChanges();
|
DbContext.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -64,7 +65,6 @@ public class BlogService : IBlogService
|
||||||
for (var i = 0; i <= 100000; i++)
|
for (var i = 0; i <= 100000; i++)
|
||||||
list.Add(new Blog
|
list.Add(new Blog
|
||||||
{
|
{
|
||||||
Id = $"{i}",
|
|
||||||
Url = url,
|
Url = url,
|
||||||
CreateTime = DateTime.Now,
|
CreateTime = DateTime.Now,
|
||||||
UpdateTime = DateTime.Now,
|
UpdateTime = DateTime.Now,
|
||||||
|
@ -72,8 +72,8 @@ public class BlogService : IBlogService
|
||||||
CreateUserId = Random.Shared.NextInt64()
|
CreateUserId = Random.Shared.NextInt64()
|
||||||
});
|
});
|
||||||
|
|
||||||
_dbContext.Blogs.AddRange(list);
|
DbContext.Blogs.AddRange(list);
|
||||||
_dbContext.SaveChanges();
|
DbContext.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -85,12 +85,12 @@ public class BlogService : IBlogService
|
||||||
var list = new List<Blog>();
|
var list = new List<Blog>();
|
||||||
for (var i = 0; i < 10; i++)
|
for (var i = 0; i < 10; i++)
|
||||||
{
|
{
|
||||||
var blog = new Blog { Id = $"{i}" };
|
var blog = new Blog();
|
||||||
list.Add(blog);
|
list.Add(blog);
|
||||||
}
|
}
|
||||||
|
|
||||||
_dbContext.Blogs.RemoveRange(list);
|
DbContext.Blogs.RemoveRange(list);
|
||||||
_dbContext.SaveChanges();
|
DbContext.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -99,19 +99,19 @@ public class BlogService : IBlogService
|
||||||
public void UseTransaction()
|
public void UseTransaction()
|
||||||
{
|
{
|
||||||
// 还可以使用异步的
|
// 还可以使用异步的
|
||||||
var transaction = _dbContext.Database.BeginTransaction();
|
var transaction = DbContext.Database.BeginTransaction();
|
||||||
|
|
||||||
// 执行批量更新操作
|
// 执行批量更新操作
|
||||||
var list = new List<Blog>();
|
var list = new List<Blog>();
|
||||||
for (var i = 0; i < 10; i++)
|
for (var i = 0; i < 10; i++)
|
||||||
{
|
{
|
||||||
var blog = new Blog { Id = $"{i}", Url = "https://learn.microsoft.com/zh-cn/ef/core/saving/transactions" };
|
var blog = new Blog { Url = "https://learn.microsoft.com/zh-cn/ef/core/saving/transactions" };
|
||||||
list.Add(blog);
|
list.Add(blog);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新内容
|
// 更新内容
|
||||||
_dbContext.Blogs.UpdateRange(list);
|
DbContext.Blogs.UpdateRange(list);
|
||||||
_dbContext.SaveChanges();
|
DbContext.SaveChanges();
|
||||||
|
|
||||||
// 还可以使用异步方式
|
// 还可以使用异步方式
|
||||||
transaction.Commit();
|
transaction.Commit();
|
||||||
|
@ -124,7 +124,7 @@ public class BlogService : IBlogService
|
||||||
/// <param name="limit"></param>
|
/// <param name="limit"></param>
|
||||||
public PageResult<Blog> QueryPage(int page, int limit)
|
public PageResult<Blog> QueryPage(int page, int limit)
|
||||||
{
|
{
|
||||||
var items = _dbContext.Blogs.ToList();
|
var items = DbContext.Blogs.Take(limit).Skip(page).ToList();
|
||||||
var total = items.Count;
|
var total = items.Count;
|
||||||
var pages = (int)Math.Ceiling(total / (double)limit);
|
var pages = (int)Math.Ceiling(total / (double)limit);
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,6 @@ public class JobService : IJobService
|
||||||
public void StartSimpleJob()
|
public void StartSimpleJob()
|
||||||
{
|
{
|
||||||
var schedulerFactory = new StdSchedulerFactory();
|
var schedulerFactory = new StdSchedulerFactory();
|
||||||
// TODO 如果使用异步,必须使用await 和 async
|
|
||||||
// var scheduler = schedulerFactory.GetScheduler();
|
// var scheduler = schedulerFactory.GetScheduler();
|
||||||
var scheduler = schedulerFactory.GetScheduler().GetAwaiter().GetResult();
|
var scheduler = schedulerFactory.GetScheduler().GetAwaiter().GetResult();
|
||||||
|
|
||||||
|
@ -32,10 +31,6 @@ public class JobService : IJobService
|
||||||
// 使用同步方法
|
// 使用同步方法
|
||||||
scheduler.ScheduleJob(jobDetail, trigger).GetAwaiter().GetResult();
|
scheduler.ScheduleJob(jobDetail, trigger).GetAwaiter().GetResult();
|
||||||
scheduler.Start().GetAwaiter().GetResult();
|
scheduler.Start().GetAwaiter().GetResult();
|
||||||
|
|
||||||
// TODO 使用异步
|
|
||||||
// await _scheduler.ScheduleJob(jobDetail, trigger);
|
|
||||||
// await _scheduler.Start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -49,8 +44,8 @@ public class JobService : IJobService
|
||||||
var jobDetail = JobBuilder.Create<SimpleJob>()
|
var jobDetail = JobBuilder.Create<SimpleJob>()
|
||||||
.UsingJobData("username", "用户名")
|
.UsingJobData("username", "用户名")
|
||||||
.UsingJobData("password", "密码")
|
.UsingJobData("password", "密码")
|
||||||
// TODO 如果不设置持久的会报错;
|
// 如果不设置持久的会报错;
|
||||||
// TODO Jobs added with no trigger must be durable.Quartz.SchedulerException: Jobs added with no trigger must be durable.
|
// Jobs added with no trigger must be durable.Quartz.SchedulerException: Jobs added with no trigger must be durable.
|
||||||
.StoreDurably() // 设置作业为持久的
|
.StoreDurably() // 设置作业为持久的
|
||||||
.WithIdentity("simpleJob", "简单的JOB")
|
.WithIdentity("simpleJob", "简单的JOB")
|
||||||
.Build();
|
.Build();
|
||||||
|
@ -63,9 +58,9 @@ public class JobService : IJobService
|
||||||
.UsingJobData("trigger", "trigger值")
|
.UsingJobData("trigger", "trigger值")
|
||||||
.WithIdentity("testTrigger", "测试发出器")
|
.WithIdentity("testTrigger", "测试发出器")
|
||||||
.StartNow()
|
.StartNow()
|
||||||
// TODO 设置调度时间单位
|
// 设置调度时间单位
|
||||||
// .WithSimpleSchedule(x => x.WithIntervalInSeconds(1).WithRepeatCount(10))
|
// .WithSimpleSchedule(x => x.WithIntervalInSeconds(1).WithRepeatCount(10))
|
||||||
// TODO 自定义调度时间单位
|
// 自定义调度时间单位
|
||||||
.WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromMinutes(1)).WithRepeatCount(10))
|
.WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromMinutes(1)).WithRepeatCount(10))
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
|
@ -95,9 +90,9 @@ public class JobService : IJobService
|
||||||
.UsingJobData("trigger", "trigger值")
|
.UsingJobData("trigger", "trigger值")
|
||||||
.WithIdentity("testTrigger", "测试发出器")
|
.WithIdentity("testTrigger", "测试发出器")
|
||||||
.StartNow()
|
.StartNow()
|
||||||
// TODO StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(20, 0)):触发器从每天的20:00(晚上8点)开始。
|
// StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(20, 0)):触发器从每天的20:00(晚上8点)开始。
|
||||||
// TODO EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(22, 0)):触发器在每天的22:00(晚上10点)结束。
|
// EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(22, 0)):触发器在每天的22:00(晚上10点)结束。
|
||||||
// TODO WithIntervalInSeconds(2):在开始和结束时间之间,触发器将每2秒触发一次作业。
|
// WithIntervalInSeconds(2):在开始和结束时间之间,触发器将每2秒触发一次作业。
|
||||||
.WithDailyTimeIntervalSchedule(x => x.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(16, 0))
|
.WithDailyTimeIntervalSchedule(x => x.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(16, 0))
|
||||||
.EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(22, 0))
|
.EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(22, 0))
|
||||||
// 设置工作时间在每周的星期几
|
// 设置工作时间在每周的星期几
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
using Bunny.Common;
|
using Bunny.Common;
|
||||||
using Bunny.Common.Connect;
|
using Bunny.Common.Context;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Minio;
|
using Minio;
|
||||||
using Minio.DataModel;
|
using Minio.DataModel;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
using Bunny.Common.Connect;
|
using Bunny.Common.Context;
|
||||||
using Bunny.Dao.Entity.System;
|
using Bunny.Dao.Entity.System;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using StackExchange.Redis;
|
using StackExchange.Redis;
|
||||||
|
@ -7,7 +7,7 @@ namespace Bunny.Service.IService.Service;
|
||||||
|
|
||||||
public class RedisOptionService : IRedisOptionService
|
public class RedisOptionService : IRedisOptionService
|
||||||
{
|
{
|
||||||
private readonly IDatabase _redisDatabase = RedisContext.RedisDatabase;
|
private readonly IDatabase _redisDatabase = RedisContext.RedisDatabase!;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -60,7 +60,7 @@ public class RedisOptionService : IRedisOptionService
|
||||||
{
|
{
|
||||||
var post = new Post
|
var post = new Post
|
||||||
{
|
{
|
||||||
PostId = 1,
|
Id = Guid.NewGuid(),
|
||||||
Title = "存入JSON内容",
|
Title = "存入JSON内容",
|
||||||
Content = "正在存入JSON内容"
|
Content = "正在存入JSON内容"
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,7 +6,7 @@ public static class WebSocketTest
|
||||||
{
|
{
|
||||||
public static void Start()
|
public static void Start()
|
||||||
{
|
{
|
||||||
var webSocketServer = new WebSocketServer("ws://0.0.0.0:8000");
|
var webSocketServer = new WebSocketServer("ws://0.0.0.0:8800");
|
||||||
|
|
||||||
webSocketServer.RestartAfterListenError = true;
|
webSocketServer.RestartAfterListenError = true;
|
||||||
webSocketServer.Start(socket =>
|
webSocketServer.Start(socket =>
|
||||||
|
|
|
@ -1,27 +0,0 @@
|
||||||
using Bunny.Common.Connect;
|
|
||||||
using Bunny.Dao.Entity.System;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace Bunny.Test.Until.EfCoreTest;
|
|
||||||
|
|
||||||
public class EfCoreTest
|
|
||||||
{
|
|
||||||
[Test]
|
|
||||||
public void TestDbConnection()
|
|
||||||
{
|
|
||||||
using var db = new EfCoreContext();
|
|
||||||
Console.WriteLine($"Database path: {db.DbPath}.");
|
|
||||||
|
|
||||||
// Create
|
|
||||||
Console.WriteLine("Inserting a new blog");
|
|
||||||
db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
|
|
||||||
db.SaveChanges();
|
|
||||||
|
|
||||||
// Read
|
|
||||||
Console.WriteLine("Querying for a blog");
|
|
||||||
var blog = db.Blogs
|
|
||||||
.OrderBy(b => b.Id)
|
|
||||||
.First();
|
|
||||||
Console.WriteLine(blog.Url);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -27,6 +27,8 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Autofac" Version="8.0.0"/>
|
||||||
|
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="9.0.0"/>
|
||||||
<PackageReference Include="IGeekFan.AspNetCore.Knife4jUI" Version="0.0.16"/>
|
<PackageReference Include="IGeekFan.AspNetCore.Knife4jUI" Version="0.0.16"/>
|
||||||
<PackageReference Include="log4net" Version="2.0.17"/>
|
<PackageReference Include="log4net" Version="2.0.17"/>
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="8.0.0"/>
|
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="8.0.0"/>
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
using Autofac;
|
||||||
|
using Bunny.Common.Context;
|
||||||
|
using Bunny.Service.IService;
|
||||||
|
using Bunny.Service.IService.Service;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace Bunny.WebApi.Configuration;
|
||||||
|
|
||||||
|
public static class AddAutofacConfig
|
||||||
|
{
|
||||||
|
public static void BuildContainer(ContainerBuilder builder)
|
||||||
|
{
|
||||||
|
// 异步方式创建数据库
|
||||||
|
new EfCoreContext().Database.EnsureCreatedAsync();
|
||||||
|
|
||||||
|
// 如果不在在 Controller 层写构造函数可以打开这个,自动完成注入
|
||||||
|
var controllerBaseType = typeof(ControllerBase);
|
||||||
|
builder.RegisterAssemblyTypes(typeof(Program).Assembly)
|
||||||
|
.Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
|
||||||
|
.PropertiesAutowired(); //支持属性注入
|
||||||
|
|
||||||
|
// 注入EfCore上下文对象
|
||||||
|
builder.RegisterType<EfCoreContext>();
|
||||||
|
|
||||||
|
// 注入Service服务
|
||||||
|
builder.RegisterType<BaseService>().As<IBaseService>();
|
||||||
|
builder.RegisterType<BlogService>().As<IBlogService>();
|
||||||
|
builder.RegisterType<RedisOptionService>().As<IRedisOptionService>();
|
||||||
|
builder.RegisterType<JobService>().As<IJobService>();
|
||||||
|
builder.RegisterType<MinioService>().As<IMinioService>();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +1,12 @@
|
||||||
using Bunny.Common;
|
using Autofac;
|
||||||
using Bunny.Common.Configuration;
|
using Autofac.Extensions.DependencyInjection;
|
||||||
using Bunny.Common.Connect;
|
using Bunny.Common;
|
||||||
|
using Bunny.Common.Context;
|
||||||
using Bunny.Service.WebSocket;
|
using Bunny.Service.WebSocket;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||||
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
|
|
||||||
namespace Bunny.WebApi.Config;
|
namespace Bunny.WebApi.Configuration;
|
||||||
|
|
||||||
public class BaseConfig(WebApplicationBuilder builder)
|
public class BaseConfig(WebApplicationBuilder builder)
|
||||||
{
|
{
|
||||||
|
@ -15,21 +18,22 @@ public class BaseConfig(WebApplicationBuilder builder)
|
||||||
// 配置跨域
|
// 配置跨域
|
||||||
UseCors();
|
UseCors();
|
||||||
// 配置日志相关
|
// 配置日志相关
|
||||||
// builder.Logging.AddLog4Net("Config/log4net.config");
|
// builder.Logging.AddLog4Net("Configuration/log4net.config");
|
||||||
// 自定义时间格式
|
// 自定义时间格式
|
||||||
builder.Services.AddControllers().AddJsonOptions(options =>
|
builder.Services.AddControllers().AddJsonOptions(options =>
|
||||||
options.JsonSerializerOptions.Converters.Add(new JsonDateTimeConverter()));
|
options.JsonSerializerOptions.Converters.Add(new JsonDateTimeConverter()));
|
||||||
// 添加使用自定义配置文件
|
// 添加使用自定义配置文件
|
||||||
builder.Services.AddSingleton(new AppSettings(builder.Configuration));
|
builder.Services.AddSingleton(new AppSettings(builder.Configuration));
|
||||||
// 添加 SignalR
|
|
||||||
builder.Services.AddSignalR();
|
|
||||||
builder.WebHost.ConfigureKestrel((_, options) =>
|
builder.WebHost.ConfigureKestrel((_, options) =>
|
||||||
{
|
{
|
||||||
// 设置文件最大上传大小
|
// 设置文件最大上传大小
|
||||||
options.Limits.MaxRequestBodySize = 1024 * 1024 * 100;
|
options.Limits.MaxRequestBodySize = 1024 * 1024 * 100;
|
||||||
});
|
});
|
||||||
// 添加Service服务
|
// 使用 AddAutofac 注册服务
|
||||||
builder.AddApplicationServices();
|
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
|
||||||
|
builder.Host.ConfigureContainer<ContainerBuilder>(AddAutofacConfig.BuildContainer);
|
||||||
|
// 让控制器实例由容器创建
|
||||||
|
builder.Services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
|
||||||
// 添加定时任务
|
// 添加定时任务
|
||||||
builder.AddApplicationBackendServices();
|
builder.AddApplicationBackendServices();
|
||||||
// 设置过滤器
|
// 设置过滤器
|
|
@ -1,7 +1,7 @@
|
||||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
|
|
||||||
namespace Bunny.WebApi.Config;
|
namespace Bunny.WebApi.Configuration;
|
||||||
|
|
||||||
public static class Knife4Net
|
public static class Knife4Net
|
||||||
{
|
{
|
|
@ -1,30 +1,14 @@
|
||||||
using Bunny.Common.Connect;
|
using Bunny.Common.Filter;
|
||||||
using Bunny.Common.Filter;
|
|
||||||
using Bunny.Service.Filter;
|
using Bunny.Service.Filter;
|
||||||
using Bunny.Service.IService;
|
|
||||||
using Bunny.Service.IService.Service;
|
|
||||||
using Bunny.Service.Job.JobService;
|
using Bunny.Service.Job.JobService;
|
||||||
using Lazy.Captcha.Core;
|
using Lazy.Captcha.Core;
|
||||||
using Lazy.Captcha.Core.Generator;
|
using Lazy.Captcha.Core.Generator;
|
||||||
using SkiaSharp;
|
using SkiaSharp;
|
||||||
|
|
||||||
namespace Bunny.WebApi.Config;
|
namespace Bunny.WebApi.Configuration;
|
||||||
|
|
||||||
public static class ServiceRegistration
|
public static class ServiceRegistration
|
||||||
{
|
{
|
||||||
public static void AddApplicationServices(this WebApplicationBuilder builder)
|
|
||||||
{
|
|
||||||
// 注入EfCore上下文对象
|
|
||||||
builder.Services.AddScoped<EfCoreContext>();
|
|
||||||
|
|
||||||
// 注入Service服务
|
|
||||||
builder.Services.AddScoped<IBaseService, BaseService>();
|
|
||||||
builder.Services.AddScoped<IBlogService, BlogService>();
|
|
||||||
builder.Services.AddScoped<IRedisOptionService, RedisOptionService>();
|
|
||||||
builder.Services.AddScoped<IJobService, JobService>();
|
|
||||||
builder.Services.AddScoped<IMinioService, MinioService>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 注入后台服务相关
|
/// 注入后台服务相关
|
||||||
/// 用于添加所以Job服务,之后在BaseConfig中进行调用
|
/// 用于添加所以Job服务,之后在BaseConfig中进行调用
|
|
@ -1,6 +1,8 @@
|
||||||
using Bunny.Dao.Entity.System;
|
using Bunny.Dao.Dto.System;
|
||||||
|
using Bunny.Dao.Entity.System;
|
||||||
using Bunny.Dao.Model.Result;
|
using Bunny.Dao.Model.Result;
|
||||||
using Bunny.Service.IService;
|
using Bunny.Service.IService;
|
||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace Bunny.WebApi.Controllers;
|
namespace Bunny.WebApi.Controllers;
|
||||||
|
@ -8,15 +10,10 @@ namespace Bunny.WebApi.Controllers;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// BLog相关接口
|
/// BLog相关接口
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Route("/api/[controller]/[action]")]
|
[Microsoft.AspNetCore.Mvc.Route("/api/[controller]/[action]")]
|
||||||
public class BlogController : ControllerBase
|
public class BlogController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly IBlogService _blogService;
|
[Inject] public required IBlogService BlogService { get; set; }
|
||||||
|
|
||||||
public BlogController(IBlogService blogService)
|
|
||||||
{
|
|
||||||
_blogService = blogService;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 添加Blog
|
/// 添加Blog
|
||||||
|
@ -26,7 +23,7 @@ public class BlogController : ControllerBase
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public Result<object> AddBlog(Blog dto)
|
public Result<object> AddBlog(Blog dto)
|
||||||
{
|
{
|
||||||
_blogService.AddBlog(dto);
|
BlogService.AddBlog(dto);
|
||||||
return Result<object>.Success();
|
return Result<object>.Success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,7 +34,7 @@ public class BlogController : ControllerBase
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public Result<List<Blog>> QueryBlog()
|
public Result<List<Blog>> QueryBlog()
|
||||||
{
|
{
|
||||||
var vo = _blogService.QueryBlog();
|
var vo = BlogService.QueryBlog();
|
||||||
return Result<List<Blog>>.Success(vo);
|
return Result<List<Blog>>.Success(vo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,9 +44,9 @@ public class BlogController : ControllerBase
|
||||||
/// <param name="dto"></param>
|
/// <param name="dto"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public Result<object> UpdateBlog(Blog dto)
|
public Result<object> UpdateBlog(BlogUpdateDto dto)
|
||||||
{
|
{
|
||||||
_blogService.UpdateBlog(dto);
|
BlogService.UpdateBlog(dto);
|
||||||
return Result<object>.Success();
|
return Result<object>.Success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +57,7 @@ public class BlogController : ControllerBase
|
||||||
[HttpDelete]
|
[HttpDelete]
|
||||||
public Result<string> DeleteBlog(string id)
|
public Result<string> DeleteBlog(string id)
|
||||||
{
|
{
|
||||||
_blogService.DeleteBlog(id);
|
BlogService.DeleteBlog(id);
|
||||||
return Result<string>.Success();
|
return Result<string>.Success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,7 +68,7 @@ public class BlogController : ControllerBase
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public Result<string> AddBatchBlogs(string url)
|
public Result<string> AddBatchBlogs(string url)
|
||||||
{
|
{
|
||||||
_blogService.AddBatchBlogs(url);
|
BlogService.AddBatchBlogs(url);
|
||||||
return Result<string>.Success();
|
return Result<string>.Success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,7 +79,7 @@ public class BlogController : ControllerBase
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public Result<string> DeleteBatchBlogs()
|
public Result<string> DeleteBatchBlogs()
|
||||||
{
|
{
|
||||||
_blogService.DeleteBatchBlogs();
|
BlogService.DeleteBatchBlogs();
|
||||||
return Result<string>.Success();
|
return Result<string>.Success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,7 +90,7 @@ public class BlogController : ControllerBase
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public Result<string> UseTransaction()
|
public Result<string> UseTransaction()
|
||||||
{
|
{
|
||||||
_blogService.UseTransaction();
|
BlogService.UseTransaction();
|
||||||
return Result<string>.Success();
|
return Result<string>.Success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,7 +101,7 @@ public class BlogController : ControllerBase
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public Result<PageResult<Blog>> QueryPage(int page = 1, int limit = 10)
|
public Result<PageResult<Blog>> QueryPage(int page = 1, int limit = 10)
|
||||||
{
|
{
|
||||||
var vo = _blogService.QueryPage(page, limit);
|
var vo = BlogService.QueryPage(page, limit);
|
||||||
|
|
||||||
return Result<PageResult<Blog>>.Success(vo);
|
return Result<PageResult<Blog>>.Success(vo);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
using Bunny.Dao.Model.Constant.Result;
|
using Bunny.Dao.Model.Constant.Result;
|
||||||
using Bunny.Dao.Model.Result;
|
using Bunny.Dao.Model.Result;
|
||||||
using Lazy.Captcha.Core;
|
using Lazy.Captcha.Core;
|
||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace Bunny.WebApi.Controllers;
|
namespace Bunny.WebApi.Controllers;
|
||||||
|
@ -8,9 +9,11 @@ namespace Bunny.WebApi.Controllers;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 验证码测试
|
/// 验证码测试
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Route("/api/[controller]/[action]")]
|
[Microsoft.AspNetCore.Mvc.Route("/api/[controller]/[action]")]
|
||||||
public class CaptchaTestController(ICaptcha _captcha) : ControllerBase
|
public class CaptchaTestController : ControllerBase
|
||||||
{
|
{
|
||||||
|
[Inject] public required ICaptcha Captcha { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 测试测试验证码生成
|
/// 测试测试验证码生成
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -18,7 +21,7 @@ public class CaptchaTestController(ICaptcha _captcha) : ControllerBase
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public IActionResult GetCaptcha(string id)
|
public IActionResult GetCaptcha(string id)
|
||||||
{
|
{
|
||||||
var captchaData = _captcha.Generate(id);
|
var captchaData = Captcha.Generate(id);
|
||||||
// 验证码的ID
|
// 验证码的ID
|
||||||
var captchaDataId = captchaData.Id;
|
var captchaDataId = captchaData.Id;
|
||||||
// 验证码的代码
|
// 验证码的代码
|
||||||
|
@ -39,7 +42,9 @@ public class CaptchaTestController(ICaptcha _captcha) : ControllerBase
|
||||||
public Result<string> CheckCode(string id, string code)
|
public Result<string> CheckCode(string id, string code)
|
||||||
{
|
{
|
||||||
// 校验用户输入的验证码是否=正确
|
// 校验用户输入的验证码是否=正确
|
||||||
var validate = _captcha.Validate(id, code);
|
var validate = Captcha.Validate(id, code);
|
||||||
return validate ? Result<string>.Success() : Result<string>.Error(ErrorConstant.ValidateCodeError);
|
return validate
|
||||||
|
? Result<string>.Success()
|
||||||
|
: Result<string>.Error(null!, ErrorConstant.ValidateCodeError);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
using Bunny.Dao.Model.Result;
|
using Bunny.Dao.Model.Result;
|
||||||
using Bunny.Service.IService;
|
using Bunny.Service.IService;
|
||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace Bunny.WebApi.Controllers;
|
namespace Bunny.WebApi.Controllers;
|
||||||
|
@ -7,9 +8,11 @@ namespace Bunny.WebApi.Controllers;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Quartz 示例相关
|
/// Quartz 示例相关
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Route("/api/[controller]/[action]")]
|
[Microsoft.AspNetCore.Mvc.Route("/api/[controller]/[action]")]
|
||||||
public class JobInitController(IJobService jobService) : ControllerBase
|
public class JobInitController : ControllerBase
|
||||||
{
|
{
|
||||||
|
[Inject] public required IJobService JobService { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 1. 开启一个简单的工作
|
/// 1. 开启一个简单的工作
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -17,7 +20,7 @@ public class JobInitController(IJobService jobService) : ControllerBase
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public Result<string> StartSimpleJob()
|
public Result<string> StartSimpleJob()
|
||||||
{
|
{
|
||||||
jobService.StartSimpleJob();
|
JobService.StartSimpleJob();
|
||||||
return Result<string>.Success("将名称生成放在jobDetail中,可以再运行时获取到");
|
return Result<string>.Success("将名称生成放在jobDetail中,可以再运行时获取到");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +31,7 @@ public class JobInitController(IJobService jobService) : ControllerBase
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public Result<string> PutJobDetail4Trigger()
|
public Result<string> PutJobDetail4Trigger()
|
||||||
{
|
{
|
||||||
jobService.PutJobDetail4Trigger();
|
JobService.PutJobDetail4Trigger();
|
||||||
return Result<string>.Success("将jobDetail中的一些信息放在触发器中,简化写法");
|
return Result<string>.Success("将jobDetail中的一些信息放在触发器中,简化写法");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +42,7 @@ public class JobInitController(IJobService jobService) : ControllerBase
|
||||||
[HttpHead]
|
[HttpHead]
|
||||||
public Result<string> SetJobWithDaily()
|
public Result<string> SetJobWithDaily()
|
||||||
{
|
{
|
||||||
jobService.SetJobWithDaily();
|
JobService.SetJobWithDaily();
|
||||||
return Result<string>.Success();
|
return Result<string>.Success();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,15 +1,18 @@
|
||||||
using Bunny.Dao.Model.Result;
|
using Bunny.Dao.Model.Result;
|
||||||
using Bunny.Service.IService;
|
using Bunny.Service.IService;
|
||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Minio.DataModel;
|
using Minio.DataModel;
|
||||||
using Minio.DataModel.Result;
|
using Minio.DataModel.Result;
|
||||||
|
|
||||||
namespace Bunny.WebApi.Controllers;
|
namespace Bunny.WebApi.Controllers;
|
||||||
|
|
||||||
[Route("/api/[controller]/[action]")]
|
[Microsoft.AspNetCore.Mvc.Route("/api/[controller]/[action]")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class MinioController(IMinioService minioService) : ControllerBase
|
public class MinioController : ControllerBase
|
||||||
{
|
{
|
||||||
|
[Inject] public required IMinioService MinioService { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 查询所有的桶
|
/// 查询所有的桶
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -17,7 +20,7 @@ public class MinioController(IMinioService minioService) : ControllerBase
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public Result<ListAllMyBucketsResult> GetAllMyBuckets()
|
public Result<ListAllMyBucketsResult> GetAllMyBuckets()
|
||||||
{
|
{
|
||||||
var listAllMyBucketsResult = minioService.GetAllMyBuckets();
|
var listAllMyBucketsResult = MinioService.GetAllMyBuckets();
|
||||||
return Result<ListAllMyBucketsResult>.Success(listAllMyBucketsResult);
|
return Result<ListAllMyBucketsResult>.Success(listAllMyBucketsResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +32,7 @@ public class MinioController(IMinioService minioService) : ControllerBase
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public Result<string> SaveMinioFile(IFormFile? file, string filepath = "test")
|
public Result<string> SaveMinioFile(IFormFile? file, string filepath = "test")
|
||||||
{
|
{
|
||||||
minioService.SaveMinioFile(file, filepath);
|
MinioService.SaveMinioFile(file, filepath);
|
||||||
return Result<string>.Success("上传成功");
|
return Result<string>.Success("上传成功");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,7 +45,7 @@ public class MinioController(IMinioService minioService) : ControllerBase
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public Result<ObjectStat> GetObjectStat(string filename, string bucketName = "csharp-test")
|
public Result<ObjectStat> GetObjectStat(string filename, string bucketName = "csharp-test")
|
||||||
{
|
{
|
||||||
var vo = minioService.GetObjectStat(filename, bucketName);
|
var vo = MinioService.GetObjectStat(filename, bucketName);
|
||||||
return Result<ObjectStat>.Success(vo);
|
return Result<ObjectStat>.Success(vo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +58,7 @@ public class MinioController(IMinioService minioService) : ControllerBase
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public Result<string> GetObjectPath(string filename, string bucketName = "csharp-test")
|
public Result<string> GetObjectPath(string filename, string bucketName = "csharp-test")
|
||||||
{
|
{
|
||||||
var vo = minioService.GetObjectPath(filename, bucketName);
|
var vo = MinioService.GetObjectPath(filename, bucketName);
|
||||||
return Result<string>.Success(vo);
|
return Result<string>.Success(vo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +71,7 @@ public class MinioController(IMinioService minioService) : ControllerBase
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public IActionResult DownloadObject(string filename, string bucketName = "csharp-test")
|
public IActionResult DownloadObject(string filename, string bucketName = "csharp-test")
|
||||||
{
|
{
|
||||||
var buffer = minioService.DownloadObject(filename, bucketName).Result;
|
var buffer = MinioService.DownloadObject(filename, bucketName).Result;
|
||||||
return File(buffer, "application/octet-stream", filename);
|
return File(buffer, "application/octet-stream", filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,7 +84,7 @@ public class MinioController(IMinioService minioService) : ControllerBase
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public IActionResult ViewObject(string filename, string bucketName = "csharp-test")
|
public IActionResult ViewObject(string filename, string bucketName = "csharp-test")
|
||||||
{
|
{
|
||||||
var buffer = minioService.DownloadObject(filename, bucketName).Result;
|
var buffer = MinioService.DownloadObject(filename, bucketName).Result;
|
||||||
return File(buffer, "image/jpeg");
|
return File(buffer, "image/jpeg");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,7 +97,7 @@ public class MinioController(IMinioService minioService) : ControllerBase
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public Result<IAsyncEnumerable<Item>> ListObject(string? filepath, string bucketName = "csharp-test")
|
public Result<IAsyncEnumerable<Item>> ListObject(string? filepath, string bucketName = "csharp-test")
|
||||||
{
|
{
|
||||||
var vo = minioService.ListObject(filepath, bucketName);
|
var vo = MinioService.ListObject(filepath, bucketName);
|
||||||
return Result<IAsyncEnumerable<Item>>.Success(vo);
|
return Result<IAsyncEnumerable<Item>>.Success(vo);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
using Bunny.Dao.Model.Result;
|
using Bunny.Dao.Model.Result;
|
||||||
using Bunny.Service.IService;
|
using Bunny.Service.IService;
|
||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace Bunny.WebApi.Controllers;
|
namespace Bunny.WebApi.Controllers;
|
||||||
|
@ -7,15 +8,10 @@ namespace Bunny.WebApi.Controllers;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 操作Redis相关内容
|
/// 操作Redis相关内容
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Route("/api/[controller]/[action]")]
|
[Microsoft.AspNetCore.Mvc.Route("/api/[controller]/[action]")]
|
||||||
public class RedisOptionController : ControllerBase
|
public class RedisOptionController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly IRedisOptionService _redisOptionService;
|
[Inject] public required IRedisOptionService RedisOptionService { get; set; }
|
||||||
|
|
||||||
public RedisOptionController(IRedisOptionService redisOptionService)
|
|
||||||
{
|
|
||||||
_redisOptionService = redisOptionService;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 添加Redis中一个值
|
/// 添加Redis中一个值
|
||||||
|
@ -26,7 +22,7 @@ public class RedisOptionController : ControllerBase
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public Result<string> AddStringValue(string key, string value)
|
public Result<string> AddStringValue(string key, string value)
|
||||||
{
|
{
|
||||||
_redisOptionService.AddStringValue(key, value);
|
RedisOptionService.AddStringValue(key, value);
|
||||||
return Result<string>.Success();
|
return Result<string>.Success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,7 +33,7 @@ public class RedisOptionController : ControllerBase
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public Result<string> QueryStringKey(string key)
|
public Result<string> QueryStringKey(string key)
|
||||||
{
|
{
|
||||||
var queryStringKey = _redisOptionService.QueryStringKey(key);
|
var queryStringKey = RedisOptionService.QueryStringKey(key);
|
||||||
return Result<string>.Success(queryStringKey);
|
return Result<string>.Success(queryStringKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,7 +46,7 @@ public class RedisOptionController : ControllerBase
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public Result<string> AddTimeRedisKey(string key, string value)
|
public Result<string> AddTimeRedisKey(string key, string value)
|
||||||
{
|
{
|
||||||
_redisOptionService.AddTimeRedisKey(key, value);
|
RedisOptionService.AddTimeRedisKey(key, value);
|
||||||
return Result<string>.Success(value);
|
return Result<string>.Success(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +59,7 @@ public class RedisOptionController : ControllerBase
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public Result<string> AddTimeRedisKeyTtl(string key, string value)
|
public Result<string> AddTimeRedisKeyTtl(string key, string value)
|
||||||
{
|
{
|
||||||
_redisOptionService.AddTimeRedisKeyTtl(key, value);
|
RedisOptionService.AddTimeRedisKeyTtl(key, value);
|
||||||
return Result<string>.Success(value);
|
return Result<string>.Success(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,7 +70,7 @@ public class RedisOptionController : ControllerBase
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public Result<string> AddJson()
|
public Result<string> AddJson()
|
||||||
{
|
{
|
||||||
var json = _redisOptionService.AddJson();
|
var json = RedisOptionService.AddJson();
|
||||||
return Result<string>.Success(json);
|
return Result<string>.Success(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,7 +82,7 @@ public class RedisOptionController : ControllerBase
|
||||||
[HttpDelete]
|
[HttpDelete]
|
||||||
public Result<string> DeleteKey(string key)
|
public Result<string> DeleteKey(string key)
|
||||||
{
|
{
|
||||||
_redisOptionService.DeleteKey(key);
|
RedisOptionService.DeleteKey(key);
|
||||||
return Result<string>.Success();
|
return Result<string>.Success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,7 +93,7 @@ public class RedisOptionController : ControllerBase
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public Result<string> SetRedisCreateTransaction(string key, string value)
|
public Result<string> SetRedisCreateTransaction(string key, string value)
|
||||||
{
|
{
|
||||||
var redisCreateTransaction = _redisOptionService.SetRedisCreateTransaction(key, value);
|
var redisCreateTransaction = RedisOptionService.SetRedisCreateTransaction(key, value);
|
||||||
return Result<string>.Success(redisCreateTransaction);
|
return Result<string>.Success(redisCreateTransaction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,7 +104,7 @@ public class RedisOptionController : ControllerBase
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public Result<string> AddHashWithRedis(string key, double keyExpire = 6.0)
|
public Result<string> AddHashWithRedis(string key, double keyExpire = 6.0)
|
||||||
{
|
{
|
||||||
_redisOptionService.AddHashWithRedis(key, keyExpire);
|
RedisOptionService.AddHashWithRedis(key, keyExpire);
|
||||||
return Result<string>.Success();
|
return Result<string>.Success();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
using Bunny.Dao.Model.Result;
|
using Bunny.Dao.Model.Result;
|
||||||
using Bunny.Service.IService;
|
using Bunny.Service.IService;
|
||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace Bunny.WebApi.Controllers;
|
namespace Bunny.WebApi.Controllers;
|
||||||
|
@ -8,15 +9,10 @@ namespace Bunny.WebApi.Controllers;
|
||||||
/// 测试请求模板
|
/// 测试请求模板
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/[controller]")]
|
[Microsoft.AspNetCore.Mvc.Route("api/[controller]")]
|
||||||
public class TemplateController : ControllerBase
|
public class TemplateController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly IBaseService _baseService;
|
[Inject] public required IBaseService BaseService { get; set; }
|
||||||
|
|
||||||
public TemplateController(IBaseService baseService)
|
|
||||||
{
|
|
||||||
_baseService = baseService;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 测试走缓存
|
/// 测试走缓存
|
||||||
|
@ -36,7 +32,7 @@ public class TemplateController : ControllerBase
|
||||||
[HttpGet("TestInject")]
|
[HttpGet("TestInject")]
|
||||||
public Result<string> TestInject()
|
public Result<string> TestInject()
|
||||||
{
|
{
|
||||||
var testIndex = _baseService.TestIndex();
|
var testIndex = BaseService.TestIndex();
|
||||||
return Result<string>.Success(testIndex);
|
return Result<string>.Success(testIndex);
|
||||||
}
|
}
|
||||||
}
|
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,4 +1,4 @@
|
||||||
using Bunny.WebApi.Config;
|
using Bunny.WebApi.Configuration;
|
||||||
using IGeekFan.AspNetCore.Knife4jUI;
|
using IGeekFan.AspNetCore.Knife4jUI;
|
||||||
|
|
||||||
namespace Bunny.WebApi;
|
namespace Bunny.WebApi;
|
||||||
|
@ -13,7 +13,6 @@ public class Program
|
||||||
// 基础配置
|
// 基础配置
|
||||||
var baseConfig = new BaseConfig(builder);
|
var baseConfig = new BaseConfig(builder);
|
||||||
baseConfig.Initialize();
|
baseConfig.Initialize();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
if (app.Environment.IsDevelopment())
|
if (app.Environment.IsDevelopment())
|
||||||
|
|
|
@ -62,3 +62,77 @@ service.AddCaptcha(options =>
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## AutoFac配置
|
||||||
|
|
||||||
|
### 自动注入按照名称注入
|
||||||
|
|
||||||
|
**AutoFac配置详情**
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
using Autofac;
|
||||||
|
using Bunny.Common.Attribute;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace Bunny.WebApi.Config;
|
||||||
|
|
||||||
|
public static class AddAutofacConfig
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// AutoFac自动注入约定名称
|
||||||
|
/// 接口以Service结尾注入到IOC中
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="builder"></param>
|
||||||
|
public static void BuildContainer(ContainerBuilder builder)
|
||||||
|
{
|
||||||
|
// 扫描所以前缀Bunny的命名空间
|
||||||
|
var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(t => t.FullName!.StartsWith("Bunny"));
|
||||||
|
|
||||||
|
// 遍历注入服务
|
||||||
|
foreach (var assembly in assemblies)
|
||||||
|
{
|
||||||
|
var types = assembly.GetTypes();
|
||||||
|
|
||||||
|
// 注入Service
|
||||||
|
var serviceTypes = types.Where(t => t.GetCustomAttributes(typeof(ServiceAttribute), false).Length != 0);
|
||||||
|
foreach (var serviceType in serviceTypes)
|
||||||
|
{
|
||||||
|
var interfaces = serviceType.GetInterfaces();
|
||||||
|
builder.RegisterType(serviceType).As(interfaces).InstancePerDependency();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 注入数据库相关
|
||||||
|
var mapperTypes = types.Where(t => t.GetCustomAttributes(typeof(MapperAttribute), false).Length != 0);
|
||||||
|
foreach (var mapperType in mapperTypes)
|
||||||
|
{
|
||||||
|
var interfaces = mapperType.GetInterfaces();
|
||||||
|
builder.RegisterType(mapperType).As(interfaces).InstancePerDependency();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 注入后台服务
|
||||||
|
var componentTypes =
|
||||||
|
types.Where(t => t.GetCustomAttributes(typeof(ComponentAttribute), false).Length != 0);
|
||||||
|
foreach (var componentType in componentTypes)
|
||||||
|
builder.RegisterType(componentType).AsSelf().As<IHostedService>().SingleInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果不在在 Controller 层写构造函数可以打开这个,自动完成注入
|
||||||
|
var controllerBaseType = typeof(ControllerBase);
|
||||||
|
builder.RegisterAssemblyTypes(typeof(Program).Assembly)
|
||||||
|
.Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
|
||||||
|
.PropertiesAutowired(); //支持属性注入
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**主程序入口配置**
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
// 让控制器实例由容器创建
|
||||||
|
builder.Services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
|
||||||
|
|
||||||
|
// 如果将Service放在 WebApi同级目录下,可以完成Controller自动注入,不需要写构造函数
|
||||||
|
builder.Services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
|
||||||
|
builder.Services.AddControllers().AddJsonOptions(options =>
|
||||||
|
options.JsonSerializerOptions.Converters.Add(new JsonDateTimeConverter()));
|
||||||
|
```
|
|
@ -8,7 +8,7 @@
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
"DataBase": {
|
"DataBase": {
|
||||||
"DataBaseConnection": "Database/bunny.db",
|
"DataBaseConnection": "192.168.3.98;Initial Catalog=BunnyDemo;TrustServerCertificate=True;Persist Security Info=True;User ID=sa;Password=abc1234.",
|
||||||
"TimeOut": 6
|
"TimeOut": 6
|
||||||
},
|
},
|
||||||
"JWT": {
|
"JWT": {
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue