33using Microsoft . AspNetCore . Mvc ;
44using NorthWindAPI . Controllers . Models . Requests ;
55using NorthWindAPI . Controllers . Models . Responses ;
6+ using NorthWindAPI . Infrastructure . Exceptions . Base ;
7+ using NorthWindAPI . Infrastructure . Exceptions . Repository ;
68using NorthWindAPI . Services . Interfaces ;
79using NorthWindAPI . Services . ResponseDto ;
810
@@ -26,14 +28,16 @@ public class CustomerController(ICustomerService customerService, IMapper mapper
2628 [ ProducesResponseType ( typeof ( UnauthorizedResult ) , StatusCodes . Status401Unauthorized ) ]
2729 public async Task < ActionResult < IEnumerable < CustomerResponse > > > All ( )
2830 {
29- var customers = await _customerService . ListCustomers ( ) ;
30- if ( customers == null || ! customers . Any ( ) )
31+ try
3132 {
32- return NotFound ( ) ;
33+ var customers = await _customerService . ListCustomers ( ) ;
34+ var response = _mapper . Map < IEnumerable < CustomerResponse > > ( customers ) ;
35+ return response . ToList ( ) ;
36+ }
37+ catch ( CustomerNotFoundException ex )
38+ {
39+ return NotFound ( ex . Message ) ;
3340 }
34-
35- var response = _mapper . Map < IEnumerable < CustomerResponse > > ( customers ) ;
36- return response . ToList ( ) ;
3741 }
3842
3943 /// <summary>
@@ -47,14 +51,16 @@ public async Task<ActionResult<IEnumerable<CustomerResponse>>> All()
4751 [ ProducesResponseType ( typeof ( UnauthorizedResult ) , StatusCodes . Status401Unauthorized ) ]
4852 public async Task < ActionResult < CustomerResponse > > Find ( string id )
4953 {
50- var customer = await _customerService . FindCustomer ( id ) ;
51- if ( customer == null )
54+ try
5255 {
53- return NotFound ( ) ;
56+ var customer = await _customerService . FindCustomer ( id ) ;
57+ var response = _mapper . Map < CustomerResponse > ( customer ) ;
58+ return response ;
59+ }
60+ catch ( CustomerNotFoundException ex )
61+ {
62+ return NotFound ( ex . Message ) ;
5463 }
55-
56- var response = _mapper . Map < CustomerResponse > ( customer ) ;
57- return response ;
5864 }
5965
6066 /// <summary>
@@ -67,14 +73,16 @@ public async Task<ActionResult<CustomerResponse>> Find(string id)
6773 [ ProducesResponseType ( typeof ( UnauthorizedResult ) , StatusCodes . Status401Unauthorized ) ]
6874 public async Task < ActionResult < RegionsResponse > > Regions ( )
6975 {
70- var customerRegions = await _customerService . CustomerRegions ( ) ;
71- if ( customerRegions == null || ! customerRegions . Any ( ) )
76+ try
7277 {
73- return NotFound ( ) ;
78+ var customerRegions = await _customerService . CustomerRegions ( ) ;
79+ RegionsResponse response = new ( ) { Regions = customerRegions } ;
80+ return response ;
81+ }
82+ catch ( CustomerNotFoundException ex )
83+ {
84+ return NotFound ( ex . Message ) ;
7485 }
75-
76- RegionsResponse response = new ( ) { Regions = customerRegions } ;
77- return response ;
7886 }
7987
8088 /// <summary>
@@ -89,23 +97,21 @@ public async Task<ActionResult<RegionsResponse>> Regions()
8997 [ HttpPost ]
9098 public async Task < ActionResult < CustomerResponse > > Create ( CustomerRequest newCustomer )
9199 {
92- var customer = new CustomerDto ( ) ;
93100 try
94101 {
95- customer = await _customerService . ProcessNewCustomer ( newCustomer ) ;
102+ var customer = await _customerService . ProcessNewCustomer ( newCustomer ) ;
96103
97- if ( customer == null )
98- {
99- return BadRequest ( ) ;
100- }
104+ var response = _mapper . Map < CustomerResponse > ( customer ) ;
105+ return response ;
101106 }
102- catch
107+ catch ( CustomerNotCreatedException ex )
103108 {
104- return Forbid ( ) ;
109+ return BadRequest ( ex . Message ) ;
110+ }
111+ catch ( Exception ex )
112+ {
113+ return Forbid ( ex . Message ) ;
105114 }
106-
107- var response = _mapper . Map < CustomerResponse > ( customer ) ;
108- return response ;
109115 }
110116
111117 /// <summary>
@@ -121,26 +127,30 @@ public async Task<ActionResult<CustomerResponse>> Create(CustomerRequest newCust
121127 [ ProducesResponseType ( typeof ( UnauthorizedResult ) , StatusCodes . Status401Unauthorized ) ]
122128 public async Task < ActionResult < CustomerResponse > > Update ( string id , CustomerRequest existingCustomer )
123129 {
124- var customer = new CustomerDto ( ) ;
125-
126130 try
127131 {
128132 var toUpdate = _mapper . Map < CustomerDto > ( existingCustomer ) ;
133+ var customer = await _customerService . Update ( id , toUpdate ) ;
129134
130- if ( id != toUpdate . Id )
131- {
132- return BadRequest ( ) ;
133- }
134-
135- customer = await _customerService . Update ( id , toUpdate ) ;
135+ var response = _mapper . Map < CustomerResponse > ( customer ) ;
136+ return response ;
136137 }
137- catch
138+ catch ( CustomerNotFoundException ex )
138139 {
139- return Forbid ( ) ;
140+ return NotFound ( ex . Message ) ;
141+ }
142+ catch ( CustomerNotUpdatedException ex )
143+ {
144+ return BadRequest ( ex . Message ) ;
145+ }
146+ catch ( EntityIdentifierMismatchException ex )
147+ {
148+ return BadRequest ( ex . Message ) ;
149+ }
150+ catch ( Exception ex )
151+ {
152+ return Forbid ( ex . Message ) ;
140153 }
141-
142- var response = _mapper . Map < CustomerResponse > ( customer ) ;
143- return response ;
144154 }
145155 }
146156}
0 commit comments