1
- using System ;
2
- using System . Collections . Generic ;
1
+ using System . Collections . Generic ;
3
2
using System . Linq ;
4
3
using System . Threading . Tasks ;
5
4
using AutoMapper ;
@@ -45,23 +44,16 @@ public CollectionsController(ICollectionService collectionService, IMapper mappe
45
44
/// <returns><see cref="IActionResult"/>of all records</returns>
46
45
public virtual async Task < IActionResult > GetAsync ( )
47
46
{
48
- try
49
- {
50
- var result = await this . collectionService . GetAsync < TInterface , TModel > ( ) ;
51
-
52
- if ( result . Any ( ) )
53
- {
54
- var mappedResults = this . mapper . Map < IEnumerable < TView > > ( result ) ;
55
-
56
- return this . Ok ( mappedResults ) ;
57
- }
47
+ var result = await this . collectionService . GetAsync < TInterface , TModel > ( ) ;
58
48
59
- return this . NotFound ( ) ;
60
- }
61
- catch ( Exception ex )
49
+ if ( result . Any ( ) )
62
50
{
63
- return this . InternalServerError ( ex ) ;
51
+ var mappedResults = this . mapper . Map < IEnumerable < TView > > ( result ) ;
52
+
53
+ return this . Ok ( mappedResults ) ;
64
54
}
55
+
56
+ return this . NotFound ( ) ;
65
57
}
66
58
67
59
/// <summary>
@@ -70,23 +62,16 @@ public virtual async Task<IActionResult> GetAsync()
70
62
/// <returns><see cref="IActionResult"/>of requested record</returns>
71
63
public virtual async Task < IActionResult > GetByIdAsync ( [ FromRoute ] string id )
72
64
{
73
- try
74
- {
75
- var result = await this . collectionService . GetByIdAsync < TInterface > ( id ) ;
76
-
77
- if ( result != null )
78
- {
79
- var mappedResult = this . mapper . Map < TDetailView > ( result ) ;
80
-
81
- return this . Ok ( mappedResult ) ;
82
- }
65
+ var result = await this . collectionService . GetByIdAsync < TInterface > ( id ) ;
83
66
84
- return this . NotFound ( ) ;
85
- }
86
- catch ( Exception ex )
67
+ if ( result != null )
87
68
{
88
- return this . InternalServerError ( ex ) ;
69
+ var mappedResult = this . mapper . Map < TDetailView > ( result ) ;
70
+
71
+ return this . Ok ( mappedResult ) ;
89
72
}
73
+
74
+ return this . NotFound ( ) ;
90
75
}
91
76
92
77
/// <summary>
@@ -96,23 +81,16 @@ public virtual async Task<IActionResult> GetByIdAsync([FromRoute]string id)
96
81
/// <returns><see cref="IActionResult"/>of search results</returns>
97
82
public virtual async Task < IActionResult > PostSearchAsync ( [ FromBody ] TModel model )
98
83
{
99
- try
100
- {
101
- var result = await this . collectionService . PostSearchAsync < TInterface , TModel > ( model ) ;
84
+ var result = await this . collectionService . PostSearchAsync < TInterface , TModel > ( model ) ;
102
85
103
- if ( result . Any ( ) )
104
- {
105
- var mappedResult = this . mapper . Map < IEnumerable < TView > > ( result ) ;
106
-
107
- return this . Ok ( mappedResult ) ;
108
- }
109
-
110
- return this . NotFound ( ) ;
111
- }
112
- catch ( Exception ex )
86
+ if ( result . Any ( ) )
113
87
{
114
- return this . InternalServerError ( ex ) ;
88
+ var mappedResult = this . mapper . Map < IEnumerable < TView > > ( result ) ;
89
+
90
+ return this . Ok ( mappedResult ) ;
115
91
}
92
+
93
+ return this . NotFound ( ) ;
116
94
}
117
95
118
96
/// <summary>
@@ -122,21 +100,14 @@ public virtual async Task<IActionResult> PostSearchAsync([FromBody]TModel model)
122
100
/// <returns><see cref="IActionResult"/>OK if successful</returns>
123
101
public virtual async Task < IActionResult > PostAsync ( [ FromBody ] IEnumerable < TModel > model )
124
102
{
125
- try
126
- {
127
- var result = await this . collectionService . PostAsync < TInterface , TModel > ( model ) ;
128
-
129
- if ( result )
130
- {
131
- return this . Ok ( ) ;
132
- }
103
+ var result = await this . collectionService . PostAsync < TInterface , TModel > ( model ) ;
133
104
134
- return this . BadRequest ( ) ;
135
- }
136
- catch ( Exception ex )
105
+ if ( result )
137
106
{
138
- return this . InternalServerError ( ex ) ;
107
+ return this . Ok ( ) ;
139
108
}
109
+
110
+ return this . BadRequest ( ) ;
140
111
}
141
112
142
113
/// <summary>
@@ -147,21 +118,14 @@ public virtual async Task<IActionResult> PostAsync([FromBody]IEnumerable<TModel>
147
118
/// <returns><see cref="IActionResult"/>OK if successful</returns>
148
119
public virtual async Task < IActionResult > PatchAsync ( [ FromRoute ] string id , [ FromBody ] TModel model )
149
120
{
150
- try
151
- {
152
- var result = await this . collectionService . PatchAsync < TInterface , TModel > ( id , model ) ;
153
-
154
- if ( result )
155
- {
156
- return this . Ok ( ) ;
157
- }
121
+ var result = await this . collectionService . PatchAsync < TInterface , TModel > ( id , model ) ;
158
122
159
- return this . BadRequest ( ) ;
160
- }
161
- catch ( Exception ex )
123
+ if ( result )
162
124
{
163
- return this . InternalServerError ( ex ) ;
125
+ return this . Ok ( ) ;
164
126
}
127
+
128
+ return this . BadRequest ( ) ;
165
129
}
166
130
167
131
/// <summary>
@@ -172,45 +136,31 @@ public virtual async Task<IActionResult> PatchAsync([FromRoute]string id, [FromB
172
136
/// <returns><see cref="IActionResult"/>OK if successful</returns>
173
137
public virtual async Task < IActionResult > PutAsync ( [ FromRoute ] string id , [ FromBody ] TModel model )
174
138
{
175
- try
176
- {
177
- var result = await this . collectionService . PutAsync < TInterface , TModel > ( id , model ) ;
178
-
179
- if ( result )
180
- {
181
- return this . Ok ( ) ;
182
- }
139
+ var result = await this . collectionService . PutAsync < TInterface , TModel > ( id , model ) ;
183
140
184
- return this . BadRequest ( ) ;
185
- }
186
- catch ( Exception ex )
141
+ if ( result )
187
142
{
188
- return this . InternalServerError ( ex ) ;
143
+ return this . Ok ( ) ;
189
144
}
145
+
146
+ return this . BadRequest ( ) ;
190
147
}
191
148
192
149
/// <summary>
193
- /// Deletes the records of the specified type asynchronously.
150
+ /// Deletes the record of the specified type asynchronously.
194
151
/// </summary>
195
- /// <param name="ids ">The identifiers of the records to delete.</param>
152
+ /// <param name="id ">The identifier of the record to delete.</param>
196
153
/// <returns><see cref="IActionResult"/>OK if successful</returns>
197
- public virtual async Task < IActionResult > DeleteAsync ( [ FromBody ] IEnumerable < string > ids )
154
+ public virtual async Task < IActionResult > DeleteAsync ( [ FromRoute ] string id )
198
155
{
199
- try
200
- {
201
- var result = await this . collectionService . DeleteAsync < TInterface > ( ids ) ;
202
-
203
- if ( result )
204
- {
205
- return this . Ok ( ) ;
206
- }
156
+ var result = await this . collectionService . DeleteAsync < TInterface > ( id ) ;
207
157
208
- return this . BadRequest ( ) ;
209
- }
210
- catch ( Exception ex )
158
+ if ( result )
211
159
{
212
- return this . InternalServerError ( ex ) ;
160
+ return this . Ok ( ) ;
213
161
}
162
+
163
+ return this . BadRequest ( ) ;
214
164
}
215
165
}
216
166
}
0 commit comments