diff --git a/Test/IntegrationTests/ItemTest.cs b/Test/IntegrationTests/ItemTest.cs index e88565e..fee712c 100644 --- a/Test/IntegrationTests/ItemTest.cs +++ b/Test/IntegrationTests/ItemTest.cs @@ -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); diff --git a/api/Controllers/ItemControllers.cs b/api/Controllers/ItemControllers.cs index 6b2162c..0b2300d 100644 --- a/api/Controllers/ItemControllers.cs +++ b/api/Controllers/ItemControllers.cs @@ -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 _logger; + + public ItemController(ItemServices item, ILogger? logger = null) { _item = item; + _logger = logger; } - // GET /Items: Returns all Items. [HttpGet("Items")] public async Task 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 GetItemById(string uid){ + public async Task 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 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 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 UpdateItem([FromRoute] string uid, [FromBody] Item item) + { + var oldItem = await _item.GetItemById(uid); + var oldItemSnapshot = JsonConvert.DeserializeObject(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 DeleteItem(string uid){ - bool itemToDeleted = await _item.DeleteItem(uid); - if(itemToDeleted == false) - return BadRequest("Item could not be deleted."); - return NoContent(); + public async Task 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 GetIventoryThroughItems(string uid){ + public async Task 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 GetItemTotalsFromInventory(string uid){ + public async Task 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); } -} \ No newline at end of file +} diff --git a/api/Program.cs b/api/Program.cs index 7b5cbe4..2b513ca 100644 --- a/api/Program.cs +++ b/api/Program.cs @@ -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(x => x.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection"))); diff --git a/api/REST/Item.REST b/api/REST/Item.REST index 40fee2d..320caa1 100644 --- a/api/REST/Item.REST +++ b/api/REST/Item.REST @@ -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 @@ -12,78 +12,60 @@ 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 @@ -91,4 +73,3 @@ 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 - diff --git a/api/api.csproj b/api/api.csproj index b1831e6..d0fcb8a 100644 --- a/api/api.csproj +++ b/api/api.csproj @@ -28,6 +28,9 @@ + + + diff --git a/api/appsettings.Development.json b/api/appsettings.Development.json index 0c208ae..1b2d3ba 100644 --- a/api/appsettings.Development.json +++ b/api/appsettings.Development.json @@ -5,4 +5,4 @@ "Microsoft.AspNetCore": "Warning" } } -} +} \ No newline at end of file diff --git a/api/appsettings.json b/api/appsettings.json index c9fc64a..77a9d25 100644 --- a/api/appsettings.json +++ b/api/appsettings.json @@ -9,4 +9,4 @@ "ConnectionStrings": { "DefaultConnection": "Data Source= Database.db" } -} +} \ No newline at end of file