1
- using AutoMapper ;
2
- using Contracts ;
3
- using Entities . DataTransferObjects ;
1
+ using Contracts ;
2
+ using Entities . Extensions ;
4
3
using Entities . Models ;
5
4
using Microsoft . AspNetCore . Mvc ;
6
5
using System ;
7
- using System . Collections . Generic ;
8
- using System . Linq ;
9
6
10
7
namespace AccountOwnerServer . Controllers
11
8
{
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 ;
17
13
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 ;
23
18
_repository = repository ;
24
- _mapper = mapper ;
25
19
}
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
+
33
28
_logger . LogInfo ( $ "Returned all owners from database.") ;
34
29
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
+ }
43
37
}
44
38
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 ) ;
59
45
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
+ }
69
62
}
70
63
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 ) ;
85
70
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" ) ;
94
86
}
95
87
}
96
88
97
89
[ HttpPost ]
98
- public IActionResult CreateOwner ( [ FromBody ] OwnerForCreationDto owner )
90
+ public IActionResult CreateOwner ( [ FromBody ] Owner owner )
99
91
{
100
92
try
101
93
{
102
- if ( owner == null )
94
+ if ( owner . IsObjectNull ( ) )
103
95
{
104
96
_logger . LogError ( "Owner object sent from client is null." ) ;
105
97
return BadRequest ( "Owner object is null" ) ;
@@ -111,14 +103,9 @@ public IActionResult CreateOwner([FromBody]OwnerForCreationDto owner)
111
103
return BadRequest ( "Invalid model object" ) ;
112
104
}
113
105
114
- var ownerEntity = _mapper . Map < Owner > ( owner ) ;
115
-
116
- _repository . Owner . CreateOwner ( ownerEntity ) ;
117
- _repository . Save ( ) ;
106
+ _repository . Owner . CreateOwner ( owner ) ;
118
107
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 ) ;
122
109
}
123
110
catch ( Exception ex )
124
111
{
@@ -128,11 +115,11 @@ public IActionResult CreateOwner([FromBody]OwnerForCreationDto owner)
128
115
}
129
116
130
117
[ HttpPut ( "{id}" ) ]
131
- public IActionResult UpdateOwner ( Guid id , [ FromBody ] OwnerForUpdateDto owner )
118
+ public IActionResult UpdateOwner ( Guid id , [ FromBody ] Owner owner )
132
119
{
133
120
try
134
121
{
135
- if ( owner == null )
122
+ if ( owner . IsObjectNull ( ) )
136
123
{
137
124
_logger . LogError ( "Owner object sent from client is null." ) ;
138
125
return BadRequest ( "Owner object is null" ) ;
@@ -144,17 +131,14 @@ public IActionResult UpdateOwner(Guid id, [FromBody]OwnerForUpdateDto owner)
144
131
return BadRequest ( "Invalid model object" ) ;
145
132
}
146
133
147
- var ownerEntity = _repository . Owner . GetOwnerById ( id ) ;
148
- if ( ownerEntity == null )
134
+ var dbOwner = _repository . Owner . GetOwnerById ( id ) ;
135
+ if ( dbOwner . IsEmptyObject ( ) )
149
136
{
150
137
_logger . LogError ( $ "Owner with id: { id } , hasn't been found in db.") ;
151
138
return NotFound ( ) ;
152
139
}
153
140
154
- _mapper . Map ( owner , ownerEntity ) ;
155
-
156
- _repository . Owner . UpdateOwner ( ownerEntity ) ;
157
- _repository . Save ( ) ;
141
+ _repository . Owner . UpdateOwner ( dbOwner , owner ) ;
158
142
159
143
return NoContent ( ) ;
160
144
}
@@ -171,14 +155,13 @@ public IActionResult DeleteOwner(Guid id)
171
155
try
172
156
{
173
157
var owner = _repository . Owner . GetOwnerById ( id ) ;
174
- if ( owner == null )
158
+ if ( owner . IsEmptyObject ( ) )
175
159
{
176
160
_logger . LogError ( $ "Owner with id: { id } , hasn't been found in db.") ;
177
161
return NotFound ( ) ;
178
162
}
179
163
180
164
_repository . Owner . DeleteOwner ( owner ) ;
181
- _repository . Save ( ) ;
182
165
183
166
return NoContent ( ) ;
184
167
}
@@ -189,4 +172,4 @@ public IActionResult DeleteOwner(Guid id)
189
172
}
190
173
}
191
174
}
192
- }
175
+ }
0 commit comments