-
Notifications
You must be signed in to change notification settings - Fork 1
12.1 items #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
12.1 items #185
Changes from all commits
b715590
7337ec8
0246b2d
95ebdf6
d6eee58
c194419
d84392b
ef38cd1
5bbd881
440fbdb
b56abf0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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
|
||
| { | ||
| _item = item; | ||
| _logger = logger; | ||
|
Check warning on line 15 in api/Controllers/ItemControllers.cs
|
||
| } | ||
|
|
||
| // 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); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,4 +5,4 @@ | |
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,4 +9,4 @@ | |
| "ConnectionStrings": { | ||
| "DefaultConnection": "Data Source= Database.db" | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vergeet die comments niet