Skip to content

Commit 9b1947d

Browse files
committed
Update CategoriesController.cs
Iservicewrappper changed to Irepositorywrapper respective Dtos added
1 parent ad45dd5 commit 9b1947d

File tree

1 file changed

+51
-33
lines changed

1 file changed

+51
-33
lines changed

Supermarket/V1/Controllers/CategoriesController.cs

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
using Microsoft.AspNetCore.Authorization;
33
using Microsoft.AspNetCore.Http;
44
using Microsoft.AspNetCore.Mvc;
5+
using Supermarket.ApiResponse;
56
using Supermarket.Core.Models;
6-
using Supermarket.Domain.Services.Contracts;
7-
using Supermarket.Resources;
7+
using Supermarket.Core.Repositories.Contracts;
8+
using Supermarket.V1.Dtos.CategoryDtos;
89
using System.Collections.Generic;
910
using System.Threading.Tasks;
1011

@@ -16,68 +17,85 @@ namespace Supermarket.V1.Controllers
1617
[ApiController]
1718
public class CategoriesController : ControllerBase
1819
{
19-
private readonly IServiceWrapper _serviceWrapper;
20+
private readonly IRepositoryWrapper _repositoryWrapper;
2021
private readonly IMapper _mapper;
2122

22-
public CategoriesController(IServiceWrapper serviceWrapper, IMapper mapper)
23+
public CategoriesController(IRepositoryWrapper repositoryWrapper, IMapper mapper)
2324
{
24-
_serviceWrapper = serviceWrapper;
25+
_repositoryWrapper = repositoryWrapper;
2526
_mapper = mapper;
2627
}
2728

2829
[HttpGet]
2930
[ProducesResponseType(StatusCodes.Status200OK)]
3031
public async Task<IActionResult> GetAllAsync()
3132
{
32-
var categories = await _serviceWrapper.Category.ListAsync();
33+
var categories = await _repositoryWrapper.Category.ListAllCategoriesAsync();
3334

34-
var resources = _mapper.Map<IEnumerable<Category>, IEnumerable<CategoryResource>>(categories);
35+
var categoryDtos = _mapper.Map<IEnumerable<Category>, IEnumerable<CategoryDto>>(categories);
3536

36-
return Ok(resources);
37+
return Ok(categoryDtos);
3738
}
3839

39-
[HttpPost]
40+
[HttpGet("{id}", Name = "CategoryById")]
4041
[ProducesResponseType(StatusCodes.Status200OK)]
41-
[ProducesResponseType(StatusCodes.Status400BadRequest)]
42-
public async Task<IActionResult> PostAsync([FromBody] SaveCategoryResource resource)
42+
[ProducesResponseType(typeof(NotFoundResponse), StatusCodes.Status404NotFound)]
43+
[ProducesDefaultResponseType]
44+
public async Task<IActionResult> GetCategoryById(int id)
45+
{
46+
var dbCategory = await _repositoryWrapper.Category.FindById(id);
47+
if (dbCategory == null)
48+
return NotFound(new NotFoundResponse("Category not found"));
49+
50+
var categoryDto = _mapper.Map<Category, CategoryDto>(dbCategory);
51+
return Ok(categoryDto);
52+
}
53+
54+
[HttpPost]
55+
[ProducesResponseType(StatusCodes.Status201Created)]
56+
[ProducesDefaultResponseType]
57+
public async Task<IActionResult> PostAsync([FromBody] CreateCategoryDto createCategoryDto)
4358
{
44-
var category = _mapper.Map<SaveCategoryResource, Category>(resource);
45-
var result = await _serviceWrapper.Category.SaveAsync(category);
59+
var category = _mapper.Map<CreateCategoryDto, Category>(createCategoryDto);
60+
61+
await _repositoryWrapper.Category.AddCategoryAsync(category);
4662

47-
if (!result.Success)
48-
return BadRequest(result.Message);
63+
var categoryDto = _mapper.Map<Category, CategoryDto>(category);
4964

50-
var categoryResource = _mapper.Map<Category, CategoryResource>(result.Category);
51-
return Ok(categoryResource);
65+
return CreatedAtRoute("CategoryById", new { id = categoryDto.Id }, categoryDto);
5266
}
5367

5468
[HttpPut("{id}")]
55-
[ProducesResponseType(StatusCodes.Status200OK)]
56-
[ProducesResponseType(StatusCodes.Status400BadRequest)]
57-
public async Task<IActionResult> PutAsync(int id, [FromBody] SaveCategoryResource resource)
69+
[ProducesResponseType(StatusCodes.Status204NoContent)]
70+
[ProducesResponseType(typeof(NotFoundResponse), StatusCodes.Status404NotFound)]
71+
[ProducesDefaultResponseType]
72+
public async Task<IActionResult> PutAsync(int id, [FromBody] SaveCategoryDto saveCategoryDto)
5873
{
59-
var category = _mapper.Map<SaveCategoryResource, Category>(resource);
60-
var result = await _serviceWrapper.Category.UpdateAsync(id, category);
74+
var dbCategory = await _repositoryWrapper.Category.FindById(id);
75+
if (dbCategory == null)
76+
return NotFound(new NotFoundResponse("Category not found"));
6177

62-
if (!result.Success)
63-
return BadRequest(result.Message);
78+
dbCategory.Name = saveCategoryDto.Name;
79+
dbCategory.DateModified = saveCategoryDto.DateModified;
6480

65-
var categoryResource = _mapper.Map<Category, CategoryResource>(result.Category);
66-
return Ok(categoryResource);
81+
await _repositoryWrapper.Category.UpdateCategoryAsync(dbCategory);
82+
83+
return NoContent();
6784
}
6885

6986
[HttpDelete("{id}")]
70-
[ProducesResponseType(StatusCodes.Status200OK)]
71-
[ProducesResponseType(StatusCodes.Status400BadRequest)]
87+
[ProducesResponseType(StatusCodes.Status204NoContent)]
88+
[ProducesResponseType(typeof(NotFoundResponse), StatusCodes.Status404NotFound)]
89+
[ProducesDefaultResponseType]
7290
public async Task<IActionResult> DeleteAsync(int id)
7391
{
74-
var result = await _serviceWrapper.Category.DeleteAsync(id);
92+
var dbCategory = await _repositoryWrapper.Category.FindById(id);
93+
if (dbCategory == null)
94+
return NotFound(new NotFoundResponse("Category not found"));
7595

76-
if (!result.Success)
77-
return BadRequest(result.Message);
96+
await _repositoryWrapper.Category.DeleteCategoryAsync(dbCategory);
7897

79-
var categoryResource = _mapper.Map<Category, CategoryResource>(result.Category);
80-
return Ok(categoryResource);
98+
return NoContent();
8199
}
82100
}
83101
}

0 commit comments

Comments
 (0)