-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create RemediatRCustomerController #52
Create RemediatRCustomerController #52
- Loading branch information
1 parent
6d30255
commit 011fc0d
Showing
1 changed file
with
114 additions
and
0 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
src/Headway.WebApi/Controllers/RemediatRCustomerController.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,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 RemediatRCustomerController : ApiControllerBase<RemediatRCustomerController> | ||
{ | ||
private readonly IRemediatRRepository remediatRRepository; | ||
|
||
public RemediatRCustomerController( | ||
IRemediatRRepository repository, | ||
ILogger<RemediatRCustomerController> logger) | ||
: base(repository, logger) | ||
{ | ||
this.remediatRRepository = repository; | ||
} | ||
|
||
[HttpGet] | ||
public async Task<IActionResult> Get() | ||
{ | ||
var authorised = await IsAuthorisedAsync(Roles.REMEDIATR_CUSTOMER_READ) | ||
.ConfigureAwait(false); | ||
|
||
if (!authorised) | ||
{ | ||
return Unauthorized(); | ||
} | ||
|
||
var customers = await remediatRRepository | ||
.GetCustomersAsync() | ||
.ConfigureAwait(false); | ||
|
||
return Ok(customers); | ||
} | ||
|
||
[HttpGet("{customerId}")] | ||
public async Task<IActionResult> Get(int customerId) | ||
{ | ||
var authorised = await IsAuthorisedAsync(Roles.REMEDIATR_CUSTOMER_READ) | ||
.ConfigureAwait(false); | ||
|
||
if (!authorised) | ||
{ | ||
return Unauthorized(); | ||
} | ||
|
||
var customer = await remediatRRepository | ||
.GetCustomerAsync(customerId) | ||
.ConfigureAwait(false); | ||
|
||
return Ok(customer); | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> Post([FromBody] Customer customer) | ||
{ | ||
var authorised = await IsAuthorisedAsync(Roles.REMEDIATR_CUSTOMER_WRITE) | ||
.ConfigureAwait(false); | ||
|
||
if (!authorised) | ||
{ | ||
return Unauthorized(); | ||
} | ||
|
||
var savedCustomer = await remediatRRepository | ||
.AddCustomerAsync(customer) | ||
.ConfigureAwait(false); | ||
|
||
return Ok(savedCustomer); | ||
} | ||
|
||
[HttpPut] | ||
public async Task<IActionResult> Put([FromBody] Customer customer) | ||
{ | ||
var authorised = await IsAuthorisedAsync(Roles.REMEDIATR_CUSTOMER_WRITE) | ||
.ConfigureAwait(false); | ||
|
||
if (!authorised) | ||
{ | ||
return Unauthorized(); | ||
} | ||
|
||
var savedCustomer = await remediatRRepository | ||
.UpdateCustomerAsync(customer) | ||
.ConfigureAwait(false); | ||
|
||
return Ok(savedCustomer); | ||
} | ||
|
||
[HttpDelete("{customerId}")] | ||
public async Task<IActionResult> Delete(int customerId) | ||
{ | ||
var authorised = await IsAuthorisedAsync(Roles.REMEDIATR_CUSTOMER_WRITE) | ||
.ConfigureAwait(false); | ||
|
||
if (!authorised) | ||
{ | ||
return Unauthorized(); | ||
} | ||
|
||
var result = await remediatRRepository | ||
.DeleteCustomerAsync(customerId) | ||
.ConfigureAwait(false); | ||
|
||
return Ok(result); | ||
} | ||
} | ||
} |