-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f163fa
commit 35bf8e8
Showing
6 changed files
with
275 additions
and
18 deletions.
There are no files selected for viewing
119 changes: 119 additions & 0 deletions
119
src/MusicCatalogue.Api/Controllers/RetailersController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using MusicCatalogue.Entities.Database; | ||
using MusicCatalogue.Entities.Exceptions; | ||
using MusicCatalogue.Entities.Interfaces; | ||
|
||
namespace MusicCatalogue.Api.Controllers | ||
{ | ||
[Authorize] | ||
[ApiController] | ||
[ApiConventionType(typeof(DefaultApiConventions))] | ||
[Route("[controller]")] | ||
public class RetailersController : Controller | ||
{ | ||
private readonly IMusicCatalogueFactory _factory; | ||
|
||
public RetailersController(IMusicCatalogueFactory factory) | ||
{ | ||
_factory = factory; | ||
} | ||
|
||
/// <summary> | ||
/// Return a list of all the retailers in the catalogue | ||
/// </summary> | ||
/// <returns></returns> | ||
[HttpGet] | ||
[Route("")] | ||
public async Task<ActionResult<List<Retailer>>> GetRetailersAsync() | ||
{ | ||
// Get a list of all artists in the catalogue | ||
List<Retailer> retailers = await _factory.Retailers.ListAsync(x => true); | ||
|
||
// If there are no artists, return a no content response | ||
if (!retailers.Any()) | ||
{ | ||
return NoContent(); | ||
} | ||
|
||
return retailers; | ||
} | ||
|
||
/// <summary> | ||
/// Return retailer details given a retailer ID | ||
/// </summary> | ||
/// <param name="id"></param> | ||
/// <returns></returns> | ||
[HttpGet] | ||
[Route("{id:int}")] | ||
public async Task<ActionResult<Retailer>> GetRetailerByIdAsync(int id) | ||
{ | ||
var retailer = await _factory.Retailers.GetAsync(x => x.Id == id); | ||
|
||
if (retailer == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
return retailer; | ||
} | ||
|
||
/// <summary> | ||
/// Add a retailer to the catalogue | ||
/// </summary> | ||
/// <param name="id"></param> | ||
/// <returns></returns> | ||
[HttpPost] | ||
[Route("")] | ||
public async Task<ActionResult<Retailer>> AddRetailerAsync([FromBody] Retailer template) | ||
{ | ||
var retailer = await _factory.Retailers.AddAsync(template.Name); | ||
return retailer; | ||
} | ||
|
||
/// <summary> | ||
/// Update an existing retailer | ||
/// </summary> | ||
/// <param name="id"></param> | ||
/// <returns></returns> | ||
[HttpPut] | ||
[Route("")] | ||
public async Task<ActionResult<Retailer?>> UpdateRetailerAsync([FromBody] Retailer template) | ||
{ | ||
var retailer = await _factory.Retailers.UpdateAsync(template.Id, template.Name); | ||
return retailer; | ||
} | ||
|
||
/// <summary> | ||
/// Delete an existing retailer | ||
/// </summary> | ||
/// <param name="id"></param> | ||
/// <returns></returns> | ||
[HttpDelete] | ||
[Route("{id}")] | ||
public async Task<IActionResult> DeleteRetailerAsync(int id) | ||
{ | ||
// Make sure the retailer exists | ||
var retailer = await _factory.Retailers.GetAsync(x => x.Id == id); | ||
|
||
// If the retailer doesn't exist, return a 404 | ||
if (retailer == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
try | ||
{ | ||
// Delete the retailer | ||
await _factory.Retailers.DeleteAsync(id); | ||
} | ||
catch (RetailerInUseException) | ||
{ | ||
// Retailer is in use so this is a bad request | ||
return BadRequest(); | ||
} | ||
|
||
return Ok(); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/MusicCatalogue.Entities/Exceptions/RetailerInUseException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.Serialization; | ||
|
||
namespace MusicCatalogue.Entities.Exceptions | ||
{ | ||
|
||
[Serializable] | ||
[ExcludeFromCodeCoverage] | ||
public class RetailerInUseException : Exception | ||
{ | ||
public RetailerInUseException() | ||
{ | ||
} | ||
|
||
public RetailerInUseException(string message) : base(message) | ||
{ | ||
} | ||
|
||
public RetailerInUseException(string message, Exception inner) : base(message, inner) | ||
{ | ||
} | ||
|
||
protected RetailerInUseException(SerializationInfo serializationInfo, StreamingContext streamingContext) : base(serializationInfo, streamingContext) | ||
{ | ||
} | ||
|
||
public override void GetObjectData(SerializationInfo info, StreamingContext context) | ||
{ | ||
base.GetObjectData(info, context); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters