1- using AutoMapper ;
2- using Contracts ;
3- using Entities . DataTransferObjects ;
1+ using Contracts ;
2+ using Entities . Extensions ;
43using Entities . Models ;
54using Microsoft . AspNetCore . Mvc ;
65using System ;
7- using System . Collections . Generic ;
8- using System . Linq ;
96
107namespace AccountOwnerServer . Controllers
118{
12- [ Route ( "api/owner" ) ]
13- [ ApiController ]
14- public class OwnerController : ControllerBase
15- {
16- private ILoggerManager _logger ;
9+ [ Route ( "api/owner" ) ]
10+ public class OwnerController : Controller
11+ {
12+ private ILoggerManager _logger ;
1713 private IRepositoryWrapper _repository ;
18- private IMapper _mapper ;
19-
20- public OwnerController ( ILoggerManager logger , IRepositoryWrapper repository , IMapper mapper )
21- {
22- _logger = logger ;
14+
15+ public OwnerController ( ILoggerManager logger , IRepositoryWrapper repository )
16+ {
17+ _logger = logger ;
2318 _repository = repository ;
24- _mapper = mapper ;
2519 }
26-
27- [ HttpGet ]
28- public IActionResult GetAllOwners ( )
29- {
30- try
31- {
32- var owners = _repository . Owner . GetAllOwners ( ) ;
20+
21+ [ HttpGet ]
22+ public IActionResult GetAllOwners ( )
23+ {
24+ try
25+ {
26+ var owners = _repository . Owner . GetAllOwners ( ) ;
27+
3328 _logger . LogInfo ( $ "Returned all owners from database.") ;
3429
35- var ownersResult = _mapper . Map < IEnumerable < OwnerDto > > ( owners ) ;
36- return Ok ( ownersResult ) ;
37- }
38- catch ( Exception ex )
39- {
40- _logger . LogError ( $ "Something went wrong inside GetAllOwners action: { ex . Message } ") ;
41- return StatusCode ( 500 , "Internal server error" ) ;
42- }
30+ return Ok ( owners ) ;
31+ }
32+ catch ( Exception ex )
33+ {
34+ _logger . LogError ( $ "Something went wrong inside GetAllOwners action: { ex . Message } ") ;
35+ return StatusCode ( 500 , "Internal server error" ) ;
36+ }
4337 }
4438
45- [ HttpGet ( "{id}" , Name = "OwnerById" ) ]
46- public IActionResult GetOwnerById ( Guid id )
47- {
48- try
49- {
50- var owner = _repository . Owner . GetOwnerById ( id ) ;
51- if ( owner == null )
52- {
53- _logger . LogError ( $ "Owner with id: { id } , hasn't been found in db.") ;
54- return NotFound ( ) ;
55- }
56- else
57- {
58- _logger . LogInfo ( $ "Returned owner with id: { id } ") ;
39+ [ HttpGet ( "{id}" , Name = "OwnerById" ) ]
40+ public IActionResult GetOwnerById ( Guid id )
41+ {
42+ try
43+ {
44+ var owner = _repository . Owner . GetOwnerById ( id ) ;
5945
60- var ownerResult = _mapper . Map < OwnerDto > ( owner ) ;
61- return Ok ( ownerResult ) ;
62- }
63- }
64- catch ( Exception ex )
65- {
66- _logger . LogError ( $ "Something went wrong inside GetOwnerById action: { ex . Message } ") ;
67- return StatusCode ( 500 , "Internal server error" ) ;
68- }
46+ if ( owner . IsEmptyObject ( ) )
47+ {
48+ _logger . LogError ( $ "Owner with id: { id } , hasn't been found in db.") ;
49+ return NotFound ( ) ;
50+ }
51+ else
52+ {
53+ _logger . LogInfo ( $ "Returned owner with id: { id } ") ;
54+ return Ok ( owner ) ;
55+ }
56+ }
57+ catch ( Exception ex )
58+ {
59+ _logger . LogError ( $ "Something went wrong inside GetOwnerById action: { ex . Message } ") ;
60+ return StatusCode ( 500 , "Internal server error" ) ;
61+ }
6962 }
7063
71- [ HttpGet ( "{id}/account" ) ]
72- public IActionResult GetOwnerWithDetails ( Guid id )
73- {
74- try
75- {
76- var owner = _repository . Owner . GetOwnerWithDetails ( id ) ;
77- if ( owner == null )
78- {
79- _logger . LogError ( $ "Owner with id: { id } , hasn't been found in db.") ;
80- return NotFound ( ) ;
81- }
82- else
83- {
84- _logger . LogInfo ( $ "Returned owner with details for id: { id } ") ;
64+ [ HttpGet ( "{id}/account" ) ]
65+ public IActionResult GetOwnerWithDetails ( Guid id )
66+ {
67+ try
68+ {
69+ var owner = _repository . Owner . GetOwnerWithDetails ( id ) ;
8570
86- var ownerResult = _mapper . Map < OwnerDto > ( owner ) ;
87- return Ok ( ownerResult ) ;
88- }
89- }
90- catch ( Exception ex )
91- {
92- _logger . LogError ( $ "Something went wrong inside GetOwnerWithDetails action: { ex . Message } ") ;
93- return StatusCode ( 500 , "Internal server error" ) ;
71+ if ( owner . IsEmptyObject ( ) )
72+ {
73+ _logger . LogError ( $ "Owner with id: { id } , hasn't been found in db.") ;
74+ return NotFound ( ) ;
75+ }
76+ else
77+ {
78+ _logger . LogInfo ( $ "Returned owner with details for id: { id } ") ;
79+ return Ok ( owner ) ;
80+ }
81+ }
82+ catch ( Exception ex )
83+ {
84+ _logger . LogError ( $ "Something went wrong inside GetOwnerWithDetails action: { ex . Message } ") ;
85+ return StatusCode ( 500 , "Internal server error" ) ;
9486 }
9587 }
9688
9789 [ HttpPost ]
98- public IActionResult CreateOwner ( [ FromBody ] OwnerForCreationDto owner )
90+ public IActionResult CreateOwner ( [ FromBody ] Owner owner )
9991 {
10092 try
10193 {
102- if ( owner == null )
94+ if ( owner . IsObjectNull ( ) )
10395 {
10496 _logger . LogError ( "Owner object sent from client is null." ) ;
10597 return BadRequest ( "Owner object is null" ) ;
@@ -111,14 +103,9 @@ public IActionResult CreateOwner([FromBody]OwnerForCreationDto owner)
111103 return BadRequest ( "Invalid model object" ) ;
112104 }
113105
114- var ownerEntity = _mapper . Map < Owner > ( owner ) ;
115-
116- _repository . Owner . CreateOwner ( ownerEntity ) ;
117- _repository . Save ( ) ;
106+ _repository . Owner . CreateOwner ( owner ) ;
118107
119- var createdOwner = _mapper . Map < OwnerDto > ( ownerEntity ) ;
120-
121- return CreatedAtRoute ( "OwnerById" , new { id = createdOwner . Id } , createdOwner ) ;
108+ return CreatedAtRoute ( "OwnerById" , new { id = owner . Id } , owner ) ;
122109 }
123110 catch ( Exception ex )
124111 {
@@ -128,11 +115,11 @@ public IActionResult CreateOwner([FromBody]OwnerForCreationDto owner)
128115 }
129116
130117 [ HttpPut ( "{id}" ) ]
131- public IActionResult UpdateOwner ( Guid id , [ FromBody ] OwnerForUpdateDto owner )
118+ public IActionResult UpdateOwner ( Guid id , [ FromBody ] Owner owner )
132119 {
133120 try
134121 {
135- if ( owner == null )
122+ if ( owner . IsObjectNull ( ) )
136123 {
137124 _logger . LogError ( "Owner object sent from client is null." ) ;
138125 return BadRequest ( "Owner object is null" ) ;
@@ -144,17 +131,14 @@ public IActionResult UpdateOwner(Guid id, [FromBody]OwnerForUpdateDto owner)
144131 return BadRequest ( "Invalid model object" ) ;
145132 }
146133
147- var ownerEntity = _repository . Owner . GetOwnerById ( id ) ;
148- if ( ownerEntity == null )
134+ var dbOwner = _repository . Owner . GetOwnerById ( id ) ;
135+ if ( dbOwner . IsEmptyObject ( ) )
149136 {
150137 _logger . LogError ( $ "Owner with id: { id } , hasn't been found in db.") ;
151138 return NotFound ( ) ;
152139 }
153140
154- _mapper . Map ( owner , ownerEntity ) ;
155-
156- _repository . Owner . UpdateOwner ( ownerEntity ) ;
157- _repository . Save ( ) ;
141+ _repository . Owner . UpdateOwner ( dbOwner , owner ) ;
158142
159143 return NoContent ( ) ;
160144 }
@@ -171,14 +155,13 @@ public IActionResult DeleteOwner(Guid id)
171155 try
172156 {
173157 var owner = _repository . Owner . GetOwnerById ( id ) ;
174- if ( owner == null )
158+ if ( owner . IsEmptyObject ( ) )
175159 {
176160 _logger . LogError ( $ "Owner with id: { id } , hasn't been found in db.") ;
177161 return NotFound ( ) ;
178162 }
179163
180164 _repository . Owner . DeleteOwner ( owner ) ;
181- _repository . Save ( ) ;
182165
183166 return NoContent ( ) ;
184167 }
@@ -189,4 +172,4 @@ public IActionResult DeleteOwner(Guid id)
189172 }
190173 }
191174 }
192- }
175+ }
0 commit comments