Product Context

This commit is contained in:
thermalthrottle
2021-03-18 22:24:12 +02:00
parent bdd0d5256f
commit 9fd0d965d9

View File

@@ -34,45 +34,178 @@ namespace Business.Business.Sales
} }
public void AddItem(string name, double price) public void AddItem(string name, double price)
{ {
var product = new Product(name, price); if (currentUser.RoleId == 3)
context.Product.Add(product); {
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) public void UpdateName(int id, string newName)
{ {
if (currentUser.RoleId > 1) if (currentUser.RoleId == 3)
{ {
var product = Get(id); var product = Get(id);
if (product != null) 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 else
{ {
throw new ArgumentException("Product id not valid!");
} }
} }
else
{
throw new ArgumentException("Insufficient Role!");
}
} }
public void UpdateName(string oldName, string newName) 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) 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) 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!");
}
} }
} }
} }