public async Task <ActionResult> EditItem(ViewModels.AddItem data)
        {
            var item = await db.Items.Where(x => x.Id == data.Id).SingleOrDefaultAsync();

            if (item != null)
            {
                item.Name         = data.Name ?? item.Name;
                item.Descriptions = data.Descriptions ?? item.Descriptions;
                item.Images       = await Helpers.UploadFileHelper.UploadBannerImage(data.Images) ?? item.Images;

                db.Entry(item).State = EntityState.Modified;
                var result = await db.SaveChangesAsync();

                if (result > 0)
                {
                    return(RedirectToAction("Items"));
                }
            }
            return(View(item));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> AddItem(ViewModels.AddItem data)
        {
            var order = await db.Orders.Include("Items").Include("Items.Item").Where(x => x.Id == data.IdOrder).OrderByDescending(x => x.Created).FirstOrDefaultAsync();

            if (order != null)
            {
                var item = await db.Items.FindAsync(data.Id);

                var exist = order.Items.Where(x => x.Item.Id == data.Id).FirstOrDefault();
                if (exist != null)
                {
                    exist.Amount          = data.Amount;
                    db.Entry(exist).State = EntityState.Modified;
                    var result = await db.SaveChangesAsync();

                    if (result > 0)
                    {
                        return(RedirectToAction("PreviewOrder", new { id = data.IdOrder }));
                    }
                }
                else
                {
                    var addItem = new Models.OrderItem()
                    {
                        Id     = Guid.NewGuid().ToString(),
                        Amount = data.Amount,
                        Item   = item,
                        Unit   = item.Unit
                    };
                    order.Items.Add(addItem);
                    var result = await db.SaveChangesAsync();

                    if (result > 0)
                    {
                        return(RedirectToAction("PreviewOrder", new { id = data.IdOrder }));
                    }
                }
            }
            return(View("Error"));
        }
        public async Task <ActionResult> AddItem(ViewModels.AddItem addItem)
        {
            if (ModelState.IsValid)
            {
                var newItem = new Models.Item()
                {
                    Id           = Guid.NewGuid().ToString(),
                    Amount       = addItem.Amount,
                    Descriptions = addItem.Descriptions,
                    Name         = addItem.Name,
                    Unit         = addItem.Unit,
                    Images       = await Helpers.UploadFileHelper.UploadBannerImage(addItem.Banner)
                };
                db.Items.Add(newItem);
                var result = await db.SaveChangesAsync();

                if (result > 0)
                {
                    return(RedirectToAction("Items"));
                }
            }
            return(View());
        }