diff --git a/Areas/Inventory/Models/CompanyModel.cs b/Areas/Inventory/Models/CompanyModel.cs index 617765a..d898041 100644 --- a/Areas/Inventory/Models/CompanyModel.cs +++ b/Areas/Inventory/Models/CompanyModel.cs @@ -1,4 +1,5 @@ using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace PSTW_CentralSystem.Areas.Inventory.Models { @@ -7,5 +8,6 @@ namespace PSTW_CentralSystem.Areas.Inventory.Models [Key] public int CompanyId { get; set; } public required string Name { get; set; } + public virtual ICollection? Departments { get; set; } } } diff --git a/Areas/Inventory/Models/ItemModel.cs b/Areas/Inventory/Models/ItemModel.cs index 99a7fc2..069de0b 100644 --- a/Areas/Inventory/Models/ItemModel.cs +++ b/Areas/Inventory/Models/ItemModel.cs @@ -6,7 +6,7 @@ namespace PSTW_CentralSystem.Areas.Inventory.Models public class ItemModel { [Key] - public required string ItemID { get; set; } + public string? ItemID { get; set; } public required int CompanyId { get; set; } public required int DepartmentId { get; set; } public required int ProductId { get; set; } diff --git a/Areas/Inventory/Models/ProductModel.cs b/Areas/Inventory/Models/ProductModel.cs index eb64bca..71a4bdf 100644 --- a/Areas/Inventory/Models/ProductModel.cs +++ b/Areas/Inventory/Models/ProductModel.cs @@ -15,5 +15,6 @@ namespace PSTW_CentralSystem.Areas.Inventory.Models public required string ImageProduct { get; set; } [ForeignKey("ManufacturerId")] public virtual ManufacturerModel? Manufacturer { get; set; } + public virtual ICollection? Items { get; set; } // Navigation property> } } diff --git a/Areas/Inventory/Views/Item/ItemRegistration.cshtml b/Areas/Inventory/Views/Item/ItemRegistration.cshtml index c028496..2238476 100644 --- a/Areas/Inventory/Views/Item/ItemRegistration.cshtml +++ b/Areas/Inventory/Views/Item/ItemRegistration.cshtml @@ -4,279 +4,244 @@ Layout = "~/Views/Shared/_Layout.cshtml"; } - -
-
- -
-
- @*Left Side*@ -
- -

Welcome

-

Registration Product! Click button to go Product Page

- Product Registration
-
+@await Html.PartialAsync("~/Areas/Inventory/Views/_InventoryPartial.cshtml"); +
+
+ +
+
+ @*Left Side*@ +
+ +

Welcome

+

Registration Product! Click button to go Product Page

+ Product Registration
+
- @*Right Side*@ -
-
-
-

REGISTRATION ITEM

-
-
+ @*Right Side*@ +
+
+
+

REGISTRATION ITEM

+
+
- -
- -
- @@ -124,6 +124,7 @@ showOtherManufacturer: false, imageSrc: '', products: null, + productDatatable: null, } }, mounted() { @@ -134,7 +135,7 @@ methods: { initiateTable() { console.log(this.products) - this.manufacturerDatatable = $('#productTable').DataTable({ + this.productDatatable = $('#productDatatable').DataTable({ "data": this.products, "columns": [ { "title": "Product Name", @@ -146,7 +147,7 @@ { "title": "Manufacturer", "data": "manufacturer.manufacturerName", }, - { "title": "productName Category", + { "title": "Product Category", "data": "category", }, { "title": "Product Stock", @@ -154,21 +155,28 @@ }, { "title": "Image", "data": "imageProduct", - }, - { "title": "Delete", "render": function (data, type, full, meta) { - var deleteButton = ``; - return deleteButton; + var image = ` + Image + `; + return image; }, - "width": '10%', }, + { + "title": "Delete", + "render": function (data, type, full, meta) { + var deleteButton = ``; + return deleteButton; + }, + } + ], }) self = this; // Attach click event listener to the delete buttons - $('#manufacturerTable tbody').on('click', '.delete-btn', function () { - const manufacturerId = $(this).data('id'); // Get the manufacturer ID from the button - self.deleteManufacturer(manufacturerId); // Call the Vue method + $('#productDatatable tbody').on('click', '.delete-btn', function () { + const productId = $(this).data('id'); // Get the manufacturer ID from the button + self.deleteProduct(productId); // Call the Vue method }); this.loading = false; @@ -207,7 +215,7 @@ async addProduct() { const existingProduct = this.products.find(p => p.modelNo === this.modelNo); if (existingProduct) { - alert('Product Error', `The model number ${this.modelNo} already exists.`, 'error'); + alert(`Product Error: The model number ${this.modelNo} already exists.`, 'error'); return; // Exit early if the modelNo exists } @@ -247,7 +255,9 @@ this.errorMessage = 'Error: ' + (errorData.message || 'Unknown error'); } else { + this.products = await response.json(); alert('Success!', 'Product form has been successfully submitted.', 'success'); + this.fillTable(this.products); this.resetForm(); } @@ -301,6 +311,48 @@ this.imageProduct = null; } }, + async deleteProduct(productId) { + if (!confirm("Are you sure you want to delete this product?")) { + return; + } + try { + const response = await fetch(`/InvMainAPI/DeleteProduct/${productId}`, { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + }, + }); + const result = await response.json(); + + if (result.success) { + alert(result.message); + // Remove the row from DataTables + this.productDatatable + .row($(`.delete-btn[data-id="${productId}"]`).closest('tr')) + .remove() + .draw(); + } else { + alert(result.message); + } + } + catch (error) { + console.error("Error deleting product:", error); + alert("An error occurred while deleting the product."); + } + finally { + this.loading = false; + } + }, + fillTable(data){ + if (!this.productDatatable) { + console.error("DataTable not initialized"); + return; + } + this.productDatatable.clear(); + this.productDatatable.rows.add(data); + this.productDatatable.draw(); + this.loading = false; + }, } }); diff --git a/Controllers/API/Inventory/InvMainAPI.cs b/Controllers/API/Inventory/InvMainAPI.cs index 1903b28..6dcc84f 100644 --- a/Controllers/API/Inventory/InvMainAPI.cs +++ b/Controllers/API/Inventory/InvMainAPI.cs @@ -23,6 +23,7 @@ namespace PSTW_CentralSystem.Controllers.API.Inventory _logger = logger; _authDbContext = authDbContext; } + #region Manufacturer [HttpPost("ManufacturerList")] @@ -74,6 +75,7 @@ namespace PSTW_CentralSystem.Controllers.API.Inventory return Ok(new { success = true, message = "Manufacturer deleted successfully" }); } + #endregion Manufacturer #region Product @@ -81,10 +83,22 @@ namespace PSTW_CentralSystem.Controllers.API.Inventory [HttpPost("ProductList")] public async Task ProductList() { - var productList = await _authDbContext.Products.Include("Manufacturer").Where(x => x.ManufacturerId == x.ManufacturerId).ToListAsync(); + var productList = await _authDbContext.Products.Include("Manufacturer").ToListAsync(); return Json(productList); } + [HttpPost("ProductListWithItem")] + public async Task ProductListWithItem() + { + var productList = await _authDbContext.Products + .Include(p => p.Items) // Include related items + .Include(p => p.Manufacturer) // Include related manufacturer + .ToListAsync(); + + return Json(productList); + } + + [HttpPost("AddProduct")] public async Task AddProduct([FromBody] ProductModel product) { @@ -104,13 +118,13 @@ namespace PSTW_CentralSystem.Controllers.API.Inventory if (!string.IsNullOrEmpty(product.ImageProduct)) { var bytes = Convert.FromBase64String(product.ImageProduct); - var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/media/inventory/images", product.ModelNo); + var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/media/inventory/images", product.ModelNo + ".jpg"); await System.IO.File.WriteAllBytesAsync(filePath, bytes); - product.ImageProduct = "/media/inventory/images/" + product.ModelNo; + product.ImageProduct = "/media/inventory/images/" + product.ModelNo + ".jpg"; } _authDbContext.Products.Add(product); await _authDbContext.SaveChangesAsync(); - var updatedList = await _authDbContext.Manufacturers.Include("Manufacturer").Where(x => x.ManufacturerId == x.ManufacturerId).ToListAsync(); + var updatedList = await _authDbContext.Products.Include("Manufacturer").Where(x => x.ManufacturerId == x.ManufacturerId).ToListAsync(); return Json(updatedList); } catch (Exception ex) @@ -122,18 +136,78 @@ namespace PSTW_CentralSystem.Controllers.API.Inventory [HttpDelete("DeleteProduct/{id}")] public async Task DeleteProduct(int id) { - var Product = await _authDbContext.Manufacturers.FindAsync(id); + var Product = await _authDbContext.Products.FindAsync(id); if (Product == null) { return NotFound(new { success = false, message = "Product not found" }); } - _authDbContext.Manufacturers.Remove(Product); + _authDbContext.Products.Remove(Product); await _authDbContext.SaveChangesAsync(); return Ok(new { success = true, message = "Product deleted successfully" }); } #endregion Product + + #region Company + [HttpPost("CompanyDepartmentList")] + public async Task CompanyDepartmentList() + { + var productList = await _authDbContext.Companies.Include("Departments").ToListAsync(); + return Json(productList); + } + #endregion Company + + #region Item + + [HttpPost("ItemList")] + public async Task ItemList() + { + var itemList = await _authDbContext.Items.ToListAsync(); + return Json(itemList); + } + + [HttpPost("AddItem")] + public async Task AddItem([FromBody] ItemModel item) + { + if (!ModelState.IsValid) + { + return BadRequest(ModelState); + } + if (item == null) + { + return NotFound("Item is null"); + } + + try + { + _authDbContext.Items.Add(item); + await _authDbContext.SaveChangesAsync(); + var updatedList = await _authDbContext.Items.ToListAsync(); + return Json(updatedList); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + } + + [HttpDelete("DeleteItem/{id}")] + public async Task DeleteItem(int id) + { + var item = await _authDbContext.Items.FindAsync(id); + if (item == null) + { + return NotFound(new { success = false, message = "Item not found" }); + } + + _authDbContext.Items.Remove(item); + await _authDbContext.SaveChangesAsync(); + + return Ok(new { success = true, message = "Item deleted successfully" }); + } + + #endregion Item } } diff --git a/PSTW_CentralSystem.csproj b/PSTW_CentralSystem.csproj index 4a74941..af4e93f 100644 --- a/PSTW_CentralSystem.csproj +++ b/PSTW_CentralSystem.csproj @@ -30,7 +30,6 @@ - diff --git a/wwwroot/Media/Inventory/Images/ThermoSo2.jpg b/wwwroot/Media/Inventory/Images/ThermoSo2.jpg new file mode 100644 index 0000000..12192bb Binary files /dev/null and b/wwwroot/Media/Inventory/Images/ThermoSo2.jpg differ diff --git a/wwwroot/Media/Inventory/Images/ThermoSo2V2.jpg b/wwwroot/Media/Inventory/Images/ThermoSo2V2.jpg new file mode 100644 index 0000000..d020daf Binary files /dev/null and b/wwwroot/Media/Inventory/Images/ThermoSo2V2.jpg differ diff --git a/wwwroot/Media/Inventory/Images/YSISonde1 b/wwwroot/Media/Inventory/Images/YSISonde1.jpg similarity index 100% rename from wwwroot/Media/Inventory/Images/YSISonde1 rename to wwwroot/Media/Inventory/Images/YSISonde1.jpg