Skip to content

Commit

Permalink
Merge pull request #9 from safouuwa/ControllerRefactor
Browse files Browse the repository at this point in the history
API ControllerRefactor
  • Loading branch information
linkverk authored Nov 18, 2024
2 parents 644c1a7 + 1e3bcf7 commit 5ecc726
Show file tree
Hide file tree
Showing 35 changed files with 1,446 additions and 1,447 deletions.
1,356 changes: 0 additions & 1,356 deletions C#api/ApiRequestHandler.cs

This file was deleted.

27 changes: 27 additions & 0 deletions C#api/Controllers/BaseApiController.cs
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

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 25 in C#api/Controllers/BaseApiController.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.
}
}
89 changes: 89 additions & 0 deletions C#api/Controllers/ClientsController.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

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'apiKey' in 'IActionResult BaseApiController.CheckAuthorization(string apiKey, string resource, string operation)'.

Check warning on line 18 in C#api/Controllers/ClientsController.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference argument for parameter 'apiKey' in 'IActionResult BaseApiController.CheckAuthorization(string apiKey, string resource, string operation)'.
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

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'apiKey' in 'IActionResult BaseApiController.CheckAuthorization(string apiKey, string resource, string operation)'.

Check warning on line 28 in C#api/Controllers/ClientsController.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference argument for parameter 'apiKey' in 'IActionResult BaseApiController.CheckAuthorization(string apiKey, string resource, string operation)'.
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

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'apiKey' in 'IActionResult BaseApiController.CheckAuthorization(string apiKey, string resource, string operation)'.

Check warning on line 40 in C#api/Controllers/ClientsController.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference argument for parameter 'apiKey' in 'IActionResult BaseApiController.CheckAuthorization(string apiKey, string resource, string operation)'.
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();
}
}
79 changes: 79 additions & 0 deletions C#api/Controllers/InventoriesController.cs
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

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'apiKey' in 'IActionResult BaseApiController.CheckAuthorization(string apiKey, string resource, string operation)'.

Check warning on line 18 in C#api/Controllers/InventoriesController.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference argument for parameter 'apiKey' in 'IActionResult BaseApiController.CheckAuthorization(string apiKey, string resource, string operation)'.
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

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'apiKey' in 'IActionResult BaseApiController.CheckAuthorization(string apiKey, string resource, string operation)'.

Check warning on line 28 in C#api/Controllers/InventoriesController.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference argument for parameter 'apiKey' in 'IActionResult BaseApiController.CheckAuthorization(string apiKey, string resource, string operation)'.
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();
}
}
89 changes: 89 additions & 0 deletions C#api/Controllers/Item_GroupsController.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 Item_GroupsController : BaseApiController
{
public Item_GroupsController(
NotificationSystem notificationSystem)
: base(notificationSystem)
{
}

[HttpGet]
public IActionResult GetItemGroups()
{
var auth = CheckAuthorization(Request.Headers["API_KEY"], "item_groups", "get");

Check warning on line 18 in C#api/Controllers/Item_GroupsController.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'apiKey' in 'IActionResult BaseApiController.CheckAuthorization(string apiKey, string resource, string operation)'.
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();
}
}
89 changes: 89 additions & 0 deletions C#api/Controllers/Item_LinesController.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 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();
}
}
Loading

0 comments on commit 5ecc726

Please sign in to comment.