Esempio n. 1
0
        public ActionResult DeleteProduct(int id)
        {
            Product dproduct = _context.Products.Include("Photos").FirstOrDefault(x => x.ID == id);

            if (dproduct != null)
            {
                _context.Products.Remove(dproduct);
                foreach (var x in dproduct.Photos)
                {
                    InitStaticFiles.DeleteImageByFileName(_env, _configuration,
                                                          new string[] { "ImagesPath", "ImagesPathProduct" },
                                                          x.Path);
                }
                _context.SaveChanges();
                return(Ok());
            }
            else
            {
                return(BadRequest());
            }
        }
Esempio n. 2
0
        public async Task <ActionResult <Product> > Edit(int id, [FromBody] ProductModel model)
        {
            Product product = _context.Products.Include("Photos").FirstOrDefault(x => x.ID == id);

            using (CTX db = _context)
            {
                if (product != null)
                {
                    List <Photo> added_photos = new List <Photo>();
                    try
                    {
                        foreach (var x in product.Photos)
                        {
                            InitStaticFiles.DeleteImageByFileName(_env, _configuration,
                                                                  new string[] { "ImagesPath", "ImagesPathProduct" },
                                                                  x.Path);
                        }
                        foreach (var photo in model.ImgsBase64)
                        {
                            string imageName = Path.GetRandomFileName() + ".jpg";

                            string pathSaveImages = InitStaticFiles
                                                    .CreateImageByFileName(_env, _configuration,
                                                                           new string[] { "ImagesPath", "ImagesPathProduct" },
                                                                           imageName,
                                                                           photo);
                            added_photos.Add(new Photo
                            {
                                Path = imageName
                            });
                        }
                    }
                    catch (Exception)
                    {
                        return(BadRequest());
                    }

                    product.Name        = model.Name;
                    product.Description = model.Description;
                    product.Price       = model.Price;
                    product.Photos.Clear();
                    product.Photos = added_photos;


                    db.SaveChanges();
                    return(Ok(product));
                }
                else
                {
                    return(BadRequest());
                }
            }
        }
Esempio n. 3
0
        public async Task <ActionResult <Order> > PostOrder([FromBody] OrderModel order)
        {
            Order o;

            try
            {
                List <OrderItem> products = new List <OrderItem>();
                foreach (var x in order.Products)
                {
                    OrderItem a = new OrderItem
                    {
                        Product   = _context.Products.FirstOrDefault(p => p.ID == x.ID),
                        ProductId = _context.Products.FirstOrDefault(p => p.ID == x.ID).ID
                    };
                    products.Add(a);
                }

                o = new Order
                {
                    Name        = order.Name,
                    Surname     = order.Surname,
                    City        = order.City,
                    Delivery    = order.Delivery,
                    DeliveryNum = order.DeliveryNum,
                    Number      = order.Number,
                    TotalPrice  = order.TotalPrice,
                    Items       = products,
                    isDone      = false
                };
                _context.Order.Add(o);
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
            return(Ok(o.ID));

            /*_context.Order.Add(order);
             * await _context.SaveChangesAsync();
             *
             * return CreatedAtAction("GetOrder", new { id = order.ID }, order);*/
        }
Esempio n. 4
0
        public async Task <IActionResult> markAsDone(int id)
        {
            Order order = _context.Order.FirstOrDefault(x => x.ID == id);

            using (CTX db = _context)
            {
                // Редактирование
                if (order != null)
                {
                    order.isDone = true;
                    db.SaveChanges();
                    return(Ok());
                }
                else
                {
                    return(BadRequest());
                }
                // выводим данные после обновления
            }
        }
Esempio n. 5
0
        public async Task <IActionResult> AddInvoice([FromBody] InvoiceModel invoice)
        {
            Order order = _context.Order.FirstOrDefault(x => x.ID == invoice.ID);

            using (CTX db = _context)
            {
                // Редактирование
                if (order != null)
                {
                    order.Invoice = invoice.Invoice;
                    db.SaveChanges();
                    return(Ok(invoice));
                }
                else
                {
                    return(BadRequest());
                }
                // выводим данные после обновления
            }
        }
Esempio n. 6
0
 public void RemoveAsync(Cliente cliente)
 {
     CTX.Clientes.Remove(cliente);
     CTX.SaveChanges();
 }
Esempio n. 7
0
 public void Add(TEntity obj)
 {
     db.Set <TEntity>().Add(obj);
     db.SaveChanges();
 }
Esempio n. 8
0
        //public int Delete(Guid key)
        //{
        //    throw new NotImplementedException();
        //}

        //public T GetSingle(Guid key)
        //{
        //    throw new NotImplementedException();
        //}

        //public T GetSingle(Guid key, Func<IQueryable<T>, IQueryable<T>> includeFunc)
        //{
        //    throw new NotImplementedException();
        //}

        //public Task<T> GetSingleAsync(Guid key)
        //{
        //    throw new NotImplementedException();
        //}

        public int SaveChange()
        {
            return(dbContext.SaveChanges());
        }