64 lines
1.6 KiB
C#
64 lines
1.6 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Models.Models;
|
|
|
|
namespace Business
|
|
{
|
|
public class ProductBusiness
|
|
{
|
|
private ProductContext productContext;
|
|
|
|
public List<Product> GetAll()
|
|
{
|
|
using (productContext = new ProductContext())
|
|
{
|
|
return productContext.Products.ToList();
|
|
}
|
|
}
|
|
|
|
public Product Get(int id)
|
|
{
|
|
using (productContext = new ProductContext())
|
|
{
|
|
return productContext.Products.Find(id);
|
|
}
|
|
}
|
|
|
|
public void Buy(Product product)
|
|
{
|
|
using (productContext = new ProductContext())
|
|
{
|
|
productContext.Products.Add(product);
|
|
productContext.SaveChanges();
|
|
}
|
|
}
|
|
|
|
public void Update(Product product)
|
|
{
|
|
using (productContext = new ProductContext())
|
|
{
|
|
var item = productContext.Products.Find(product.Id);
|
|
if (item != null)
|
|
{
|
|
productContext.Entry(item).CurrentValues.SetValues(product);
|
|
productContext.SaveChanges();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Sell(int id)
|
|
{
|
|
using (productContext = new ProductContext())
|
|
{
|
|
var product = productContext.Products.Find(id);
|
|
if (product != null)
|
|
{
|
|
productContext.Products.Remove(product);
|
|
productContext.SaveChanges();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|