feat: 添加swagger文档,优化枚举操作
This commit is contained in:
@@ -1,7 +1,11 @@
|
|||||||
using System.Diagnostics;
|
using System.ComponentModel;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Text;
|
||||||
|
using System.Xml.Linq;
|
||||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
using Microsoft.OpenApi.Any;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||||
using Volo.Abp.AspNetCore.Mvc;
|
using Volo.Abp.AspNetCore.Mvc;
|
||||||
@@ -75,12 +79,59 @@ namespace Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection
|
|||||||
{
|
{
|
||||||
[scheme] = new string[0]
|
[scheme] = new string[0]
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
options.SchemaFilter<EnumSchemaFilter>();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Swagger文档枚举字段显示枚举属性和枚举值,以及枚举描述
|
||||||
|
/// </summary>
|
||||||
|
public class EnumSchemaFilter : ISchemaFilter
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 实现接口
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="model"></param>
|
||||||
|
/// <param name="context"></param>
|
||||||
|
|
||||||
|
public void Apply(OpenApiSchema model, SchemaFilterContext context)
|
||||||
|
{
|
||||||
|
if (context.Type.IsEnum)
|
||||||
|
{
|
||||||
|
model.Enum.Clear();
|
||||||
|
model.Type = "string";
|
||||||
|
model.Format = null;
|
||||||
|
|
||||||
|
|
||||||
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
Enum.GetNames(context.Type)
|
||||||
|
.ToList()
|
||||||
|
.ForEach(name =>
|
||||||
|
{
|
||||||
|
Enum e = (Enum)Enum.Parse(context.Type, name);
|
||||||
|
var descrptionOrNull = GetEnumDescription(e);
|
||||||
|
model.Enum.Add(new OpenApiString(name));
|
||||||
|
stringBuilder.Append($"【枚举:{name}{(descrptionOrNull is null ? string.Empty : $"({descrptionOrNull})")}={Convert.ToInt64(Enum.Parse(context.Type, name))}】<br />");
|
||||||
|
});
|
||||||
|
model.Description= stringBuilder.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string? GetEnumDescription(Enum value)
|
||||||
|
{
|
||||||
|
var fieldInfo = value.GetType().GetField(value.ToString());
|
||||||
|
var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
||||||
|
return attributes.Length > 0 ? attributes[0].Description : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using System.ComponentModel.DataAnnotations;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Yi.Framework.Bbs.Domain.Shared.Enums;
|
using Yi.Framework.Bbs.Domain.Shared.Enums;
|
||||||
|
|
||||||
namespace Yi.Framework.Bbs.Application.Contracts.Dtos.Article
|
namespace Yi.Framework.Bbs.Application.Contracts.Dtos.Article
|
||||||
@@ -16,6 +17,8 @@ namespace Yi.Framework.Bbs.Application.Contracts.Dtos.Article
|
|||||||
[Required]
|
[Required]
|
||||||
public Guid DiscussId { get; set; }
|
public Guid DiscussId { get; set; }
|
||||||
|
|
||||||
|
public Guid ArticleParentId { get; set; }= Guid.Empty;
|
||||||
|
|
||||||
public ArticleImportTypeEnum ImportType { get; set; } = ArticleImportTypeEnum.Defalut;
|
public ArticleImportTypeEnum ImportType { get; set; } = ArticleImportTypeEnum.Defalut;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Mapster;
|
using Mapster;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
@@ -143,7 +144,7 @@ namespace Yi.Framework.Bbs.Application.Services
|
|||||||
/// 导入文章
|
/// 导入文章
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task PostImportAsync(ArticleImprotDto input, [FromForm] IFormFileCollection file)
|
public async Task PostImportAsync([FromQuery] ArticleImprotDto input, [FromForm][Required] IFormFileCollection file)
|
||||||
{
|
{
|
||||||
var fileObjs = new List<FileObject>();
|
var fileObjs = new List<FileObject>();
|
||||||
if (file.Count > 0)
|
if (file.Count > 0)
|
||||||
@@ -172,7 +173,7 @@ namespace Yi.Framework.Bbs.Application.Services
|
|||||||
throw new UserFriendlyException("未选择文件");
|
throw new UserFriendlyException("未选择文件");
|
||||||
}
|
}
|
||||||
//使用简单工厂根据传入的类型进行判断
|
//使用简单工厂根据传入的类型进行判断
|
||||||
await _forumManager.PostImportAsync(input.DiscussId, fileObjs, input.ImportType);
|
await _forumManager.PostImportAsync(input.DiscussId, input.ArticleParentId, fileObjs, input.ImportType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,16 @@
|
|||||||
using System;
|
using System.ComponentModel;
|
||||||
using System.Collections.Generic;
|
using Newtonsoft.Json;
|
||||||
using System.Linq;
|
using Newtonsoft.Json.Converters;
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Yi.Framework.Bbs.Domain.Shared.Enums
|
namespace Yi.Framework.Bbs.Domain.Shared.Enums
|
||||||
{
|
{
|
||||||
|
[JsonConverter(typeof(StringEnumConverter))]
|
||||||
public enum ArticleImportTypeEnum
|
public enum ArticleImportTypeEnum
|
||||||
{
|
{
|
||||||
//默认导入方式
|
[Description("默认导入方式")]
|
||||||
Defalut,
|
Defalut,
|
||||||
|
|
||||||
//vuePresss方式
|
[Description("vuePresss方式")]
|
||||||
VuePress
|
VuePress
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,12 +10,13 @@ namespace Yi.Framework.Bbs.Domain.Managers.ArticleImport
|
|||||||
{
|
{
|
||||||
public abstract class AbstractArticleImport
|
public abstract class AbstractArticleImport
|
||||||
{
|
{
|
||||||
public virtual List<ArticleEntity> Import(Guid discussId, List<FileObject> fileObjs)
|
public virtual List<ArticleEntity> Import(Guid discussId,Guid articleParentId, List<FileObject> fileObjs)
|
||||||
{
|
{
|
||||||
var articles = Convert(fileObjs);
|
var articles = Convert(fileObjs);
|
||||||
articles.ForEach(article =>
|
articles.ForEach(article =>
|
||||||
{
|
{
|
||||||
article.DiscussId = discussId;
|
article.DiscussId = discussId;
|
||||||
|
article.ParentId = articleParentId;
|
||||||
});
|
});
|
||||||
return articles;
|
return articles;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,8 +46,10 @@ namespace Yi.Framework.Bbs.Domain.Managers.ArticleImport
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
lines.ToList().RemoveRange(0, num);
|
var linesRef = lines.ToList();
|
||||||
var result = string.Join(Environment.NewLine, lines);
|
|
||||||
|
linesRef.RemoveRange(0, startIndex+1);
|
||||||
|
var result = string.Join(Environment.NewLine, linesRef);
|
||||||
f.Content = result;
|
f.Content = result;
|
||||||
return f;
|
return f;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -48,10 +48,11 @@ namespace Yi.Framework.Bbs.Domain.Managers
|
|||||||
/// 导入文章
|
/// 导入文章
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="discussId"></param>
|
/// <param name="discussId"></param>
|
||||||
|
/// <param name="articleParentId"></param>
|
||||||
/// <param name="fileObjs"></param>
|
/// <param name="fileObjs"></param>
|
||||||
/// <param name="importType"></param>
|
/// <param name="importType"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task PostImportAsync(Guid discussId, List<FileObject> fileObjs, ArticleImportTypeEnum importType)
|
public async Task PostImportAsync(Guid discussId,Guid articleParentId, List<FileObject> fileObjs, ArticleImportTypeEnum importType)
|
||||||
{
|
{
|
||||||
AbstractArticleImport abstractArticleImport = default;
|
AbstractArticleImport abstractArticleImport = default;
|
||||||
switch (importType)
|
switch (importType)
|
||||||
@@ -67,9 +68,9 @@ namespace Yi.Framework.Bbs.Domain.Managers
|
|||||||
default: abstractArticleImport = new DefaultArticleImport(); break;
|
default: abstractArticleImport = new DefaultArticleImport(); break;
|
||||||
}
|
}
|
||||||
|
|
||||||
var articleHandled = abstractArticleImport.Import(discussId, fileObjs);
|
var articleHandled = abstractArticleImport.Import(discussId, articleParentId, fileObjs);
|
||||||
|
|
||||||
await _articleRepository.InsertManyAsync(articleHandled);
|
//await _articleRepository.InsertManyAsync(articleHandled);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
using Microsoft.AspNetCore.Builder;
|
|
||||||
using Microsoft.AspNetCore.Cors;
|
using Microsoft.AspNetCore.Cors;
|
||||||
using Microsoft.IdentityModel.Tokens;
|
using Microsoft.IdentityModel.Tokens;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
|
using Newtonsoft.Json.Converters;
|
||||||
using Volo.Abp;
|
using Volo.Abp;
|
||||||
using Volo.Abp.AspNetCore.Authentication.JwtBearer;
|
using Volo.Abp.AspNetCore.Authentication.JwtBearer;
|
||||||
using Volo.Abp.AspNetCore.Mvc;
|
using Volo.Abp.AspNetCore.Mvc;
|
||||||
@@ -65,6 +66,7 @@ namespace Yi.Abp.Web
|
|||||||
service.AddControllers().AddNewtonsoftJson(options =>
|
service.AddControllers().AddNewtonsoftJson(options =>
|
||||||
{
|
{
|
||||||
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
||||||
|
options.SerializerSettings.Converters.Add(new StringEnumConverter());
|
||||||
});
|
});
|
||||||
|
|
||||||
Configure<AbpAntiForgeryOptions>(options =>
|
Configure<AbpAntiForgeryOptions>(options =>
|
||||||
|
|||||||
Reference in New Issue
Block a user