Skip to content

Commit c6ea94f

Browse files
committed
Fixed anjoy8#230 bug.
1 parent da69167 commit c6ea94f

6 files changed

Lines changed: 146 additions & 78 deletions

File tree

Blog.Core.Api/Controllers/BlogController.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,9 @@ public async Task<MessageModel<string>> Put([FromBody] BlogArticle BlogArticle)
233233
model.btraffic = BlogArticle.btraffic;
234234

235235
if (await _blogArticleServices.Update(model))
236-
Success<string>(BlogArticle?.bID.ObjToString());
236+
{
237+
return Success<string>(BlogArticle?.bID.ObjToString());
238+
}
237239
}
238240
}
239241
return Failed("更新失败");
@@ -254,6 +256,10 @@ public async Task<MessageModel<string>> Delete(int id)
254256
if (id > 0)
255257
{
256258
var blogArticle = await _blogArticleServices.QueryById(id);
259+
if (blogArticle == null)
260+
{
261+
return Failed("查询无数据");
262+
}
257263
blogArticle.IsDeleted = true;
258264
return await _blogArticleServices.Update(blogArticle) ? Success(blogArticle?.bID.ObjToString(), "删除成功") : Failed("删除失败");
259265
}

Blog.Core.Repository/UnitOfWork/UnitOfWork.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Blog.Core.IRepository.UnitOfWork;
2-
using Microsoft.Extensions.Logging;
32
using SqlSugar;
43
using System;
54

@@ -8,12 +7,10 @@ namespace Blog.Core.Repository.UnitOfWork
87
public class UnitOfWork : IUnitOfWork
98
{
109
private readonly ISqlSugarClient _sqlSugarClient;
11-
private readonly ILogger<UnitOfWork> _logger;
1210

13-
public UnitOfWork(ISqlSugarClient sqlSugarClient, ILogger<UnitOfWork> logger)
11+
public UnitOfWork(ISqlSugarClient sqlSugarClient)
1412
{
1513
_sqlSugarClient = sqlSugarClient;
16-
_logger = logger;
1714
}
1815

1916
/// <summary>
@@ -40,7 +37,6 @@ public void CommitTran()
4037
catch (Exception ex)
4138
{
4239
GetDbClient().RollbackTran();
43-
_logger.LogError($"{ex.Message}\r\n{ex.InnerException}");
4440
}
4541
}
4642

Blog.Core.Tests/Blog.Core.Tests.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@
3030

3131
<ItemGroup>
3232
<ProjectReference Include="..\Blog.Core.Api\Blog.Core.Api.csproj" />
33-
<ProjectReference Include="..\Blog.Core.Repository\Blog.Core.Repository.csproj" />
34-
<ProjectReference Include="..\Blog.Core.Services\Blog.Core.Services.csproj" />
33+
</ItemGroup>
34+
35+
<ItemGroup>
36+
<None Update="WMBlog.db">
37+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
38+
</None>
3539
</ItemGroup>
3640

3741
<ProjectExtensions><VisualStudio><UserProperties appsettings_1json__JsonSchema="" /></VisualStudio></ProjectExtensions>

Blog.Core.Tests/Controller_Test/BlogController_Should.cs

Lines changed: 115 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using Autofac;
22
using Blog.Core.Controllers;
33
using Blog.Core.IServices;
4+
using Blog.Core.Model;
45
using Blog.Core.Model.Models;
6+
using Blog.Core.Model.ViewModels;
57
using Microsoft.Extensions.Logging;
68
using Moq;
79
using System;
10+
using System.Collections.Generic;
811
using Xunit;
912

1013
namespace Blog.Core.Tests
@@ -24,10 +27,10 @@ public BlogController_Should()
2427
{
2528
mockBlogSev.Setup(r => r.Query());
2629

27-
2830
var container = dI_Test.DICollections();
2931
blogArticleServices = container.Resolve<IBlogArticleServices>();
3032
blogController = new BlogController(mockLogger.Object);
33+
blogController._blogArticleServices = blogArticleServices;
3134
}
3235

3336
[Fact]
@@ -37,12 +40,59 @@ public void TestEntity()
3740

3841
Assert.True(blogArticle.bID >= 0);
3942
}
43+
44+
[Fact]
45+
public async void Get_Blog_Page_Test()
46+
{
47+
MessageModel<PageModel<BlogArticle>> blogs = await blogController.Get(1, 1, "技术博文", "");
48+
Assert.NotNull(blogs);
49+
Assert.NotNull(blogs.response);
50+
Assert.True(blogs.response.dataCount >= 0);
51+
}
52+
53+
[Fact]
54+
public async void Get_Blog_Test()
55+
{
56+
MessageModel<BlogViewModels> blogVo = await blogController.Get(1);
57+
58+
Assert.NotNull(blogVo);
59+
}
60+
61+
[Fact]
62+
public async void Get_Blog_For_Nuxt_Test()
63+
{
64+
MessageModel<BlogViewModels> blogVo = await blogController.DetailNuxtNoPer(1);
65+
66+
Assert.NotNull(blogVo);
67+
}
68+
69+
[Fact]
70+
public async void Get_Go_Url_Test()
71+
{
72+
object urlAction = await blogController.GoUrl(1);
73+
74+
Assert.NotNull(urlAction);
75+
}
76+
4077
[Fact]
41-
public async void GetBlogsTest()
78+
public async void Get_Blog_By_Type_For_MVP_Test()
4279
{
43-
object blogs =await blogController.Get(1);
80+
MessageModel<List<BlogArticle>> blogs = await blogController.GetBlogsByTypesForMVP("技术博文");
4481

4582
Assert.NotNull(blogs);
83+
Assert.True(blogs.success);
84+
Assert.NotNull(blogs.response);
85+
Assert.True(blogs.response.Count >= 0);
86+
}
87+
88+
[Fact]
89+
public async void Get_Blog_By_Id_For_MVP_Test()
90+
{
91+
MessageModel<BlogArticle> blog = await blogController.GetBlogByIdForMVP(1);
92+
93+
Assert.NotNull(blog);
94+
Assert.True(blog.success);
95+
Assert.NotNull(blog.response);
4696
}
4797

4898
[Fact]
@@ -53,7 +103,7 @@ public async void PostTest()
53103
bCreateTime = DateTime.Now,
54104
bUpdateTime = DateTime.Now,
55105
btitle = "xuint :test controller addEntity",
56-
106+
bcontent = "xuint :test controller addEntity. this is content.this is content."
57107
};
58108

59109
var res = await blogController.Post(blogArticle);
@@ -64,5 +114,66 @@ public async void PostTest()
64114

65115
Assert.NotNull(data);
66116
}
117+
118+
[Fact]
119+
public async void Post_Insert_For_MVP_Test()
120+
{
121+
BlogArticle blogArticle = new BlogArticle()
122+
{
123+
bCreateTime = DateTime.Now,
124+
bUpdateTime = DateTime.Now,
125+
btitle = "xuint :test controller addEntity",
126+
bcontent = "xuint :test controller addEntity. this is content.this is content."
127+
};
128+
129+
var res = await blogController.AddForMVP(blogArticle);
130+
131+
Assert.True(res.success);
132+
133+
var data = res.response;
134+
135+
Assert.NotNull(data);
136+
}
137+
138+
[Fact]
139+
public async void Put_Test()
140+
{
141+
BlogArticle blogArticle = new BlogArticle()
142+
{
143+
bID = 1,
144+
bCreateTime = DateTime.Now,
145+
bUpdateTime = DateTime.Now,
146+
btitle = "xuint put :test controller addEntity",
147+
bcontent = "xuint put :test controller addEntity. this is content.this is content."
148+
};
149+
150+
var res = await blogController.Put(blogArticle);
151+
152+
Assert.True(res.success);
153+
154+
var data = res.response;
155+
156+
Assert.NotNull(data);
157+
}
158+
159+
[Fact]
160+
public async void Delete_Test()
161+
{
162+
var res = await blogController.Delete(99);
163+
164+
Assert.False(res.success);
165+
166+
var data = res.response;
167+
168+
Assert.Null(data);
169+
}
170+
171+
[Fact]
172+
public async void Apache_Update_Test()
173+
{
174+
var res = await blogController.ApacheTestUpdate();
175+
176+
Assert.True(res.success);
177+
}
67178
}
68179
}

Blog.Core.Tests/DependencyInjection/DI_Test.cs

Lines changed: 17 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,82 +8,25 @@
88
using Blog.Core.Common.DB;
99
using Blog.Core.Common.LogHelper;
1010
using Blog.Core.Common.Seed;
11+
using Blog.Core.Extensions;
1112
using Blog.Core.IRepository.Base;
12-
using Blog.Core.IServices;
1313
using Blog.Core.Repository.Base;
14-
using Blog.Core.Services;
14+
using Microsoft.AspNetCore.Mvc;
15+
using Microsoft.AspNetCore.Mvc.Controllers;
1516
using Microsoft.Extensions.DependencyInjection;
17+
using Microsoft.Extensions.DependencyInjection.Extensions;
1618
using Microsoft.IdentityModel.Tokens;
1719
using System;
1820
using System.Collections.Generic;
1921
using System.IO;
20-
using System.Linq;
2122
using System.Reflection;
2223
using System.Security.Claims;
2324
using System.Text;
24-
using Xunit;
2525

2626
namespace Blog.Core.Tests
2727
{
2828
public class DI_Test
2929
{
30-
31-
[Fact]
32-
public void DI_Connet_Test()
33-
{
34-
var basePath = AppContext.BaseDirectory;
35-
36-
IServiceCollection services = new ServiceCollection().AddLogging();
37-
services.AddAutoMapper(typeof(Startup));
38-
39-
services.AddScoped<SqlSugar.ISqlSugarClient>(o =>
40-
{
41-
return new SqlSugar.SqlSugarScope(new SqlSugar.ConnectionConfig()
42-
{
43-
ConnectionString = GetMainConnectionDb().Connection,//必填, 数据库连接字符串
44-
DbType = (SqlSugar.DbType)GetMainConnectionDb().DbType,//必填, 数据库类型
45-
IsAutoCloseConnection = true,//默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
46-
IsShardSameThread = true,//共享线程
47-
InitKeyType = SqlSugar.InitKeyType.SystemTable//默认SystemTable, 字段信息读取, 如:该属性是不是主键,标识列等等信息
48-
});
49-
});
50-
51-
//services.AddSingleton(new Appsettings(Env));
52-
53-
54-
//实例化 AutoFac 容器
55-
var builder = new ContainerBuilder();
56-
builder.RegisterType<AdvertisementServices>().As<IAdvertisementServices>();
57-
58-
//指定已扫描程序集中的类型注册为提供所有其实现的接口。
59-
//var assemblysServices = Assembly.Load("Blog.Core.Services");
60-
//builder.RegisterAssemblyTypes(assemblysServices).AsImplementedInterfaces();
61-
//var assemblysRepository = Assembly.Load("Blog.Core.Repository");
62-
//builder.RegisterAssemblyTypes(assemblysRepository).AsImplementedInterfaces();
63-
64-
var servicesDllFile = Path.Combine(basePath, "Blog.Core.Services.dll");
65-
var assemblysServices = Assembly.LoadFrom(servicesDllFile);
66-
builder.RegisterAssemblyTypes(assemblysServices)
67-
.AsImplementedInterfaces()
68-
.InstancePerLifetimeScope()
69-
.EnableInterfaceInterceptors();
70-
71-
var repositoryDllFile = Path.Combine(basePath, "Blog.Core.Repository.dll");
72-
var assemblysRepository = Assembly.LoadFrom(repositoryDllFile);
73-
builder.RegisterAssemblyTypes(assemblysRepository).AsImplementedInterfaces();
74-
75-
//将services填充到Autofac容器生成器中
76-
builder.Populate(services);
77-
78-
//使用已进行的组件登记创建新容器
79-
var ApplicationContainer = builder.Build();
80-
81-
var blogservice = ApplicationContainer.Resolve<IBlogArticleServices>();
82-
83-
Assert.True(ApplicationContainer.ComponentRegistry.Registrations.Count() > 0);
84-
}
85-
86-
8730
/// <summary>
8831
/// 连接字符串
8932
/// Blog.Core
@@ -146,16 +89,13 @@ public IContainer DICollections()
14689
policy => policy.Requirements.Add(permissionRequirement));
14790
});
14891

149-
150-
15192
services.AddScoped<SqlSugar.ISqlSugarClient>(o =>
15293
{
15394
return new SqlSugar.SqlSugarScope(new SqlSugar.ConnectionConfig()
15495
{
15596
ConnectionString = GetMainConnectionDb().Connection,//必填, 数据库连接字符串
15697
DbType = (SqlSugar.DbType)GetMainConnectionDb().DbType,//必填, 数据库类型
15798
IsAutoCloseConnection = true,//默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
158-
IsShardSameThread = true,//共享线程
15999
InitKeyType = SqlSugar.InitKeyType.SystemTable//默认SystemTable, 字段信息读取, 如:该属性是不是主键,标识列等等信息
160100
});
161101
});
@@ -168,17 +108,28 @@ public IContainer DICollections()
168108

169109
builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IBaseRepository<>)).InstancePerDependency();//注册仓储
170110

171-
111+
// 属性注入
112+
var controllerBaseType = typeof(ControllerBase);
113+
builder.RegisterAssemblyTypes(typeof(Program).Assembly)
114+
.Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
115+
.PropertiesAutowired();
116+
172117
var servicesDllFile = Path.Combine(basePath, "Blog.Core.Services.dll");
173118
var assemblysServices = Assembly.LoadFrom(servicesDllFile);
174119
builder.RegisterAssemblyTypes(assemblysServices)
175120
.AsImplementedInterfaces()
176121
.InstancePerLifetimeScope()
122+
.PropertiesAutowired()
177123
.EnableInterfaceInterceptors();
178124

179125
var repositoryDllFile = Path.Combine(basePath, "Blog.Core.Repository.dll");
180126
var assemblysRepository = Assembly.LoadFrom(repositoryDllFile);
181-
builder.RegisterAssemblyTypes(assemblysRepository).AsImplementedInterfaces();
127+
builder.RegisterAssemblyTypes(assemblysRepository)
128+
.PropertiesAutowired().AsImplementedInterfaces();
129+
130+
services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
131+
132+
services.AddAutoMapperSetup();
182133

183134
//将services填充到Autofac容器生成器中
184135
builder.Populate(services);

Blog.Core.Tests/WMBlog.db

188 KB
Binary file not shown.

0 commit comments

Comments
 (0)