Minimal API's در دات نت 6 - قسمت ششم - غنی سازی اطلاعات Swagger
نویسنده: وحید نصیری
تاریخ: ۱۴۰۰/۱۲/۲۴ ۱۲:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class AuthorModule : IModule
{
public IEndpointRouteBuilder RegisterEndpoints(IEndpointRouteBuilder endpoints)
{
endpoints.MapGet("/api/authors",
async (IMediator mediator, CancellationToken ct) => await GetAllAuthorsAsync(mediator, ct));
endpoints.MapPost("/api/authors",
async (IMediator mediator, AuthorDto authorDto, CancellationToken ct) =>
await CreateAuthorAsync(authorDto, mediator, ct));
return endpoints;
}
private static async Task<IResult> CreateAuthorAsync(AuthorDto authorDto, IMediator mediator, CancellationToken ct)
{
var command = new CreateAuthorCommand { AuthorDto = authorDto };
var author = await mediator.Send(command, ct);
return Results.Ok(author);
}
private static async Task<IResult> GetAllAuthorsAsync(IMediator mediator, CancellationToken ct)
{
var request = new GetAllAuthorsQuery();
var authors = await mediator.Send(request, ct);
return Results.Ok(authors);
}
} Challenge, Forbid, SignIn, SignOut, Content, Text, Json, File, Bytes, Stream, Redirect, LocalRedirect, StatusCode NotFound, Unauthorized, BadRequest, Conflict, NoContent, Ok UnprocessableEntity, Problem, ValidationProblem, Created CreatedAtRoute, Accepted, AcceptedAtRoute
try
{
return Results.Ok(await data.GetUsers());
}
catch (Exception ex)
{
return Results.Problem(ex.Message);
} endpoints.MapGet("/api/authors",
async (IMediator mediator, CancellationToken ct) => await GetAllAuthorsAsync(mediator, ct)); endpoints.MapGet("/api/authors", GetAllAuthorsAsync);
endpoints.MapPost("/api/authors", CreateAuthorAsync); public static class ServiceCollectionExtensions
{
public static IServiceCollection AddApplicationServices(this IServiceCollection services,
WebApplicationBuilder builder)
{
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// ... public class AuthorModule : IModule
{
public IEndpointRouteBuilder RegisterEndpoints(IEndpointRouteBuilder endpoints)
{
endpoints.MapGet("/api/authors",
async (IMediator mediator, CancellationToken ct) => await GetAllAuthorsAsync(mediator, ct))
.WithName("GetAllAuthors")
.WithDisplayName("Authors")
.WithTags("Authors")
.Produces(500);
endpoints.MapPost("/api/authors",
async (IMediator mediator, AuthorDto authorDto, CancellationToken ct) =>
await CreateAuthorAsync(authorDto, mediator, ct))
.WithName("CreateAuthor")
.WithDisplayName("Authors")
.WithTags("Authors")
.Produces(500);
return endpoints;
} builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = builder.Environment.ApplicationName, Version = "v1" });
options.TagActionsBy(ta => new List<string> { ta.ActionDescriptor.DisplayName! });
});
namespace MinimalBlog.Api.Features.Authors;
public record AuthorGetDto
{
public int Id { get; init; }
public string Name { get; init; } = default!;
public string? Bio { get; init; }
public DateTime DateOfBirth { get; init; }
} public class AuthorProfile : Profile
{
public AuthorProfile()
{
CreateMap<AuthorDto, Author>().ReverseMap();
CreateMap<Author, AuthorGetDto>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.FullName));
}
} public class CreateAuthorCommand : IRequest<AuthorGetDto>
public class CreateAuthorCommandHandler : IRequestHandler<CreateAuthorCommand, AuthorGetDto>
public async Task<AuthorGetDto> Handle(CreateAuthorCommand request, CancellationToken cancellationToken)
return _mapper.Map<AuthorGetDto>(toAdd);
public class GetAllAuthorsQuery : IRequest<List<AuthorGetDto>>
public class GetAllAuthorsHandler : IRequestHandler<GetAllAuthorsQuery, List<AuthorGetDto>>
{
private readonly MinimalBlogDbContext _context;
private readonly IMapper _mapper;
public GetAllAuthorsHandler(MinimalBlogDbContext context, IMapper mapper)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}
public async Task<List<AuthorGetDto>> Handle(GetAllAuthorsQuery request, CancellationToken cancellationToken)
{
var authors = await _context.Authors.ToListAsync(cancellationToken);
return _mapper.Map<List<AuthorGetDto>>(authors);
}
} endpoints.MapGet("/api/authors",
async (IMediator mediator, CancellationToken ct) => await GetAllAuthorsAsync(mediator, ct))
.WithName("GetAllAuthors")
.WithDisplayName("Authors")
.WithTags("Authors")
.Produces<List<AuthorGetDto>>()
.Produces(500);
endpoints.MapPost("/api/authors",
async (IMediator mediator, AuthorDto authorDto, CancellationToken ct) =>
await CreateAuthorAsync(authorDto, mediator, ct))
.WithName("CreateAuthor")
.WithDisplayName("Authors")
.WithTags("Authors")
.Produces<AuthorGetDto>()
.Produces(500);
[HttpGet(Name = "GetWeatherForecast")]
public IResult Get()
{
return Results.Ok(new { Name = "My name" });
} {
"value": {
"name" : "My name"
},
"statusCode" : 200,
"contentType" : null
} {
"name" : "My name"
}