public async Task <CreateOrderVm> Handle(CreateOrderCommand request, CancellationToken cancellationToken)
        {
            var restaurant = await _context.Restaurants.FirstAsync(r => r.Id == request.RestaurantId);

            var user = await _context.Users.FirstAsync(u => u.Id == request.OwnerId);

            var entity = new Order
            {
                Restaurant  = restaurant,
                Owner       = user,
                OrderStatus = OrderStatus.Open,
                Created     = DateTime.Now
            };

            _context.Orders.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            var vm = new CreateOrderVm
            {
                OrderId = entity.Id
            };

            return(vm);
        }
        public async Task <Unit> Handle(SetOrderStatusCommand request, CancellationToken cancellationToken)
        {
            var order = await _context.Orders.FirstOrDefaultAsync(o => o.Id == request.OrderId);

            order.OrderStatus = request.OrderStatus;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(UpdateDeliveryCostCommand request, CancellationToken cancellationToken)
        {
            var order = await _context.Orders.FirstAsync(r => r.Id == request.OrderId);

            order.DeliveryCost = request.DeliveryCost;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(UpdateRestaurantCommand request, CancellationToken cancellationToken)
        {
            var restaurant = _context.Restaurants.FirstOrDefault(u => u.Id == request.Id);

            restaurant.Title = request.Title;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Пример #5
0
        public async Task <Unit> Handle(UpdateUserCommand request, CancellationToken cancellationToken)
        {
            var user = _context.Users.FirstOrDefault(u => u.Id == request.Id);

            user.Name        = request.Name;
            user.PhoneNumber = request.PhoneNumber;
            user.BankName    = request.BankName;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Пример #6
0
        public async Task <Unit> Handle(AddFoodCommand request, CancellationToken cancellationToken)
        {
            var restaurant = await _context.Restaurants
                             .Include(r => r.Foods)
                             .FirstAsync(r => r.Id == request.RestaurantId);

            var food = _mapper.Map <Food>(request.Food);

            restaurant.Foods.Add(food);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Пример #7
0
        public async Task <Unit> Handle(DeleteFoodCommand request, CancellationToken cancellationToken)
        {
            var restaurant = await _context.Restaurants
                             .Include(r => r.Foods)
                             .FirstAsync(r => r.Id == request.RestaurantId);

            var food = restaurant.Foods.FirstOrDefault(f => f.Id == request.Food.Id);

            restaurant.Foods.Remove(food);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Пример #8
0
        public async Task <Unit> Handle(SetUserPaidStatusCommand request, CancellationToken cancellationToken)
        {
            var order = await _context.Orders
                        .Include(o => o.UserOrders).ThenInclude(uo => uo.User)
                        .FirstAsync(r => r.Id == request.OrderId);

            var userOrder = order.UserOrders.FirstOrDefault(uo => uo.User.Id == request.UserId);

            userOrder.IsOrderPaid = request.IsPaid;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Пример #9
0
        public async Task <Unit> Handle(CreateUserCommand request, CancellationToken cancellationToken)
        {
            var entity = new User
            {
                Name        = request.Name,
                PhoneNumber = request.PhoneNumber,
                BankName    = request.BankName
            };

            _context.Users.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Пример #10
0
        public async Task <Unit> Handle(EditFoodCommand request, CancellationToken cancellationToken)
        {
            var restaurant = await _context.Restaurants
                             .Include(r => r.Foods)
                             .FirstAsync(r => r.Id == request.RestaurantId);

            var food = restaurant.Foods.FirstOrDefault(f => f.Id == request.Food.Id);

            food.Title       = request.Food.Title;
            food.Description = request.Food.Description;
            food.Cost        = request.Food.Cost;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(AddFoodOrderCommand request, CancellationToken cancellationToken)
        {
            var order = await _context.Orders
                        .Include(o => o.UserOrders)
                        .ThenInclude(uo => uo.FoodOrders)
                        .FirstAsync(o => o.Id == request.OrderId);

            var user = await _context.Users.FirstAsync(u => u.Id == request.FoodOrder.UserId);

            var food    = order.Restaurant.Foods.First(f => f.Id == request.FoodOrder.FoodId);
            var comment = string.IsNullOrEmpty(request.FoodOrder.Comment?.Trim())
                                ? null
                                : request.FoodOrder.Comment?.Trim();
            var foodOrder = new FoodOrder
            {
                Food    = food,
                Count   = request.FoodOrder.Count,
                Comment = comment
            };

            var userOrder = order.UserOrders?.FirstOrDefault(o => o.User == user);

            if (userOrder == null)
            {
                if (order.UserOrders == null)
                {
                    order.UserOrders = new List <UserOrder>();
                }

                userOrder = new UserOrder()
                {
                    User       = user,
                    FoodOrders = new List <FoodOrder> {
                        foodOrder
                    }
                };
                order.UserOrders.Add(userOrder);
            }
            else
            {
                userOrder.FoodOrders.Add(foodOrder);
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <CreateRestaurantVm> Handle(CreateRestaurantCommand request, CancellationToken cancellationToken)
        {
            var entity = new Restaurant
            {
                Id    = new System.Guid(),
                Title = request.Title
            };

            _context.Restaurants.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            var result = new CreateRestaurantVm()
            {
                Id = entity.Id
            };

            return(result);
        }
        public async Task <Unit> Handle(UpdateFoodOrderCommand request, CancellationToken cancellationToken)
        {
            var order = await _context.Orders
                        .Include(o => o.UserOrders)
                        .ThenInclude(uo => uo.FoodOrders)
                        .FirstAsync(o => o.Id == request.OrderId);

            var user = await _context.Users.FirstAsync(u => u.Id == request.FoodOrder.UserId);

            var food = order.Restaurant.Foods.First(f => f.Id == request.FoodOrder.FoodId);

            var userOrder = order.UserOrders?.FirstOrDefault(o => o.User == user);
            var foodOrder = userOrder.FoodOrders.FirstOrDefault(fo => fo.Id == request.FoodOrder.Id);

            foodOrder.Food    = food;
            foodOrder.Count   = request.FoodOrder.Count;
            foodOrder.Comment = request.FoodOrder.Comment;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(DeleteFoodOrderCommand request, CancellationToken cancellationToken)
        {
            var order = await _context.Orders
                        .Include(o => o.UserOrders)
                        .ThenInclude(uo => uo.FoodOrders)
                        .FirstAsync(o => o.Id == request.OrderId);

            var user = await _context.Users.FirstAsync(u => u.Id == request.FoodOrder.UserId);

            var userOrder = order.UserOrders?.FirstOrDefault(o => o.User == user);
            var foodOrder = userOrder.FoodOrders.FirstOrDefault(fo => fo.Id == request.FoodOrder.Id);

            userOrder.FoodOrders.Remove(foodOrder);

            if (!userOrder.FoodOrders.Any())
            {
                order.UserOrders.Remove(userOrder);
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }