From 9fd0d965d92665cc291c9c8ea7fefd66fbaa1279 Mon Sep 17 00:00:00 2001 From: thermalthrottle Date: Thu, 18 Mar 2021 22:24:12 +0200 Subject: [PATCH] Product Context --- .../Business/Sales/ProductController.cs | 163 ++++++++++++++++-- 1 file changed, 148 insertions(+), 15 deletions(-) diff --git a/LuminousSales/Business/Business/Sales/ProductController.cs b/LuminousSales/Business/Business/Sales/ProductController.cs index 0cb59cd..3e270db 100644 --- a/LuminousSales/Business/Business/Sales/ProductController.cs +++ b/LuminousSales/Business/Business/Sales/ProductController.cs @@ -34,45 +34,178 @@ namespace Business.Business.Sales } public void AddItem(string name, double price) { - var product = new Product(name, price); - context.Product.Add(product); + if (currentUser.RoleId == 3) + { + if (!GetAll().Where(p => p.Name == name).Any()) + { + if (price > 0) + { + var product = new Product(name, price); + context.Product.Add(product); + context.SaveChanges(); + } + else + { + throw new ArgumentException("Price cannot be negative"); + } + } + else + { + throw new ArgumentException("Item with the given name already exists!"); + } + } + else + { + throw new ArgumentException("Insufficient Role!"); + } } public void UpdateName(int id, string newName) { - if (currentUser.RoleId > 1) + if (currentUser.RoleId == 3) { var product = Get(id); if (product != null) { - + if (!GetAll().Where(p => p.Name == newName).Any()) + { + product.Name = newName; + context.SaveChanges(); + } + else + { + throw new ArgumentException("Item with the given name already exists!"); + } } else { - + throw new ArgumentException("Product id not valid!"); } } + else + { + throw new ArgumentException("Insufficient Role!"); + } } public void UpdateName(string oldName, string newName) { - throw new NotImplementedException(); + if (currentUser.RoleId == 3) + { + var product = Get(oldName); + if (product != null) + { + if (!GetAll().Where(p => p.Name == newName).Any()) + { + product.Name = newName; + context.SaveChanges(); + } + else + { + throw new ArgumentException("Item with the given name already exists!"); + } + } + else + { + throw new ArgumentException("Product name not valid!"); + } + } + else + { + throw new ArgumentException("Insufficient Role!"); + } } - public void UpdatePrice(int id) + public void UpdatePrice(int id, double price) { - + if (currentUser.RoleId == 3) + { + var product = Get(id); + if (product != null) + { + if (price > 0) + { + product.Price = price; + context.SaveChanges(); + } + else + { + throw new ArgumentException("Price cannot be negative"); + } + } + else + { + throw new ArgumentException("Product id not valid!"); + } + } + else + { + throw new ArgumentException("Insufficient Role!"); + } } - public void UpdatePrice(string name) + public void UpdatePrice(string name, double price) { - + if (currentUser.RoleId == 3) + { + var product = Get(name); + if (product != null) + { + if (price > 0) + { + product.Price = price; + context.SaveChanges(); + } + else + { + throw new ArgumentException("Price cannot be negative"); + } + } + else + { + throw new ArgumentException("Product name not valid!"); + } + } + else + { + throw new ArgumentException("Insufficient Role!"); + } } public void Delete(int id) { - throw new NotImplementedException(); + if (currentUser.RoleId == 3) + { + var user = Get(id); + if (user != null) + { + context.Product.Remove(user); + context.SaveChanges(); + } + else + { + throw new ArgumentException("User not found"); + } + } + else + { + throw new ArgumentException("Insufficient Role!"); + } } - public void Delete(string name) { - throw new NotImplementedException(); + if (currentUser.RoleId == 3) + { + var user = Get(name); + if (user != null) + { + context.Product.Remove(user); + context.SaveChanges(); + } + else + { + throw new ArgumentException("User not found"); + } + } + else + { + throw new ArgumentException("Insufficient Role!"); + } } - } -} +} \ No newline at end of file