Skip to content
Merged
2 changes: 1 addition & 1 deletion Test/IntegrationTests/ItemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ItemTest()
SeedData();

var service = new ItemServices(_context);
_controller = new ItemController(service);
_controller = new ItemController(service, null);
var lineService = new ItemLineServices(_context);
_controllerLines = new ItemLinesController(lineService);
var typeService = new ItemTypeServices(_context);
Expand Down
108 changes: 72 additions & 36 deletions api/Controllers/ItemControllers.cs
Original file line number Diff line number Diff line change
@@ -1,94 +1,130 @@
using Microsoft.AspNetCore.Mvc;
using Serilog;
using Newtonsoft.Json;

[ApiController]
[Route("/api/v1/")]
public class ItemController : Controller{
public class ItemController : Controller
{
private readonly ItemServices _item;
public ItemController(ItemServices item)
private readonly ILogger<ItemController> _logger;

public ItemController(ItemServices item, ILogger<ItemController>? logger = null)

Check warning on line 12 in api/Controllers/ItemControllers.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_logger' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 12 in api/Controllers/ItemControllers.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_logger' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 12 in api/Controllers/ItemControllers.cs

View workflow job for this annotation

GitHub Actions / test

Non-nullable field '_logger' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 12 in api/Controllers/ItemControllers.cs

View workflow job for this annotation

GitHub Actions / test

Non-nullable field '_logger' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 12 in api/Controllers/ItemControllers.cs

View workflow job for this annotation

GitHub Actions / test

Non-nullable field '_logger' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 12 in api/Controllers/ItemControllers.cs

View workflow job for this annotation

GitHub Actions / test

Non-nullable field '_logger' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
_item = item;
_logger = logger;

Check warning on line 15 in api/Controllers/ItemControllers.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.

Check warning on line 15 in api/Controllers/ItemControllers.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.

Check warning on line 15 in api/Controllers/ItemControllers.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference assignment.

Check warning on line 15 in api/Controllers/ItemControllers.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference assignment.

Check warning on line 15 in api/Controllers/ItemControllers.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference assignment.

Check warning on line 15 in api/Controllers/ItemControllers.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference assignment.
}

// GET /Items: Returns all Items.
[HttpGet("Items")]
public async Task<IActionResult> GetItems()
{
var items = await _item.GetItems();
return Ok(items);
var items = await _item.GetItems();
return Ok(items);
}

// GET /Item/{uid}: Returns the details of a specific item by its ID.
[HttpGet("Item/{uid}")]
public async Task<IActionResult> GetItemById(string uid){
public async Task<IActionResult> GetItemById(string uid)
{
var item = await _item.GetItemById(uid);
if(item == null){
if (item == null)
{
_logger?.LogInformation("GET /api/v1/Item: Item with id {uid} not found.",uid);
return NotFound("No Item found with that ID");
}
return Ok(item);
}

[HttpPost("Item")]
public async Task<IActionResult> AddItem([FromBody] Item item)
{
try{
try
{
var result = await _item.AddItem(item);
if (result == null){
if (result == null)
{
_logger?.LogInformation("POST /api/v1/Item: Item could not be added or already exists.");
return BadRequest("Item could not be added or already exists.");
}
//_logger.LogInformation("POST /api/v1/Item: Item with ID {Uid} added successfully", item.Uid);
_logger?.LogInformation("POST /api/v1/Item: Item added successfully. Details: {@Item}", item);
return Ok(result);
}
catch (Exception ex){
return BadRequest(ex.Message); // Return the error message in a bad request response
catch (Exception ex)
{
_logger?.LogInformation("POST /api/v1/Item: Item could not be added or already exists.");
return BadRequest(ex.Message);
}
}

// PUT /Item/{uid}: Updates item information.
[HttpPut("Item/{uid}")]
public async Task<IActionResult> UpdateItem([FromRoute]string uid, [FromBody] Item item){
try{
//if(id <= 0 || uid != item.Uid){
// return BadRequest("Item ID is invalid or does not match the item ID in the request body.");
if (uid == null || uid.Length == 0){
public async Task<IActionResult> UpdateItem([FromRoute] string uid, [FromBody] Item item)
{
var oldItem = await _item.GetItemById(uid);
var oldItemSnapshot = JsonConvert.DeserializeObject<Item>(JsonConvert.SerializeObject(oldItem));

try
{
if (string.IsNullOrEmpty(uid))
{
_logger?.LogInformation("PUT /api/v1/Item: Item with id: {uid} could not be updated.", uid);
return BadRequest("Item ID is invalid or does not match the item ID in the request body.");
}

var result = await _item.UpdateItem(uid, item);
if (result == null) {
if (result == null)
{
_logger?.LogInformation("PUT /api/v1/Item: Item with id: {uid} could not be updated.", uid);
return BadRequest("Item could not be updated.");
}
_logger?.LogInformation("PUT /api/v1/Item/{Uid}: Item updated. Old Item: {@OldItem}, New Item: {@UpdatedItem}",uid, oldItemSnapshot, result);

return Ok(result);
}
catch (Exception ex){
return BadRequest(ex.Message); // Return the error message in a bad request response
catch (Exception ex)
{
_logger?.LogInformation("PUT /api/v1/Item: Item with id: {uid} could not be updated.", uid);
return BadRequest(ex.Message);
}
}

// DELETE /Item/ {uid}: Deletes a client.
[HttpDelete("Item/{uid}")]
public async Task<IActionResult> DeleteItem(string uid){
bool itemToDeleted = await _item.DeleteItem(uid);
if(itemToDeleted == false)
return BadRequest("Item could not be deleted.");
return NoContent();
public async Task<IActionResult> DeleteItem(string uid)
{
try
{
bool itemToDeleted = await _item.DeleteItem(uid);
if (itemToDeleted == false)
{
return BadRequest("Item could not be deleted.");
}
_logger?.LogInformation("DELETE /api/v1/Item/{Uid}: Item with ID {Uid} deleted successfully", uid,uid);
return NoContent();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}

// GET /Item/{uid}/Inventory: Returns the inventory of a specific item by its ID.
[HttpGet("Item/{uid}/Inventory")]
public async Task<IActionResult> GetIventoryThroughItems(string uid){
public async Task<IActionResult> GetIventoryThroughItems(string uid)
{
var item = await _item.GetIventoryThroughItems(uid);
if(item == null){
if (item == null)
{
return NotFound("No Item found with that ID");
}
return Ok(item);
}

// GET /Item/{uid}/Totals: Returns the total of a specific item by its ID. // from inventory only the total expected tot available
[HttpGet("Item/{uid}/Inventory/Totals")]
public async Task<IActionResult> GetItemTotalsFromInventory(string uid){
public async Task<IActionResult> GetItemTotalsFromInventory(string uid)
{
var item = await _item.GetItemTotalsFromInventory(uid);
if(item == null){
if (item == null)
{
return NotFound("No Item found with that ID");
}
return Ok(item);
}
}
}
14 changes: 14 additions & 0 deletions api/Program.cs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vergeet die comments niet

Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
using Serilog;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

Log.Logger = new LoggerConfiguration()
.WriteTo.File("logs/app.log", rollingInterval: RollingInterval.Day)
.WriteTo.Console(restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information)
.Filter.ByIncludingOnly(logEvent =>
logEvent.MessageTemplate.Text.Contains("api/v1"))

.CreateLogger();

//nu ziet het er raar uit

builder.Logging.ClearProviders(); //voor geen dubbel code met asp.net
builder.Logging.AddSerilog();

builder.Services.AddControllers();
builder.Services.AddDbContext<MyContext>(x => x.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));

Expand Down
105 changes: 43 additions & 62 deletions api/REST/Item.REST
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
GET http://localhost:5000/api/v1/Items HTTP/1.1
Api-Key: a1b2c3d4e5

### Get Item by uid
### Get Item by uid
GET http://localhost:5000/api/v1/Item/P000001 HTTP/1.1
Api-Key: a1b2c3d4e5

Expand All @@ -12,83 +12,64 @@ Content-Type: application/json
Api-Key: a1b2c3d4e5

{
"Uid": "P000001",
"Code": "sjQ23408K",
"Description": "Face-to-face clear-thinking complexity",
"ShortDescription": "must",
"UpcCode": "6523540947122",
"ModelNumber": "63-OFFTq0T",
"CommodityCode": "oTo304",
"ItemLine": 1,
"ItemGroup": 1,
"ItemType": 2,
"UnitPurchaseQuantity": 47,
"UnitOrderQuantity": 13,
"PackOrderQuantity": 11,
"SupplierId": 1,
"SupplierCode": "SUP423",
"SupplierPartNumber": "E-86805-uTM",
"CreatedAt": "2015-02-19T16:08:24Z",
"UpdatedAt": "2015-09-26T06:37:56Z"
}
### Post Item based on body given
POST http://localhost:5000/api/v1/Item HTTP/1.1
Content-Type: application/json
Api-Key: a1b2c3d4e5

{
"Uid": "P000002",
"Code": "sjQ23408K",
"Description": "Face-to-face clear-thinking complexity",
"ShortDescription": "must",
"UpcCode": "6523540947122",
"ModelNumber": "63-OFFTq0T",
"CommodityCode": "oTo304",
"UnitPurchaseQuantity": 47,
"UnitOrderQuantity": 13,
"PackOrderQuantity": 11,
"SupplierId": 1,
"SupplierCode": "SUP423",
"SupplierPartNumber": "E-86805-uTM",
"CreatedAt": "2015-02-19T16:08:24Z",
"UpdatedAt": "2015-09-26T06:37:56Z"
"uid": "P000001",
"code": "sjQ23408K",
"description": "Face-to-face clear-thinking complexity",
"short_description": "must",
"upc_code": "6523540947122",
"model_number": "63-OFFTq0T",
"commodity_code": "oTo304",
"item_line": 1,
"item_group": 1,
"item_type": 2,
"unit_purchase_quantity": 47,
"unit_order_quantity": 13,
"pack_order_quantity": 11,
"supplier_id": 1,
"supplier_code": "SUP423",
"supplier_part_number": "E-86805-uTM",
"created_at": "2015-02-19T16:08:24Z",
"updated_at": "2015-09-26T06:37:56Z"
}

### Put Update Item based on body given
PUT http://localhost:5000/api/v1/Item/P000001 HTTP/1.1
PUT http://localhost:5000/api/v1/Item/P000001 HTTP/1.1
Content-Type: application/json
Api-Key: a1b2c3d4e5

{
"Uid": "P000001",
"Code": "sjQ23408K",
"Description": "Update worked!!!!!!!!!!!!!!",
"ShortDescription": "must",
"UpcCode": "6523540947122",
"ModelNumber": "63-OFFTq0T",
"CommodityCode": "oTo304",
"ItemLine": 1,
"ItemGroup": 1,
"ItemType": 2,
"UnitPurchaseQuantity": 47,
"UnitOrderQuantity": 13,
"PackOrderQuantity": 11,
"SupplierId": 1,
"SupplierCode": "SUP423",
"SupplierPartNumber": "E-86805-uTM",
"CreatedAt": "2015-02-19T16:08:24Z",
"UpdatedAt": "2015-09-26T06:37:56Z"
"uid": "P000001",
"code": "sjQ23408K",
"description": "Update worked!!!!!!!!!!!!!!",
"short_description": "must",
"upc_code": "6523540947122",
"model_number": "63-OFFTq0T",
"commodity_code": "oTo304",
"item_line": 1,
"item_group": 1,
"item_type": 2,
"unit_purchase_quantity": 47,
"unit_order_quantity": 13,
"pack_order_quantity": 11,
"supplier_id": 1,
"supplier_code": "SUP423",
"supplier_part_number": "E-86805-uTM",
"created_at": "2015-02-19T16:08:24Z",
"updated_at": "2015-09-26T06:37:56Z"
}

### Del Delete Item by uid
### Delete Item by uid
DELETE http://localhost:5000/api/v1/Item/P000001 HTTP/1.1
Api-Key: a1b2c3d4e5

### Delete Item by uid
DELETE http://localhost:5000/api/v1/Item/P000002 HTTP/1.1
Api-Key: a1b2c3d4e5

### Get Inventory by Item uid
GET http://localhost:5000/api/v1/Item/P000001/Inventory HTTP/1.1
Api-Key: a1b2c3d4e5

### Get Inventory Totals by Item uid
GET http://localhost:5000/api/v1/Item/P000001/Inventory/Totals HTTP/1.1
Api-Key: a1b2c3d4e5

3 changes: 3 additions & 0 deletions api/api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion api/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
"Microsoft.AspNetCore": "Warning"
}
}
}
}
2 changes: 1 addition & 1 deletion api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
"ConnectionStrings": {
"DefaultConnection": "Data Source= Database.db"
}
}
}
Loading