-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
API ControllerRefactor
- Loading branch information
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Providers; | ||
|
||
[ApiController] | ||
[Route("api/v1/[controller]")] | ||
public abstract class BaseApiController : ControllerBase | ||
{ | ||
protected readonly NotificationSystem _notificationSystem; | ||
|
||
public BaseApiController( | ||
NotificationSystem notificationSystem) | ||
{ | ||
_notificationSystem = notificationSystem; | ||
} | ||
|
||
protected IActionResult CheckAuthorization(string apiKey, string resource, string operation) | ||
{ | ||
var user = AuthProvider.GetUser(apiKey); | ||
if (user == null) | ||
return Unauthorized(); | ||
|
||
if (!AuthProvider.HasAccess(user, resource, operation)) | ||
return Forbid(); | ||
|
||
return null; | ||
Check warning on line 25 in C#api/Controllers/BaseApiController.cs
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Models; | ||
using Providers; | ||
|
||
[ApiController] | ||
[Route("api/v1/[controller]")] | ||
public class ClientsController : BaseApiController | ||
{ | ||
public ClientsController( | ||
NotificationSystem notificationSystem) | ||
: base(notificationSystem) | ||
{ | ||
} | ||
|
||
[HttpGet] | ||
public IActionResult GetClients() | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "clients", "get"); | ||
Check warning on line 18 in C#api/Controllers/ClientsController.cs
|
||
if (auth != null) return auth; | ||
|
||
var clients = DataProvider.fetch_client_pool().GetClients(); | ||
return Ok(clients); | ||
} | ||
|
||
[HttpGet("{id}")] | ||
public IActionResult GetClient(int id) | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "clients", "get"); | ||
Check warning on line 28 in C#api/Controllers/ClientsController.cs
|
||
if (auth != null) return auth; | ||
|
||
var client = DataProvider.fetch_client_pool().GetClient(id); | ||
if (client == null) return NotFound(); | ||
|
||
return Ok(client); | ||
} | ||
|
||
[HttpGet("{id}/orders")] | ||
public IActionResult GetClientOrders(int id) | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "clients", "get"); | ||
Check warning on line 40 in C#api/Controllers/ClientsController.cs
|
||
if (auth != null) return auth; | ||
|
||
var orders = DataProvider.fetch_order_pool().GetOrdersForClient(id); | ||
return Ok(orders); | ||
} | ||
|
||
[HttpPost] | ||
public IActionResult CreateClient([FromBody] Client client) | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "clients", "post"); | ||
if (auth != null) return auth; | ||
|
||
if (client.Id == -10) return BadRequest("ID not given in body"); | ||
|
||
var success = DataProvider.fetch_client_pool().AddClient(client); | ||
if (!success) return NotFound("ID already exists in data"); | ||
|
||
DataProvider.fetch_client_pool().Save(); | ||
return CreatedAtAction(nameof(GetClient), new { id = client.Id }, client); | ||
} | ||
|
||
[HttpPut("{id}")] | ||
public IActionResult UpdateClient(int id, [FromBody] Client client) | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "clients", "put"); | ||
if (auth != null) return auth; | ||
|
||
if (client.Id == -10) return BadRequest("ID not given in body"); | ||
|
||
var success = DataProvider.fetch_client_pool().UpdateClient(id, client); | ||
if (!success) return NotFound("ID not found or ID in Body and Route are not matching"); | ||
|
||
DataProvider.fetch_client_pool().Save(); | ||
return Ok(); | ||
} | ||
|
||
[HttpDelete("{id}")] | ||
public IActionResult DeleteClient(int id) | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "clients", "delete"); | ||
if (auth != null) return auth; | ||
|
||
var success = DataProvider.fetch_client_pool().RemoveClient(id); | ||
if (!success) return NotFound("ID not found or other data is dependent on this data"); | ||
|
||
DataProvider.fetch_client_pool().Save(); | ||
return Ok(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Models; | ||
using Providers; | ||
|
||
[ApiController] | ||
[Route("api/v1/[controller]")] | ||
public class InventoriesController : BaseApiController | ||
{ | ||
public InventoriesController( | ||
NotificationSystem notificationSystem) | ||
: base(notificationSystem) | ||
{ | ||
} | ||
|
||
[HttpGet] | ||
public IActionResult GetInventories() | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "inventories", "get"); | ||
Check warning on line 18 in C#api/Controllers/InventoriesController.cs
|
||
if (auth != null) return auth; | ||
|
||
var inventories = DataProvider.fetch_inventory_pool().GetInventories(); | ||
return Ok(inventories); | ||
} | ||
|
||
[HttpGet("{id}")] | ||
public IActionResult GetInventory(int id) | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "inventories", "get"); | ||
Check warning on line 28 in C#api/Controllers/InventoriesController.cs
|
||
if (auth != null) return auth; | ||
|
||
var inventory = DataProvider.fetch_inventory_pool().GetInventory(id); | ||
if (inventory == null) return NotFound(); | ||
|
||
return Ok(inventory); | ||
} | ||
|
||
[HttpPost] | ||
public IActionResult CreateInventory([FromBody] Inventory inventory) | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "inventories", "post"); | ||
if (auth != null) return auth; | ||
|
||
if (inventory.Id == -10) return BadRequest("ID not given in body"); | ||
|
||
var success = DataProvider.fetch_inventory_pool().AddInventory(inventory); | ||
if (!success) return NotFound("ID already exists in data"); | ||
|
||
DataProvider.fetch_inventory_pool().Save(); | ||
return CreatedAtAction(nameof(GetInventory), new { id = inventory.Id }, inventory); | ||
} | ||
|
||
[HttpPut("{id}")] | ||
public IActionResult UpdateInventory(int id, [FromBody] Inventory inventory) | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "inventories", "put"); | ||
if (auth != null) return auth; | ||
|
||
if (inventory.Id == -10) return BadRequest("ID not given in body"); | ||
|
||
var success = DataProvider.fetch_inventory_pool().UpdateInventory(id, inventory); | ||
if (!success) return NotFound("ID not found or ID in Body and Route are not matching"); | ||
|
||
DataProvider.fetch_inventory_pool().Save(); | ||
return Ok(); | ||
} | ||
|
||
[HttpDelete("{id}")] | ||
public IActionResult DeleteInventory(int id) | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "inventories", "delete"); | ||
if (auth != null) return auth; | ||
|
||
var success = DataProvider.fetch_inventory_pool().RemoveInventory(id); | ||
if (!success) return NotFound("ID not found or other data is dependent on this data"); | ||
|
||
DataProvider.fetch_inventory_pool().Save(); | ||
return Ok(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Models; | ||
using Providers; | ||
|
||
[ApiController] | ||
[Route("api/v1/[controller]")] | ||
public class Item_GroupsController : BaseApiController | ||
{ | ||
public Item_GroupsController( | ||
NotificationSystem notificationSystem) | ||
: base(notificationSystem) | ||
{ | ||
} | ||
|
||
[HttpGet] | ||
public IActionResult GetItemGroups() | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "item_groups", "get"); | ||
if (auth != null) return auth; | ||
|
||
var itemGroups = DataProvider.fetch_itemgroup_pool().GetItemGroups(); | ||
return Ok(itemGroups); | ||
} | ||
|
||
[HttpGet("{id}")] | ||
public IActionResult GetItemGroup(int id) | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "item_groups", "get"); | ||
if (auth != null) return auth; | ||
|
||
var itemGroup = DataProvider.fetch_itemgroup_pool().GetItemGroup(id); | ||
if (itemGroup == null) return NotFound(); | ||
|
||
return Ok(itemGroup); | ||
} | ||
|
||
[HttpGet("{id}/items")] | ||
public IActionResult GetItemGroupItems(int id) | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "item_groups", "get"); | ||
if (auth != null) return auth; | ||
|
||
var items = DataProvider.fetch_item_pool().GetItemsForItemGroup(id); | ||
return Ok(items); | ||
} | ||
|
||
[HttpPost] | ||
public IActionResult CreateItemGroup([FromBody] ItemGroup itemGroup) | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "item_groups", "post"); | ||
if (auth != null) return auth; | ||
|
||
if (itemGroup.Id == -10) return BadRequest("ID not given in body"); | ||
|
||
var success = DataProvider.fetch_itemgroup_pool().AddItemGroup(itemGroup); | ||
if (!success) return NotFound("ID already exists in data"); | ||
|
||
DataProvider.fetch_itemgroup_pool().Save(); | ||
return CreatedAtAction(nameof(GetItemGroup), new { id = itemGroup.Id }, itemGroup); | ||
} | ||
|
||
[HttpPut("{id}")] | ||
public IActionResult UpdateItemGroup(int id, [FromBody] ItemGroup itemGroup) | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "item_groups", "put"); | ||
if (auth != null) return auth; | ||
|
||
if (itemGroup.Id == -10) return BadRequest("ID not given in body"); | ||
|
||
var success = DataProvider.fetch_itemgroup_pool().UpdateItemGroup(id, itemGroup); | ||
if (!success) return NotFound("ID not found or ID in Body and Route are not matching"); | ||
|
||
DataProvider.fetch_itemgroup_pool().Save(); | ||
return Ok(); | ||
} | ||
|
||
[HttpDelete("{id}")] | ||
public IActionResult DeleteItemGroup(int id) | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "item_groups", "delete"); | ||
if (auth != null) return auth; | ||
|
||
var success = DataProvider.fetch_itemgroup_pool().RemoveItemGroup(id); | ||
if (!success) return NotFound("ID not found or other data is dependent on this data"); | ||
|
||
DataProvider.fetch_itemgroup_pool().Save(); | ||
return Ok(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Models; | ||
using Providers; | ||
|
||
[ApiController] | ||
[Route("api/v1/[controller]")] | ||
public class Item_LinesController : BaseApiController | ||
{ | ||
public Item_LinesController( | ||
NotificationSystem notificationSystem) | ||
: base(notificationSystem) | ||
{ | ||
} | ||
|
||
[HttpGet] | ||
public IActionResult GetItemLines() | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "item_lines", "get"); | ||
if (auth != null) return auth; | ||
|
||
var itemLines = DataProvider.fetch_itemline_pool().GetItemLines(); | ||
return Ok(itemLines); | ||
} | ||
|
||
[HttpGet("{id}")] | ||
public IActionResult GetItemLine(int id) | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "item_lines", "get"); | ||
if (auth != null) return auth; | ||
|
||
var itemLine = DataProvider.fetch_itemline_pool().GetItemLine(id); | ||
if (itemLine == null) return NotFound(); | ||
|
||
return Ok(itemLine); | ||
} | ||
|
||
[HttpGet("{id}/items")] | ||
public IActionResult GetItemLineItems(int id) | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "item_lines", "get"); | ||
if (auth != null) return auth; | ||
|
||
var items = DataProvider.fetch_item_pool().GetItemsForItemLine(id); | ||
return Ok(items); | ||
} | ||
|
||
[HttpPost] | ||
public IActionResult CreateItemLine([FromBody] ItemLine itemLine) | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "item_lines", "post"); | ||
if (auth != null) return auth; | ||
|
||
if (itemLine.Id == -10) return BadRequest("ID not given in body"); | ||
|
||
var success = DataProvider.fetch_itemline_pool().AddItemline(itemLine); | ||
if (!success) return NotFound("ID already exists in data"); | ||
|
||
DataProvider.fetch_itemline_pool().Save(); | ||
return CreatedAtAction(nameof(GetItemLine), new { id = itemLine.Id }, itemLine); | ||
} | ||
|
||
[HttpPut("{id}")] | ||
public IActionResult UpdateItemLine(int id, [FromBody] ItemLine itemLine) | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "item_lines", "put"); | ||
if (auth != null) return auth; | ||
|
||
if (itemLine.Id == -10) return BadRequest("ID not given in body"); | ||
|
||
var success = DataProvider.fetch_itemline_pool().UpdateItemline(id, itemLine); | ||
if (!success) return NotFound("ID not found or ID in Body and Route are not matching"); | ||
|
||
DataProvider.fetch_itemline_pool().Save(); | ||
return Ok(); | ||
} | ||
|
||
[HttpDelete("{id}")] | ||
public IActionResult DeleteItemLine(int id) | ||
{ | ||
var auth = CheckAuthorization(Request.Headers["API_KEY"], "item_lines", "delete"); | ||
if (auth != null) return auth; | ||
|
||
var success = DataProvider.fetch_itemline_pool().RemoveItemline(id); | ||
if (!success) return NotFound("ID not found or other data is dependent on this data"); | ||
|
||
DataProvider.fetch_itemline_pool().Save(); | ||
return Ok(); | ||
} | ||
} |