Skip to content

Commit

Permalink
Merge pull request #43 from davewalker5/MC-258-Equipment-Register-API
Browse files Browse the repository at this point in the history
Added the API for an equipment registry
  • Loading branch information
davewalker5 authored Dec 2, 2023
2 parents 55b6045 + 1dbbd00 commit d447774
Show file tree
Hide file tree
Showing 39 changed files with 2,233 additions and 57 deletions.
4 changes: 2 additions & 2 deletions docker/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/dotnet/core/aspnet:latest
COPY musiccatalogue.api-1.23.0.0 /opt/musiccatalogue.api-1.23.0.0
WORKDIR /opt/musiccatalogue.api-1.23.0.0/bin
COPY musiccatalogue.api-1.24.0.0 /opt/musiccatalogue.api-1.24.0.0
WORKDIR /opt/musiccatalogue.api-1.24.0.0/bin
ENTRYPOINT [ "./MusicCatalogue.Api" ]
130 changes: 130 additions & 0 deletions src/MusicCatalogue.Api/Controllers/EquipmentController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MusicCatalogue.Entities.Database;
using MusicCatalogue.Entities.Interfaces;
using MusicCatalogue.Entities.Search;

namespace MusicCatalogue.Api.Controllers
{
[Authorize]
[ApiController]
[ApiConventionType(typeof(DefaultApiConventions))]
[Route("[controller]")]
public class EquipmentController : Controller
{
private readonly IMusicCatalogueFactory _factory;

public EquipmentController(IMusicCatalogueFactory factory)
{
_factory = factory;
}

/// <summary>
/// Return a list of all items of equipment matching the filter criteria
/// </summary>
/// <returns></returns>
[HttpPost]
[Route("search")]
public async Task<ActionResult<List<Equipment>>> GetEquipmentAsync([FromBody] EquipmentSearchCriteria criteria)
{
// Ideally, this method would use the GET verb but as more filtering criteria are added that leads
// to an increasing number of query string parameters and a very messy URL. So the filter criteria
// are POSTed in the request body, instead, and bound into a strongly typed criteria object

var equipment = await _factory.Search.EquipmentSearchAsync(criteria);

if (equipment == null)
{
return NoContent();
}

return equipment;
}

/// <summary>
/// Return equipment details given an equipment type ID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
[Route("{id:int}")]
public async Task<ActionResult<Equipment>> GetEquipmentByIdAsync(int id)
{
var equipment = await _factory.Equipment.GetAsync(x => x.Id == id);

if (equipment == null)
{
return NotFound();
}

return equipment;
}

/// <summary>
/// Add an item of equipment from a template contained in the request body
/// </summary>
/// <param name="template"></param>
/// <returns></returns>
[HttpPost]
[Route("")]
public async Task<ActionResult<Equipment>> AddEquipmentAsync([FromBody] Equipment template)
{
var equipment = await _factory.Equipment.AddAsync(
template.EquipmentTypeId,
template.ManufacturerId,
template.Description,
template.Model,
template.SerialNumber,
template.IsWishListItem,
template.Purchased,
template.Price,
template.RetailerId);
return equipment;
}

/// <summary>
/// Update an item of equipment from a template contained in the request body
/// </summary>
/// <param name="template"></param>
/// <returns></returns>
[HttpPut]
[Route("")]
public async Task<ActionResult<Equipment?>> UpdateEquipmentAsync([FromBody] Equipment template)
{
var equipment = await _factory.Equipment.UpdateAsync(
template.Id,
template.EquipmentTypeId,
template.ManufacturerId,
template.Description,
template.Model,
template.SerialNumber,
template.IsWishListItem,
template.Purchased,
template.Price,
template.RetailerId);
return equipment;
}

/// <summary>
/// Delete an item of equipment given their ID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpDelete]
[Route("{id}")]
public async Task<IActionResult> DeleteEquipment(int id)
{
// Check the equipment exists, first
var equipment = await _factory.Equipment.GetAsync(x => x.Id == id);
if (equipment == null)
{
return NotFound();
}

// It does, so delete it
await _factory.Equipment.DeleteAsync(id);

return Ok();
}
}
}
115 changes: 115 additions & 0 deletions src/MusicCatalogue.Api/Controllers/EquipmentTypesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
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 EquipmentTypesController : Controller
{
private readonly IMusicCatalogueFactory _factory;

public EquipmentTypesController(IMusicCatalogueFactory factory)
{
_factory = factory;
}

/// <summary>
/// Return a list of all the equipment types in the catalogue matching the filter criteria in the request body
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("")]
public async Task<ActionResult<List<EquipmentType>>> GetEquipmentTypesAsync()
{
var equipmentTypes = await _factory.EquipmentTypes.ListAsync(x => true);

if (equipmentTypes == null)
{
return NoContent();
}

return equipmentTypes;
}

/// <summary>
/// Return equipment type details given an equipment type ID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
[Route("{id:int}")]
public async Task<ActionResult<EquipmentType>> GetEquipmentTypeByIdAsync(int id)
{
var equipmentType = await _factory.EquipmentTypes.GetAsync(x => x.Id == id);

if (equipmentType == null)
{
return NotFound();
}

return equipmentType;
}

/// <summary>
/// Add an equipment type from a template contained in the request body
/// </summary>
/// <param name="template"></param>
/// <returns></returns>
[HttpPost]
[Route("")]
public async Task<ActionResult<EquipmentType>> AddEquipmentTypeAsync([FromBody] EquipmentType template)
{
var equipmentType = await _factory.EquipmentTypes.AddAsync(template.Name);
return equipmentType;
}

/// <summary>
/// Update an equipment type from a template contained in the request body
/// </summary>
/// <param name="template"></param>
/// <returns></returns>
[HttpPut]
[Route("")]
public async Task<ActionResult<EquipmentType?>> UpdateEquipmentTypeAsync([FromBody] EquipmentType template)
{
var equipmentType = await _factory.EquipmentTypes.UpdateAsync(template.Id, template.Name);
return equipmentType;
}

/// <summary>
/// Delete an equipment type given their ID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpDelete]
[Route("{id}")]
public async Task<IActionResult> DeleteEquipmentType(int id)
{
// Check the equipment type exists, first
var equipmentType = await _factory.EquipmentTypes.GetAsync(x => x.Id == id);
if (equipmentType == null)
{
return NotFound();
}

try
{
// It does, so delete them
await _factory.EquipmentTypes.DeleteAsync(id);
}
catch (EquipmentTypeInUseException)
{
// Equipment type is in use (has equipment associated with it) so this is a bad request
return BadRequest();
}

return Ok();
}
}
}
115 changes: 115 additions & 0 deletions src/MusicCatalogue.Api/Controllers/ManufacturersController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
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 ManufacturersController : Controller
{
private readonly IMusicCatalogueFactory _factory;

public ManufacturersController(IMusicCatalogueFactory factory)
{
_factory = factory;
}

/// <summary>
/// Return a list of all the manufacturers in the catalogue matching the filter criteria in the request body
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("")]
public async Task<ActionResult<List<Manufacturer>>> GetManufacturersAsync()
{
var manufacturers = await _factory.Manufacturers.ListAsync(x => true);

if (manufacturers == null)
{
return NoContent();
}

return manufacturers;
}

/// <summary>
/// Return manufacturer details given a manufacturer ID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
[Route("{id:int}")]
public async Task<ActionResult<Manufacturer>> GetManufacturerByIdAsync(int id)
{
var manufacturer = await _factory.Manufacturers.GetAsync(x => x.Id == id);

if (manufacturer == null)
{
return NotFound();
}

return manufacturer;
}

/// <summary>
/// Add a manufacturer from a template contained in the request body
/// </summary>
/// <param name="template"></param>
/// <returns></returns>
[HttpPost]
[Route("")]
public async Task<ActionResult<Manufacturer>> AddManufacturerAsync([FromBody] Manufacturer template)
{
var manufacturer = await _factory.Manufacturers.AddAsync(template.Name);
return manufacturer;
}

/// <summary>
/// Update a manufacturer from a template contained in the request body
/// </summary>
/// <param name="template"></param>
/// <returns></returns>
[HttpPut]
[Route("")]
public async Task<ActionResult<Manufacturer?>> UpdateManufacturerAsync([FromBody] Manufacturer template)
{
var manufacturer = await _factory.Manufacturers.UpdateAsync(template.Id, template.Name);
return manufacturer;
}

/// <summary>
/// Delete a manufacturer given their ID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpDelete]
[Route("{id}")]
public async Task<IActionResult> DeleteManufacturer(int id)
{
// Check the manufacturer exists, first
var manufacturer = await _factory.Manufacturers.GetAsync(x => x.Id == id);
if (manufacturer == null)
{
return NotFound();
}

try
{
// They do, so delete them
await _factory.Manufacturers.DeleteAsync(id);
}
catch (ManufacturerInUseException)
{
// Manufacturer is in use (have equipment associated with them) so this is a bad request
return BadRequest();
}

return Ok();
}
}
}
6 changes: 3 additions & 3 deletions src/MusicCatalogue.Api/MusicCatalogue.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ReleaseVersion>1.23.0.0</ReleaseVersion>
<FileVersion>1.23.0.0</FileVersion>
<ProductVersion>1.23.0</ProductVersion>
<ReleaseVersion>1.24.0.0</ReleaseVersion>
<FileVersion>1.24.0.0</FileVersion>
<ProductVersion>1.24.0</ProductVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
Loading

0 comments on commit d447774

Please sign in to comment.