Пример #1
0
 public IActionResult ChangeOrderStatus(OrderStatusInputViewModel model)
 {
     try
     {
         var item = _orderService.ChangeOrderStatus(model);
         return(Ok(item));
     }
     catch (Exception ex)
     {
         _logger.LogError(ex, "Error occured in ChangeOrderStatus");
         return(BadRequest(ex.Message));
     }
 }
Пример #2
0
        public bool ChangeOrderStatus(OrderStatusInputViewModel input)
        {
            try
            {
                var order = _databaseContext.Orders.Where(x => x.OrderId == input.OrderId).FirstOrDefault();
                if (order != null)
                {
                    switch (input.OrderStatus)
                    {
                    case OrderStatus.BeingPrepared:
                        //if (order.OrderStatus != OrderStatus.NewOrder)
                        //{
                        //    throw new Exception("Status Being Prepared is applicable only for New Order.");
                        //}
                        order.OrderStatus = OrderStatus.BeingPrepared;
                        order.UpdatedBy   = _userInfo.UserId;
                        order.UpdatedOn   = DateTime.Now;
                        _databaseContext.Orders.Update(order);
                        _databaseContext.SaveChanges();
                        break;

                    case OrderStatus.Prepared:
                        //if (order.OrderStatus != OrderStatus.BeingPrepared)
                        //{
                        //    throw new Exception("Status Prepared is applicable only for the items which are already Being Prepared.");
                        //}
                        order.OrderStatus = OrderStatus.Prepared;
                        order.UpdatedBy   = _userInfo.UserId;
                        order.UpdatedOn   = DateTime.Now;
                        _databaseContext.Orders.Update(order);
                        _databaseContext.SaveChanges();
                        break;

                    case OrderStatus.Packing:
                        if (order.OrderType != OrderType.TakeAway)
                        {
                            throw new Exception("This status is invalid for DineIn orders.");
                        }
                        //else if (order.OrderStatus != OrderStatus.Prepared)
                        //{
                        //    throw new Exception("Status Packing is applicable only for the items which are already Prepared.");
                        //}
                        order.OrderStatus = OrderStatus.Packing;
                        order.UpdatedBy   = _userInfo.UserId;
                        order.UpdatedOn   = DateTime.Now;
                        _databaseContext.Orders.Update(order);
                        _databaseContext.SaveChanges();
                        break;

                    case OrderStatus.ReadyToBeDelivered:

                        if (order.OrderType != OrderType.TakeAway)
                        {
                            throw new Exception("This status is invalid for DineIn orders.");
                        }
                        //else if (!(order.OrderStatus >= OrderStatus.Prepared && order.OrderStatus <= OrderStatus.Packing))
                        //{
                        //    throw new Exception("Status Ready To Be Delivered is applicable only for the items which are Prepared or already BeingPacked.");
                        //}
                        order.OrderStatus = OrderStatus.ReadyToBeDelivered;
                        order.UpdatedBy   = _userInfo.UserId;
                        order.UpdatedOn   = DateTime.Now;
                        _databaseContext.Orders.Update(order);
                        _databaseContext.SaveChanges();
                        break;

                    case OrderStatus.Completed:
                        order.OrderStatus = OrderStatus.Completed;
                        order.UpdatedBy   = _userInfo.UserId;
                        order.UpdatedOn   = DateTime.Now;
                        _databaseContext.Orders.Update(order);
                        _databaseContext.SaveChanges();
                        break;

                    case OrderStatus.Cancelled:
                        if (order.OrderStatus != OrderStatus.NewOrder)
                        {
                            throw new Exception("Only new order can be cancelled.");
                        }
                        else if (input.CancellationReason == string.Empty)
                        {
                            throw new Exception("Cancellation reason is mandatory.");
                        }
                        order.OrderStatus = OrderStatus.Cancelled;
                        order.UpdatedBy   = _userInfo.UserId;
                        order.UpdatedOn   = DateTime.Now;
                        _databaseContext.Orders.Update(order);
                        _databaseContext.SaveChanges();
                        break;

                    default:
                        throw new NotImplementedException("The inputed status is invalid.");
                    }
                    return(true);
                }
                else
                {
                    throw new Exception("Order not found");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }