Пример #1
0
        public Product Add(Product product)
        {
            product.LastUpdate = DateTime.Now;

            var context = new ProductsDAL();

            context.Products.Add(product);
            context.SaveChanges();
            return(context.Products.FirstOrDefault(x => x.Name == product.Name));
        }
Пример #2
0
        public void Delete(int id)
        {
            var context = new ProductsDAL();
            var product = context.Products.FirstOrDefault(x => x.Id == id);

            if (product == null)
            {
                throw new Exception("Product was not found");
            }
            context.Products.Remove(product);
            context.SaveChanges();
        }
Пример #3
0
        public Product Update(Product product)
        {
            var     context      = new ProductsDAL();
            Product savedProduct = context.Products.FirstOrDefault(x => x.Id == product.Id);

            if (savedProduct == null)
            {
                throw new Exception("Product was not found");
            }

            savedProduct.Name       = product.Name;
            savedProduct.Image      = product.Image;
            savedProduct.Price      = product.Price;
            savedProduct.LastUpdate = DateTime.Now;

            context.SaveChanges();
            return(context.Products.FirstOrDefault(x => x.Id == product.Id));
        }
Пример #4
0
        public Product GetById(int id)
        {
            var context = new ProductsDAL();

            return(context.Products.FirstOrDefault(x => x.Id == id));
        }
Пример #5
0
        public IEnumerable <Product> GetAll()
        {
            var context = new ProductsDAL();

            return(context.Products.ToList());
        }