2
2
using Microsoft . AspNetCore . Authorization ;
3
3
using Microsoft . AspNetCore . Http ;
4
4
using Microsoft . AspNetCore . Mvc ;
5
+ using Supermarket . ApiResponse ;
5
6
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 ;
8
9
using System . Collections . Generic ;
9
10
using System . Threading . Tasks ;
10
11
@@ -16,68 +17,85 @@ namespace Supermarket.V1.Controllers
16
17
[ ApiController ]
17
18
public class CategoriesController : ControllerBase
18
19
{
19
- private readonly IServiceWrapper _serviceWrapper ;
20
+ private readonly IRepositoryWrapper _repositoryWrapper ;
20
21
private readonly IMapper _mapper ;
21
22
22
- public CategoriesController ( IServiceWrapper serviceWrapper , IMapper mapper )
23
+ public CategoriesController ( IRepositoryWrapper repositoryWrapper , IMapper mapper )
23
24
{
24
- _serviceWrapper = serviceWrapper ;
25
+ _repositoryWrapper = repositoryWrapper ;
25
26
_mapper = mapper ;
26
27
}
27
28
28
29
[ HttpGet ]
29
30
[ ProducesResponseType ( StatusCodes . Status200OK ) ]
30
31
public async Task < IActionResult > GetAllAsync ( )
31
32
{
32
- var categories = await _serviceWrapper . Category . ListAsync ( ) ;
33
+ var categories = await _repositoryWrapper . Category . ListAllCategoriesAsync ( ) ;
33
34
34
- var resources = _mapper . Map < IEnumerable < Category > , IEnumerable < CategoryResource > > ( categories ) ;
35
+ var categoryDtos = _mapper . Map < IEnumerable < Category > , IEnumerable < CategoryDto > > ( categories ) ;
35
36
36
- return Ok ( resources ) ;
37
+ return Ok ( categoryDtos ) ;
37
38
}
38
39
39
- [ HttpPost ]
40
+ [ HttpGet ( "{id}" , Name = "CategoryById" ) ]
40
41
[ 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 )
43
58
{
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 ) ;
46
62
47
- if ( ! result . Success )
48
- return BadRequest ( result . Message ) ;
63
+ var categoryDto = _mapper . Map < Category , CategoryDto > ( category ) ;
49
64
50
- var categoryResource = _mapper . Map < Category , CategoryResource > ( result . Category ) ;
51
- return Ok ( categoryResource ) ;
65
+ return CreatedAtRoute ( "CategoryById" , new { id = categoryDto . Id } , categoryDto ) ;
52
66
}
53
67
54
68
[ 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 )
58
73
{
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" ) ) ;
61
77
62
- if ( ! result . Success )
63
- return BadRequest ( result . Message ) ;
78
+ dbCategory . Name = saveCategoryDto . Name ;
79
+ dbCategory . DateModified = saveCategoryDto . DateModified ;
64
80
65
- var categoryResource = _mapper . Map < Category , CategoryResource > ( result . Category ) ;
66
- return Ok ( categoryResource ) ;
81
+ await _repositoryWrapper . Category . UpdateCategoryAsync ( dbCategory ) ;
82
+
83
+ return NoContent ( ) ;
67
84
}
68
85
69
86
[ HttpDelete ( "{id}" ) ]
70
- [ ProducesResponseType ( StatusCodes . Status200OK ) ]
71
- [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
87
+ [ ProducesResponseType ( StatusCodes . Status204NoContent ) ]
88
+ [ ProducesResponseType ( typeof ( NotFoundResponse ) , StatusCodes . Status404NotFound ) ]
89
+ [ ProducesDefaultResponseType ]
72
90
public async Task < IActionResult > DeleteAsync ( int id )
73
91
{
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" ) ) ;
75
95
76
- if ( ! result . Success )
77
- return BadRequest ( result . Message ) ;
96
+ await _repositoryWrapper . Category . DeleteCategoryAsync ( dbCategory ) ;
78
97
79
- var categoryResource = _mapper . Map < Category , CategoryResource > ( result . Category ) ;
80
- return Ok ( categoryResource ) ;
98
+ return NoContent ( ) ;
81
99
}
82
100
}
83
101
}
0 commit comments