Beispiel #1
0
        public async Task <IActionResult> Create([Bind("FridgeId")] Guid fridgeId, RorderViewModel model)
        {
            if (ModelState.IsValid)
            {
                var order = new Order
                {
                    Id               = Guid.NewGuid(),
                    FridgeId         = fridgeId,
                    DeliveryInterval = model.DeliveryInterval,
                    IsReccuring      = true,
                    StartDate        = model.StartDate,
                    NextDeliveryDate = model.StartDate.AddDays(model.DeliveryInterval)
                };
                var orderItem = new OrderItem
                {
                    ItemId   = model.ItemId,
                    OrderId  = order.Id,
                    Quantity = model.Quantity
                };

                _context.Orders.Add(order);
                _context.OrderItems.Add(orderItem);

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(List), new { fridgeId }));
            }

            ViewData["Items"] = new SelectList(_context.Items, "Id", "Name");
            ViewBag.FridgeId  = fridgeId;
            return(View(model));
        }
Beispiel #2
0
        public async Task <IActionResult> Edit(Guid id, RorderViewModel model)
        {
            if (ModelState.IsValid)
            {
                var order = _context.Orders.Include(o => o.OrderItems).First(o => o.Id == id);
                order.OrderItems.First().Quantity = model.Quantity;
                order.DeliveryInterval = model.DeliveryInterval;
                try
                {
                    _context.Update(order);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!OrderExists(order.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(List), new { order.FridgeId }));
            }

            ViewData["Items"] = new SelectList(_context.Items, "Id", "Name");
            return(View(model));
        }