//*** public IResponseDto GetAllWithinRange(DateTime startDate, DateTime endDate) { Console.WriteLine(startDate); Func <Order, bool> condition = (order) => order.Date.CompareTo(startDate) >= 0 && order.Date.CompareTo(endDate) <= 0; var orders = _unitOfWork.OrdersRepository.FindAll(condition); return(SuccessResponseDto.Create(_mapper.Map <List <OrderDto> >(orders))); }
// *** public async Task <IResponseDto> CreateOrder(CreateOrderDto request) { bool areAllAvailable = true; var wantedDishesEnumerator = request.WantedDishes.GetEnumerator(); IList <Dish> wantedDishes = new List <Dish>(); IDictionary <Guid, int> wantedDishesCount = new Dictionary <Guid, int>(); while (wantedDishesEnumerator.MoveNext() && areAllAvailable == true) { var wantedDish = await _unitOfWork.DishesRepository.Get(wantedDishesEnumerator.Current.Id); wantedDishes.Add(wantedDish); wantedDishesCount.Add(wantedDish.Id, wantedDishesEnumerator.Current.Count); areAllAvailable = wantedDish.IsAvailable; } wantedDishesEnumerator.Dispose(); if (areAllAvailable == false) { return(ErrorResponseDto.Create("Some dishes are not available.")); } var createdOrder = Order.Create(DateTime.Now, request.Adress); await _unitOfWork.OrdersRepository.Add(createdOrder); foreach (var wantedDish in wantedDishes) { wantedDishesCount.TryGetValue(wantedDish.Id, out int count); var orderDish = OrderDish.Create(wantedDish, createdOrder, count); await _unitOfWork.OrderDishesRepository.Add(orderDish); foreach (var wantedDishIngredient in wantedDish.DishIngredients) { IngredientOnStock usedIngredient = wantedDishIngredient.Ingredient; usedIngredient.Quantity -= wantedDishIngredient.Quantity; await _unitOfWork.IngredientsRepository.Update(wantedDishIngredient.Ingredient); foreach (var usedIngredientDish in wantedDishIngredient.Ingredient.IngredientDishes) { if (usedIngredientDish.Quantity > wantedDishIngredient.Ingredient.Quantity) { usedIngredientDish.Dish.IsAvailable = false; await _unitOfWork.DishesRepository.Update(usedIngredientDish.Dish); } } } } await _unitOfWork.CommitAsync(); // cauta dishurile orderului //verifica daca sunt available // create order // scade din fiecare ingredient folosit in dishurile alese //si pentru fiecare ingredient modificat se verifica dishurile la care se foloseste ingredientul (is available?) return(SuccessResponseDto.Create(_mapper.Map <OrderDto>(createdOrder))); }
public async Task <IResponseDto> Get(string id) { Guid.TryParse(id, out Guid dishId); var searchedDish = await _unityOfWork.DishesRepository.Get(dishId); if (searchedDish == null) { return(ErrorResponseDto.Create(ErrorMessages.IdNotFound)); } return(SuccessResponseDto.Create(_mapper.Map <DishDto>(searchedDish))); }
public async Task <IResponseDto> Create(CreateDishDto request) { IDictionary <Guid, IngredientOnStock> usedIngredients = new Dictionary <Guid, IngredientOnStock>(); bool isAvailable = true; var dishIngredientsEnumerator = request.DishIngredients.GetEnumerator(); while (dishIngredientsEnumerator.MoveNext() && isAvailable == true) { double quantity = dishIngredientsEnumerator.Current.Value; var usedIngredient = await _unityOfWork.IngredientsRepository.Get(dishIngredientsEnumerator.Current.Key); if (usedIngredient == null) { return(null); } usedIngredients.Add(usedIngredient.Id, usedIngredient); if (usedIngredient.Quantity < quantity) { isAvailable = false; } } dishIngredientsEnumerator.Dispose(); Dish createdDish = Dish.Create(request.Name, request.Price, isAvailable); var id = createdDish.Id; await _unityOfWork.DishesRepository.Add(createdDish); dishIngredientsEnumerator = request.DishIngredients.GetEnumerator(); while (dishIngredientsEnumerator.MoveNext()) { DishIngredient link = new DishIngredient() { Dish = createdDish, Ingredient = usedIngredients[dishIngredientsEnumerator.Current.Key], Quantity = dishIngredientsEnumerator.Current.Value }; await _unityOfWork.DishIngredientsRepository.Add(link); Console.WriteLine(link); } dishIngredientsEnumerator.Dispose(); await _unityOfWork.CommitAsync(); var dishDto = _mapper.Map <DishDto>(createdDish); return(SuccessResponseDto.Create(dishDto)); }
// *** public async Task <IResponseDto> GetAll() { var allOrders = await _unitOfWork.OrdersRepository.GetAll(); return(SuccessResponseDto.Create(_mapper.Map <List <OrderDto> >(allOrders))); }
public async Task <IResponseDto> GetAll() { return(SuccessResponseDto.Create(_mapper.Map <IList <DishDto> >(await _unityOfWork.DishesRepository.GetAll()))); }