-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from evgomes/net-7-update
.NET version update and code refactoring
- Loading branch information
Showing
43 changed files
with
521 additions
and
590 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,5 @@ namespace Supermarket.API.Controllers | |
[ApiController] | ||
public class BaseApiController : ControllerBase | ||
{ | ||
|
||
} | ||
} |
161 changes: 78 additions & 83 deletions
161
src/Supermarket.API/Controllers/CategoriesController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,97 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using AutoMapper; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Supermarket.API.Domain.Models; | ||
using Supermarket.API.Domain.Services; | ||
using Supermarket.API.Resources; | ||
|
||
namespace Supermarket.API.Controllers | ||
{ | ||
public class CategoriesController : BaseApiController | ||
{ | ||
private readonly ICategoryService _categoryService; | ||
private readonly IMapper _mapper; | ||
public class CategoriesController : BaseApiController | ||
{ | ||
private readonly ICategoryService _categoryService; | ||
private readonly IMapper _mapper; | ||
|
||
public CategoriesController(ICategoryService categoryService, IMapper mapper) | ||
{ | ||
_categoryService = categoryService; | ||
_mapper = mapper; | ||
} | ||
public CategoriesController(ICategoryService categoryService, IMapper mapper) | ||
{ | ||
_categoryService = categoryService; | ||
_mapper = mapper; | ||
} | ||
|
||
/// <summary> | ||
/// Lists all categories. | ||
/// </summary> | ||
/// <returns>List os categories.</returns> | ||
[HttpGet] | ||
[ProducesResponseType(typeof(IEnumerable<CategoryResource>), 200)] | ||
public async Task<IEnumerable<CategoryResource>> ListAsync() | ||
{ | ||
var categories = await _categoryService.ListAsync(); | ||
var resources = _mapper.Map<IEnumerable<Category>, IEnumerable<CategoryResource>>(categories); | ||
/// <summary> | ||
/// Lists all categories. | ||
/// </summary> | ||
/// <returns>List os categories.</returns> | ||
[HttpGet] | ||
[ProducesResponseType(typeof(IEnumerable<CategoryResource>), 200)] | ||
public async Task<IEnumerable<CategoryResource>> ListAsync() | ||
{ | ||
var categories = await _categoryService.ListAsync(); | ||
return _mapper.Map<IEnumerable<CategoryResource>>(categories); | ||
} | ||
|
||
return resources; | ||
} | ||
/// <summary> | ||
/// Saves a new category. | ||
/// </summary> | ||
/// <param name="resource">Category data.</param> | ||
/// <returns>Response for the request.</returns> | ||
[HttpPost] | ||
[ProducesResponseType(typeof(CategoryResource), 201)] | ||
[ProducesResponseType(typeof(ErrorResource), 400)] | ||
public async Task<IActionResult> PostAsync([FromBody] SaveCategoryResource resource) | ||
{ | ||
var category = _mapper.Map<Category>(resource); | ||
var result = await _categoryService.SaveAsync(category); | ||
|
||
/// <summary> | ||
/// Saves a new category. | ||
/// </summary> | ||
/// <param name="resource">Category data.</param> | ||
/// <returns>Response for the request.</returns> | ||
[HttpPost] | ||
[ProducesResponseType(typeof(CategoryResource), 201)] | ||
[ProducesResponseType(typeof(ErrorResource), 400)] | ||
public async Task<IActionResult> PostAsync([FromBody] SaveCategoryResource resource) | ||
{ | ||
var category = _mapper.Map<SaveCategoryResource, Category>(resource); | ||
var result = await _categoryService.SaveAsync(category); | ||
if (!result.Success) | ||
{ | ||
return BadRequest(new ErrorResource(result.Message!)); | ||
} | ||
|
||
if (!result.Success) | ||
{ | ||
return BadRequest(new ErrorResource(result.Message)); | ||
} | ||
var categoryResource = _mapper.Map<CategoryResource>(result.Resource!); | ||
return Ok(categoryResource); | ||
} | ||
|
||
var categoryResource = _mapper.Map<Category, CategoryResource>(result.Resource); | ||
return Ok(categoryResource); | ||
} | ||
/// <summary> | ||
/// Updates an existing category according to an identifier. | ||
/// </summary> | ||
/// <param name="id">Category identifier.</param> | ||
/// <param name="resource">Updated category data.</param> | ||
/// <returns>Response for the request.</returns> | ||
[HttpPut("{id}")] | ||
[ProducesResponseType(typeof(CategoryResource), 200)] | ||
[ProducesResponseType(typeof(ErrorResource), 400)] | ||
public async Task<IActionResult> PutAsync(int id, [FromBody] SaveCategoryResource resource) | ||
{ | ||
var category = _mapper.Map<Category>(resource); | ||
var result = await _categoryService.UpdateAsync(id, category); | ||
|
||
/// <summary> | ||
/// Updates an existing category according to an identifier. | ||
/// </summary> | ||
/// <param name="id">Category identifier.</param> | ||
/// <param name="resource">Updated category data.</param> | ||
/// <returns>Response for the request.</returns> | ||
[HttpPut("{id}")] | ||
[ProducesResponseType(typeof(CategoryResource), 200)] | ||
[ProducesResponseType(typeof(ErrorResource), 400)] | ||
public async Task<IActionResult> PutAsync(int id, [FromBody] SaveCategoryResource resource) | ||
{ | ||
var category = _mapper.Map<SaveCategoryResource, Category>(resource); | ||
var result = await _categoryService.UpdateAsync(id, category); | ||
if (!result.Success) | ||
{ | ||
return BadRequest(new ErrorResource(result.Message!)); | ||
} | ||
|
||
if (!result.Success) | ||
{ | ||
return BadRequest(new ErrorResource(result.Message)); | ||
} | ||
var categoryResource = _mapper.Map<CategoryResource>(result.Resource!); | ||
return Ok(categoryResource); | ||
} | ||
|
||
var categoryResource = _mapper.Map<Category, CategoryResource>(result.Resource); | ||
return Ok(categoryResource); | ||
} | ||
/// <summary> | ||
/// Deletes a given category according to an identifier. | ||
/// </summary> | ||
/// <param name="id">Category identifier.</param> | ||
/// <returns>Response for the request.</returns> | ||
[HttpDelete("{id}")] | ||
[ProducesResponseType(typeof(CategoryResource), 200)] | ||
[ProducesResponseType(typeof(ErrorResource), 400)] | ||
public async Task<IActionResult> DeleteAsync(int id) | ||
{ | ||
var result = await _categoryService.DeleteAsync(id); | ||
|
||
/// <summary> | ||
/// Deletes a given category according to an identifier. | ||
/// </summary> | ||
/// <param name="id">Category identifier.</param> | ||
/// <returns>Response for the request.</returns> | ||
[HttpDelete("{id}")] | ||
[ProducesResponseType(typeof(CategoryResource), 200)] | ||
[ProducesResponseType(typeof(ErrorResource), 400)] | ||
public async Task<IActionResult> DeleteAsync(int id) | ||
{ | ||
var result = await _categoryService.DeleteAsync(id); | ||
if (!result.Success) | ||
{ | ||
return BadRequest(new ErrorResource(result.Message!)); | ||
} | ||
|
||
if (!result.Success) | ||
{ | ||
return BadRequest(new ErrorResource(result.Message)); | ||
} | ||
|
||
var categoryResource = _mapper.Map<Category, CategoryResource>(result.Resource); | ||
return Ok(categoryResource); | ||
} | ||
} | ||
var categoryResource = _mapper.Map<CategoryResource>(result.Resource!); | ||
return Ok(categoryResource); | ||
} | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
src/Supermarket.API/Controllers/Config/InvalidModelStateResponseFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Supermarket.API.Domain.Models | ||
{ | ||
public class Category | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
public IList<Product> Products { get; set; } = new List<Product>(); | ||
} | ||
public class Category | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } = null!; | ||
public List<Product> Products { get; set; } = new(); | ||
} | ||
} |
Oops, something went wrong.