diff --git a/OrderTrackingApp.Api/Controllers/OrdersController.cs b/OrderTrackingApp.Api/Controllers/OrdersController.cs index 13dd792..4778258 100644 --- a/OrderTrackingApp.Api/Controllers/OrdersController.cs +++ b/OrderTrackingApp.Api/Controllers/OrdersController.cs @@ -1,8 +1,10 @@ using AutoMapper; using MediatR; using Microsoft.AspNetCore.Mvc; +using OrderTrackingApp.Api.Examples; using OrderTrackingApp.Api.Models; using OrderTrackingApp.Application.Commands.Orders; +using Swashbuckle.AspNetCore.Filters; namespace OrderTrackingApp.Api.Controllers { @@ -11,6 +13,9 @@ namespace OrderTrackingApp.Api.Controllers public class OrdersController(IMediator mediator, IMapper mapper) : ControllerBase { [HttpPost] + + [SwaggerRequestExample(typeof(CreateOrderRequest), typeof(CreateOrderRequestExample))] + [ProducesResponseType(StatusCodes.Status201Created)] public async Task CreateOrder([FromBody] CreateOrderRequest request) { var command = mapper.Map(request); diff --git a/OrderTrackingApp.Api/Examples/CreateOrderRequestExample.cs b/OrderTrackingApp.Api/Examples/CreateOrderRequestExample.cs new file mode 100644 index 0000000..bcbc646 --- /dev/null +++ b/OrderTrackingApp.Api/Examples/CreateOrderRequestExample.cs @@ -0,0 +1,28 @@ +using OrderTrackingApp.Api.Models; +using Swashbuckle.AspNetCore.Filters; + +namespace OrderTrackingApp.Api.Examples; + +public class CreateOrderRequestExample : IExamplesProvider +{ + public CreateOrderRequest GetExamples() + { + return new CreateOrderRequest + { + CustomerId = Guid.NewGuid(), + Items = new List + { + new CreateOrderItemRequest + { + ProductId = Guid.Parse("b111a4dd-f45c-4d40-a6a3-3002f62f823f"), + Quantity = 2 + }, + new CreateOrderItemRequest + { + ProductId = Guid.Parse("2a13551b-059c-4035-b275-8a8cb82bd272"), + Quantity = 3 + } + } + }; + } +} diff --git a/OrderTrackingApp.Api/Extensions/ApiServiceRegistrations.cs b/OrderTrackingApp.Api/Extensions/ApiServiceRegistrations.cs index d0d3e4f..aaa2245 100644 --- a/OrderTrackingApp.Api/Extensions/ApiServiceRegistrations.cs +++ b/OrderTrackingApp.Api/Extensions/ApiServiceRegistrations.cs @@ -1,6 +1,8 @@ using FluentValidation; +using OrderTrackingApp.Api.Examples; using OrderTrackingApp.Api.MappingProfiles; using OrderTrackingApp.Api.Validators; +using Swashbuckle.AspNetCore.Filters; using System.Text.Json; namespace OrderTrackingApp.Api.Extensions @@ -10,7 +12,12 @@ public static class ApiServiceRegistrations public static IServiceCollection AddApiServices(this IServiceCollection services) { services.AddEndpointsApiExplorer(); - services.AddSwaggerGen(); + + services.AddSwaggerGen(options => { + options.ExampleFilters(); + }); + + services.AddSwaggerExamplesFromAssemblyOf(); services.AddControllers() .AddJsonOptions(options => diff --git a/OrderTrackingApp.Api/OrderTrackingApp.Api.csproj b/OrderTrackingApp.Api/OrderTrackingApp.Api.csproj index 2dfe61f..a6aebfd 100644 --- a/OrderTrackingApp.Api/OrderTrackingApp.Api.csproj +++ b/OrderTrackingApp.Api/OrderTrackingApp.Api.csproj @@ -15,6 +15,7 @@ + diff --git a/OrderTrackingApp.Api/WeatherForecast.cs b/OrderTrackingApp.Api/WeatherForecast.cs deleted file mode 100644 index f9643e9..0000000 --- a/OrderTrackingApp.Api/WeatherForecast.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace OrderTrackingApp.Api -{ - public class WeatherForecast - { - public DateOnly Date { get; set; } - - public int TemperatureC { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - - public string? Summary { get; set; } - } -}