Skip to content

Commit 963c2d8

Browse files
committed
feat: Swagger examples
1 parent 5d5d4f1 commit 963c2d8

8 files changed

Lines changed: 85 additions & 66 deletions

File tree

ProjectTemplates/AspNetCore.WebApi/ReferenceProject/ReferenceProject/App_Start/Profiles/DefaultProfile.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class DefaultProfile: Profile
77
public DefaultProfile()
88
{
99
CreateMap<Model.Product, Dto.Product>();
10+
CreateMap<Dto.UpdateProduct, Model.Product>();
1011
// For copy creation
1112
CreateMap<Model.Product, Model.Product>();
1213
}

ProjectTemplates/AspNetCore.WebApi/ReferenceProject/ReferenceProject/Controllers/ProductsController.cs

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
namespace ReferenceProject.Controllers
1111
{
12+
/// <summary>
13+
/// Products Controller
14+
/// </summary>
1215
[Route("[controller]")]
1316
[ApiController]
1417
[Produces("application/json")]
@@ -26,18 +29,18 @@ public ProductsController(Repo.IProductsRepo productsRepo, IMapper mapper)
2629
/// <summary>
2730
/// Get all products
2831
/// </summary>
29-
/// <response code="200">List of all of the products</response>
32+
/// <response code="200">List of all products</response>
3033
[HttpGet]
3134
public IEnumerable<Dto.Product> Get()
3235
{
3336
return ProductsRepo.Get().Select(Mapper.Map<Dto.Product>);
3437
}
3538

3639
/// <summary>
37-
/// Get product by id
40+
/// Get a product by id
3841
/// </summary>
39-
/// <param name="id">Product id</param>
40-
/// <response code="200">Product with the specified id</response>
42+
/// <param name="id">A product id</param>
43+
[ProducesResponseType(typeof(Dto.Product), StatusCodes.Status200OK)]
4144
[ProducesResponseType(StatusCodes.Status404NotFound)]
4245
[HttpGet]
4346
[Route("{id}")]
@@ -46,6 +49,56 @@ public Dto.Product GetById(int id)
4649
return Mapper.Map<Dto.Product>(ProductsRepo.GetById(id));
4750
}
4851

52+
/// <summary>
53+
/// Create a new product
54+
/// </summary>
55+
/// <param name="id">A new product id</param>
56+
/// <param name="newProductDto">New product data</param>
57+
/// <response code="201">The created product</response>
58+
[ProducesResponseType(typeof(Dto.Product), StatusCodes.Status201Created)]
59+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
60+
[HttpPost]
61+
[Route("{id}")]
62+
public IActionResult Create(int id, [FromBody]Dto.UpdateProduct newProductDto)
63+
{
64+
var newProduct = new Model.Product(id);
65+
Mapper.Map(newProductDto, newProduct);
66+
ProductsRepo.Create(newProduct);
67+
return Created($"{id}", Mapper.Map<Dto.Product>(ProductsRepo.GetById(id)));
68+
}
69+
70+
/// <summary>
71+
/// Update a product
72+
/// </summary>
73+
/// <param name="id">Id of the product to update</param>
74+
/// <param name="productDto">Product data</param>
75+
[ProducesResponseType(StatusCodes.Status200OK)]
76+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
77+
[ProducesResponseType(StatusCodes.Status404NotFound)]
78+
[HttpPut]
79+
[Route("{id}")]
80+
public IActionResult Update(int id, [FromBody]Dto.UpdateProduct productDto)
81+
{
82+
var product = ProductsRepo.GetById(id);
83+
Mapper.Map(productDto, product);
84+
ProductsRepo.Update(product);
85+
return Ok();
86+
}
87+
88+
/// <summary>
89+
/// Delete a product
90+
/// </summary>
91+
/// <param name="id">Id of the product to delete</param>
92+
[ProducesResponseType(StatusCodes.Status200OK)]
93+
[ProducesResponseType(StatusCodes.Status404NotFound)]
94+
[HttpDelete]
95+
[Route("{id}")]
96+
public IActionResult Delete(int id)
97+
{
98+
ProductsRepo.Delete(id);
99+
return Ok();
100+
}
101+
49102
/// <summary>
50103
/// Example of an exception handling
51104
/// </summary>

ProjectTemplates/AspNetCore.WebApi/ReferenceProject/ReferenceProject/Controllers/ValuesController.cs

Lines changed: 0 additions & 56 deletions
This file was deleted.

ProjectTemplates/AspNetCore.WebApi/ReferenceProject/ReferenceProject/Dto/Product.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
namespace ReferenceProject.Dto
22
{
33
/// <summary>
4-
/// Model example: Product
4+
/// DTO for reading product (-s)
55
/// </summary>
66
public class Product
77
{
8+
/// <summary>
9+
/// Product id
10+
/// </summary>
11+
public int Id { get; set; }
12+
813
/// <summary>
914
/// Product name
1015
/// </summary>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace ReferenceProject.Dto
2+
{
3+
/// <summary>
4+
/// DTO for creating and updating product
5+
/// </summary>
6+
public class UpdateProduct
7+
{
8+
/// <summary>
9+
/// Product name
10+
/// </summary>
11+
public string Name { get; set; }
12+
}
13+
}

ProjectTemplates/AspNetCore.WebApi/ReferenceProject/ReferenceProject/Model/Product.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ public Product()
88
{
99
}
1010

11+
public Product(int id)
12+
{
13+
Id = id;
14+
}
15+
1116
public Product(int id, string name)
1217
{
1318
Id = id;

ProjectTemplates/AspNetCore.WebApi/ReferenceProject/ReferenceProject/Repo/ProductsRepo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void Create(Product p)
3434

3535
if (products.Any(x => x.Id == p.Id))
3636
{
37-
throw new DuplicateKeyException($"Can't create object of type {nameof(Product)} with the key '{p.Id}'. Object with the same key is already exists");
37+
throw new DuplicateKeyException($"Can't create an object of a type {nameof(Product)} with the key '{p.Id}'. The object with the same key is already exists");
3838
}
3939

4040
products.Add(Mapper.Map<Product>(p));
@@ -45,7 +45,7 @@ public void Delete(int id)
4545
var p = products.FirstOrDefault(x => x.Id == id);
4646
if (p == null)
4747
{
48-
throw new KeyNotFoundException($"Object of type '{nameof(Product)}' with the key '{id}' not found");
48+
throw new KeyNotFoundException($"An object of a type '{nameof(Product)}' with the key '{id}' not found");
4949
}
5050

5151
products.RemoveAll(x => x.Id == p.Id);
@@ -59,7 +59,7 @@ public void Update(Product p)
5959
var stored = products.FirstOrDefault(x => x.Id == p.Id);
6060
if (stored == null)
6161
{
62-
throw new KeyNotFoundException($"Object of type '{nameof(Product)}' with the key '{p.Id}' not found");
62+
throw new KeyNotFoundException($"An object of a type '{nameof(Product)}' with the key '{p.Id}' not found");
6363
}
6464

6565
products.RemoveAll(x => x.Id == stored.Id);

ProjectTemplates/AspNetCore.WebApi/ReferenceProject/ReferenceProject/Startup.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
using ReferenceProject.Modules;
1717
using System.IO;
1818

19-
// TODO: Create and validate configuration in web.config:
2019
// https://www.talkingdotnet.com/how-to-increase-file-upload-size-asp-net-core/
2120
// https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-2.1&tabs=aspnetcore2x#webconfig-file
22-
2321
namespace ReferenceProject
2422
{
2523
public class Startup

0 commit comments

Comments
 (0)