stock controller

This commit is contained in:
Aneliya Konarcheva
2021-03-18 23:42:49 +02:00
parent 9fd0d965d9
commit 128a243133

View File

@@ -8,100 +8,100 @@ using Models.Models;
namespace Business.Business.Sales namespace Business.Business.Sales
{ {
public class StockController : ISalesController<Product> public class StockController : ISalesController<Stock>
{ {
private LuminousContext context; private LuminousContext context = new LuminousContext();
private User currentUser;
private ProductController productCtrl;
Product ISalesController<Product>.Get(int id) public StockController(User currentUser)
{ {
return context.Product.Find(id); this.currentUser = currentUser;
} }
ICollection<Product> ISalesController<Product>.GetByTime(DateTime time)
public ICollection<Stock> GetAll()
{ {
throw new NotImplementedException(); if (currentUser.RoleId > 1)
} return context.Stock.ToList();
void ISalesController<Product>.Add(int productId, double Amount) else throw new InvalidOperationException("Cannot return all stocks!");
{
throw new NotImplementedException();
}
void ISalesController<Product>.Add(string productName, double Amount)
{
throw new NotImplementedException();
}
void ISalesController<Product>.Delete(int id)
{
throw new NotImplementedException();
}
public ICollection<Product> GetAll()
{
using (context = new LuminousContext())
{
return context.Product.ToList();
}
}
public void AddProduct(Product product)
{
using (context = new LuminousContext())
{
context.Product.Add(product);
context.SaveChanges();
}
} }
public void LoadProduct(int id)
public Stock Get(int id)
{ {
using (context = new LuminousContext()) if (currentUser.RoleId > 1)
{ {
var item = context.Product.Find(id); return context.Stock.Find(id);
if (item != null) }
else throw new InvalidOperationException("Cannot get stock!");
}
public ICollection<Stock> GetByTime(DateTime time)
{
throw new NotImplementedException();
}
public void Add(int productId, double Amount)
{
if (currentUser.RoleId > 1)
{
if (Amount > 0)
{ {
context.Entry(item).CurrentValues.SetValues(id); var stock = new Stock(currentUser.Id, productId, Amount);
context.Stock.Add(stock);
context.SaveChanges(); context.SaveChanges();
} }
else
{
throw new ArgumentException("Amount cannot be negative");
}
}
else throw new ArgumentException("Insufficient role!");
}
public void Add(string productName, double Amount)
{
if (currentUser.RoleId > 1)
{
if (Amount > 0)
{
productCtrl = new ProductController(currentUser);
var productId = Get(productName);
var stock = new Stock(currentUser.Id, productId , Amount);
context.Stock.Add(stock);
context.SaveChanges();
}
else throw new ArgumentException("Amount cannot be negative");
}
else
{
throw new ArgumentException("Insufficient role!");
} }
} }
public void LoadProduct(Product product) public void Delete(int id)
{ {
using (context = new LuminousContext()) if (currentUser.RoleId > 1)
{ {
var item = context.Product.Find(product.Id); var user = Get(id);
if (item !=null) if (user != null)
{ {
context.Entry(item).CurrentValues.SetValues(product); context.Stock.Remove(user);
context.SaveChanges(); context.SaveChanges();
} }
} else
}
public void Sale(int id)
{
using (context = new LuminousContext())
{
var product = context.Product.Find(id);
if (product != null)
{ {
context.Product.Remove(product); throw new ArgumentException("User not found");
context.SaveChanges();
} }
} }
} else
public void Sale(string name)
{
using (context = new LuminousContext())
{ {
var product = context.Product.Find(name); throw new ArgumentException("Insufficient role!");
if (product !=null)
{
context.Product.Remove(product);
context.SaveChanges();
}
} }
} }
} }
} }