Skip to content

Commit

Permalink
Create RemediatRRedressController #52
Browse files Browse the repository at this point in the history
Create RemediatRRedressController #52
  • Loading branch information
grantcolley committed Jun 5, 2022
1 parent 7ce5c2c commit 926cd33
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 additions & 0 deletions src/Headway.WebApi/Controllers/RemediatRRedressController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using Headway.Core.Attributes;
using Headway.Core.Constants;
using Headway.RemediatR.Core.Interface;
using Headway.RemediatR.Core.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;

namespace Headway.WebApi.Controllers
{
[DynamicApiController]
public class RemediatRRedressController : ApiControllerBase<RemediatRRedressController>
{
private readonly IRemediatRRepository remediatRRepository;

public RemediatRRedressController(
IRemediatRRepository repository,
ILogger<RemediatRRedressController> logger)
: base(repository, logger)
{
this.remediatRRepository = repository;
}

[HttpGet]
public async Task<IActionResult> Get()
{
var authorised = await IsAuthorisedAsync(Roles.REMEDIATR_REDRESS_READ)
.ConfigureAwait(false);

if (!authorised)
{
return Unauthorized();
}

var redresses = await remediatRRepository
.GetRedressesAsync()
.ConfigureAwait(false);

return Ok(redresses);
}

[HttpGet("{redressId}")]
public async Task<IActionResult> Get(int redressId)
{
var authorised = await IsAuthorisedAsync(Roles.REMEDIATR_REDRESS_READ)
.ConfigureAwait(false);

if (!authorised)
{
return Unauthorized();
}

var redress = await remediatRRepository
.GetRedressAsync(redressId)
.ConfigureAwait(false);

return Ok(redress);
}

[HttpPost]
public async Task<IActionResult> Post([FromBody] Redress redress)
{
var authorised = await IsAuthorisedAsync(Roles.REMEDIATR_REDRESS_WRITE)
.ConfigureAwait(false);

if (!authorised)
{
return Unauthorized();
}

var savedRedress = await remediatRRepository
.AddRedressAsync(redress)
.ConfigureAwait(false);

return Ok(savedRedress);
}

[HttpPut]
public async Task<IActionResult> Put([FromBody] Redress redress)
{
var authorised = await IsAuthorisedAsync(Roles.REMEDIATR_REDRESS_WRITE)
.ConfigureAwait(false);

if (!authorised)
{
return Unauthorized();
}

var savedRedress = await remediatRRepository
.UpdateRedressAsync(redress)
.ConfigureAwait(false);

return Ok(savedRedress);
}

[HttpDelete("{redressId}")]
public async Task<IActionResult> Delete(int redressId)
{
var authorised = await IsAuthorisedAsync(Roles.REMEDIATR_REDRESS_WRITE)
.ConfigureAwait(false);

if (!authorised)
{
return Unauthorized();
}

var result = await remediatRRepository
.DeleteRedressAsync(redressId)
.ConfigureAwait(false);

return Ok(result);
}
}
}

0 comments on commit 926cd33

Please sign in to comment.