Skip to content

Commit 9e596be

Browse files
committed
Added error handling middleware and updated delete route
1 parent e063fee commit 9e596be

13 files changed

+192
-157
lines changed

.vs/config/applicationhost.config

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
1+
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
44
IIS configuration sections.
@@ -156,10 +156,10 @@
156156
</site>
157157
<site name="Collections.API" id="2">
158158
<application path="/" applicationPool="Clr4IntegratedAppPool">
159-
<virtualDirectory path="/" physicalPath="C:\Source\Repos\Collections.API\Collections.API" />
159+
<virtualDirectory path="/" physicalPath="C:\Source\Repos\Collection.API\Collections.API" />
160160
</application>
161161
<bindings>
162-
<binding protocol="http" bindingInformation="*:3763:localhost" />
162+
<binding protocol="http" bindingInformation="*:3763:localhost" />
163163
</bindings>
164164
</site>
165165
<site name="Collections.API(1)" id="3">

Collections.API/Controllers/V1/AlbumsController.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class AlbumsController : CollectionsController<AlbumModel, IAlbumModel, A
2222
/// <param name="collectionService">The collection service.</param>
2323
/// <param name="mapper">The mapper.</param>
2424
/// <param name="logger">The logger.</param>
25-
public AlbumsController(ICollectionService collectionService, IMapper mapper, ILogger logger)
25+
public AlbumsController(ICollectionService collectionService, IMapper mapper, ILogger<AlbumsController> logger)
2626
: base(collectionService, mapper, logger) {}
2727

2828
/// <summary>
@@ -86,13 +86,13 @@ public AlbumsController(ICollectionService collectionService, IMapper mapper, IL
8686
public override async Task<IActionResult> PutAsync([FromRoute]string id, [FromBody]AlbumModel model) { return await base.PutAsync(id, model); }
8787

8888
/// <summary>
89-
/// Deletes the records of the specified type asynchronously.
89+
/// Deletes the record of the specified type asynchronously.
9090
/// </summary>
91-
/// <param name="ids">The identifiers of the records to delete.</param>
91+
/// <param name="id">The identifier of the record to delete.</param>
9292
/// <returns><see cref="IActionResult"/>OK if successful</returns>
9393
[HttpDelete]
9494
[Route("")]
9595
[Produces(typeof(IActionResult))]
96-
public override async Task<IActionResult> DeleteAsync([FromBody]IEnumerable<string> ids) { return await base.DeleteAsync(ids); }
96+
public override async Task<IActionResult> DeleteAsync([FromRoute]string id) { return await base.DeleteAsync(id); }
9797
}
9898
}

Collections.API/Controllers/V1/BooksController.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class BooksController : CollectionsController<BookModel, IBookModel, Book
2222
/// <param name="collectionService">The collection service.</param>
2323
/// <param name="mapper">The mapper.</param>
2424
/// <param name="logger">The logger.</param>
25-
public BooksController(ICollectionService collectionService, IMapper mapper, ILogger logger)
25+
public BooksController(ICollectionService collectionService, IMapper mapper, ILogger<BooksController> logger)
2626
: base(collectionService, mapper, logger) { }
2727

2828
/// <summary>
@@ -86,13 +86,13 @@ public BooksController(ICollectionService collectionService, IMapper mapper, ILo
8686
public override async Task<IActionResult> PutAsync([FromRoute]string id, [FromBody]BookModel model) { return await base.PutAsync(id, model); }
8787

8888
/// <summary>
89-
/// Deletes the records of the specified type asynchronously.
89+
/// Deletes the record of the specified type asynchronously.
9090
/// </summary>
91-
/// <param name="ids">The identifiers of the records to delete.</param>
91+
/// <param name="id">The identifier of the record to delete.</param>
9292
/// <returns><see cref="IActionResult"/>OK if successful</returns>
9393
[HttpDelete]
9494
[Route("")]
9595
[Produces(typeof(IActionResult))]
96-
public override async Task<IActionResult> DeleteAsync([FromBody]IEnumerable<string> ids) { return await base.DeleteAsync(ids); }
96+
public override async Task<IActionResult> DeleteAsync([FromRoute]string id) { return await base.DeleteAsync(id); }
9797
}
9898
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Linq;
43
using System.Threading.Tasks;
54
using AutoMapper;
@@ -45,23 +44,16 @@ public CollectionsController(ICollectionService collectionService, IMapper mappe
4544
/// <returns><see cref="IActionResult"/>of all records</returns>
4645
public virtual async Task<IActionResult> GetAsync()
4746
{
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>();
5848

59-
return this.NotFound();
60-
}
61-
catch (Exception ex)
49+
if (result.Any())
6250
{
63-
return this.InternalServerError(ex);
51+
var mappedResults = this.mapper.Map<IEnumerable<TView>>(result);
52+
53+
return this.Ok(mappedResults);
6454
}
55+
56+
return this.NotFound();
6557
}
6658

6759
/// <summary>
@@ -70,23 +62,16 @@ public virtual async Task<IActionResult> GetAsync()
7062
/// <returns><see cref="IActionResult"/>of requested record</returns>
7163
public virtual async Task<IActionResult> GetByIdAsync([FromRoute]string id)
7264
{
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);
8366

84-
return this.NotFound();
85-
}
86-
catch (Exception ex)
67+
if (result != null)
8768
{
88-
return this.InternalServerError(ex);
69+
var mappedResult = this.mapper.Map<TDetailView>(result);
70+
71+
return this.Ok(mappedResult);
8972
}
73+
74+
return this.NotFound();
9075
}
9176

9277
/// <summary>
@@ -96,23 +81,16 @@ public virtual async Task<IActionResult> GetByIdAsync([FromRoute]string id)
9681
/// <returns><see cref="IActionResult"/>of search results</returns>
9782
public virtual async Task<IActionResult> PostSearchAsync([FromBody]TModel model)
9883
{
99-
try
100-
{
101-
var result = await this.collectionService.PostSearchAsync<TInterface, TModel>(model);
84+
var result = await this.collectionService.PostSearchAsync<TInterface, TModel>(model);
10285

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())
11387
{
114-
return this.InternalServerError(ex);
88+
var mappedResult = this.mapper.Map<IEnumerable<TView>>(result);
89+
90+
return this.Ok(mappedResult);
11591
}
92+
93+
return this.NotFound();
11694
}
11795

11896
/// <summary>
@@ -122,21 +100,14 @@ public virtual async Task<IActionResult> PostSearchAsync([FromBody]TModel model)
122100
/// <returns><see cref="IActionResult"/>OK if successful</returns>
123101
public virtual async Task<IActionResult> PostAsync([FromBody]IEnumerable<TModel> model)
124102
{
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);
133104

134-
return this.BadRequest();
135-
}
136-
catch (Exception ex)
105+
if (result)
137106
{
138-
return this.InternalServerError(ex);
107+
return this.Ok();
139108
}
109+
110+
return this.BadRequest();
140111
}
141112

142113
/// <summary>
@@ -147,21 +118,14 @@ public virtual async Task<IActionResult> PostAsync([FromBody]IEnumerable<TModel>
147118
/// <returns><see cref="IActionResult"/>OK if successful</returns>
148119
public virtual async Task<IActionResult> PatchAsync([FromRoute]string id, [FromBody]TModel model)
149120
{
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);
158122

159-
return this.BadRequest();
160-
}
161-
catch (Exception ex)
123+
if (result)
162124
{
163-
return this.InternalServerError(ex);
125+
return this.Ok();
164126
}
127+
128+
return this.BadRequest();
165129
}
166130

167131
/// <summary>
@@ -172,45 +136,31 @@ public virtual async Task<IActionResult> PatchAsync([FromRoute]string id, [FromB
172136
/// <returns><see cref="IActionResult"/>OK if successful</returns>
173137
public virtual async Task<IActionResult> PutAsync([FromRoute]string id, [FromBody]TModel model)
174138
{
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);
183140

184-
return this.BadRequest();
185-
}
186-
catch (Exception ex)
141+
if (result)
187142
{
188-
return this.InternalServerError(ex);
143+
return this.Ok();
189144
}
145+
146+
return this.BadRequest();
190147
}
191148

192149
/// <summary>
193-
/// Deletes the records of the specified type asynchronously.
150+
/// Deletes the record of the specified type asynchronously.
194151
/// </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>
196153
/// <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)
198155
{
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);
207157

208-
return this.BadRequest();
209-
}
210-
catch (Exception ex)
158+
if (result)
211159
{
212-
return this.InternalServerError(ex);
160+
return this.Ok();
213161
}
162+
163+
return this.BadRequest();
214164
}
215165
}
216166
}

Collections.API/Controllers/V1/MoviesController.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class MoviesController : CollectionsController<MovieModel, IMovieModel, M
2222
/// <param name="collectionService">The collection service.</param>
2323
/// <param name="mapper">The mapper.</param>
2424
/// <param name="logger">The logger.</param>
25-
public MoviesController(ICollectionService collectionService, IMapper mapper, ILogger logger)
25+
public MoviesController(ICollectionService collectionService, IMapper mapper, ILogger<MoviesController> logger)
2626
: base(collectionService, mapper, logger) { }
2727

2828
/// <summary>
@@ -86,13 +86,13 @@ public MoviesController(ICollectionService collectionService, IMapper mapper, IL
8686
public override async Task<IActionResult> PutAsync([FromRoute]string id, [FromBody]MovieModel model) { return await base.PutAsync(id, model); }
8787

8888
/// <summary>
89-
/// Deletes the records of the specified type asynchronously.
89+
/// Deletes the record of the specified type asynchronously.
9090
/// </summary>
91-
/// <param name="ids">The identifiers of the records to delete.</param>
91+
/// <param name="id">The identifier of the record to delete.</param>
9292
/// <returns><see cref="IActionResult"/>OK if successful</returns>
9393
[HttpDelete]
9494
[Route("")]
9595
[Produces(typeof(IActionResult))]
96-
public override async Task<IActionResult> DeleteAsync([FromBody]IEnumerable<string> ids) { return await base.DeleteAsync(ids); }
96+
public override async Task<IActionResult> DeleteAsync([FromRoute]string id) { return await base.DeleteAsync(id); }
9797
}
9898
}

Collections.API/Controllers/V1/VideoGamesController.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class VideoGamesController : CollectionsController<VideoGameModel, IVideo
2222
/// <param name="collectionService">The collection service.</param>
2323
/// <param name="mapper">The mapper.</param>
2424
/// <param name="logger">The logger.</param>
25-
public VideoGamesController(ICollectionService collectionService, IMapper mapper, ILogger logger)
25+
public VideoGamesController(ICollectionService collectionService, IMapper mapper, ILogger<VideoGamesController> logger)
2626
: base(collectionService, mapper, logger) { }
2727

2828
/// <summary>
@@ -86,13 +86,13 @@ public VideoGamesController(ICollectionService collectionService, IMapper mapper
8686
public override async Task<IActionResult> PutAsync([FromRoute]string id, [FromBody]VideoGameModel model) { return await base.PutAsync(id, model); }
8787

8888
/// <summary>
89-
/// Deletes the records of the specified type asynchronously.
89+
/// Deletes the record of the specified type asynchronously.
9090
/// </summary>
91-
/// <param name="ids">The identifiers of the records to delete.</param>
91+
/// <param name="id">The identifier of the record to delete.</param>
9292
/// <returns><see cref="IActionResult"/>OK if successful</returns>
9393
[HttpDelete]
9494
[Route("")]
9595
[Produces(typeof(IActionResult))]
96-
public override async Task<IActionResult> DeleteAsync([FromBody]IEnumerable<string> ids) { return await base.DeleteAsync(ids); }
96+
public override async Task<IActionResult> DeleteAsync([FromRoute]string id) { return await base.DeleteAsync(id); }
9797
}
9898
}

0 commit comments

Comments
 (0)