public void TestAddOrder(OrderToAddDTO order) { // arrange _dbContext = _dbContext ?? Configurations.GetDbContext(); _mapperProvider = _mapperProvider ?? Configurations.GetMapperProvider(); _mapper = _mapper ?? Configurations.GetMapper(); // act var result = new OrderRepo(_dbContext, _mapperProvider, _mapper).AddOrder(order); // assert Assert.IsType <ResultDTO_AddOrder>(result); if (result.SkuIdsOverStock.Any()) { Assert.True(result.OrderId == -1); } else { Assert.True(result.OrderId > 0); Assert.All(result.Skus, sku => { Assert.True(sku.Quantity == 0); }); Assert.True(result.StyleStates.First().SoldOut); } }
public ActionResult <ResultDTO_AddOrder> AddOrder(OrderToAddDTO order) { try { var result = _orderRepo.AddOrder(order); return(result); } catch (Exception e) { // TODO log(e) Console.WriteLine(e); return(new BadRequestResult()); } }
public async Task <IActionResult> AddOrderToQueue(OrderToAddDTO order) { return(await AddItemResponseHandler(async() => await service.AddOrderToQueue(order))); }
public async Task <ApiResponse <bool> > ReassignOrderToDifferentQueue(string orderId, string newQueueId) { OrderToReassign orderToReassign = new OrderToReassign(); orderToReassign.OrderId = Int32.Parse(orderId); orderToReassign.NewQueueId = Int32.Parse(newQueueId); //Return Barber Name, barber status, barber queue status, total waiting time (orders) var result = new ApiResponse <bool>(); Order orderToUpdate = new Order(); OrderToAddDTO orderToCreate = new OrderToAddDTO(); try { var orderToFetch = await unitOfWork.OrdersManager.GetAsync(b => b.Id == orderToReassign.OrderId, includeProperties : "OrderServices"); var order = orderToFetch.FirstOrDefault(); if (order != null) { orderToCreate.BarberQueueId = orderToReassign.NewQueueId.Value; orderToCreate.CustomerId = order.CustomerId; orderToCreate.OrderServices = mapper.Map <List <OrderServiceToAddDTO> >(order.OrderServices); var isRemoved = await RemoveOrder(order.Id, order.CustomerId); if (isRemoved.Succeeded) { var orderAdditionResult = await AddOrderToQueue(orderToCreate); if (orderAdditionResult.Succeeded) { result.Data = true; result.Succeeded = true; return(result); } else { result.Errors.Add("Error adding order to new queue !"); result.Succeeded = false; return(result); } } else { result.Succeeded = false; result.Errors.Add("Error removing order from original queue !"); return(result); } } else { result.Succeeded = false; result.Errors.Add("Error fetching order"); return(result); } } catch (Exception ex) { result.Succeeded = false; result.Errors.Add(ex.Message); return(result); } }
//public async Task<ApiResponse<bool>> AddOrderToQueue(OrderToAddDTO addOrderRequest) //{ // var result = new ApiResponse<bool>(); // BarberQueue currentQueue; // Customer currentCustomer; // Order orderToAdd = new Order(); // TimeSpan? waitingTime; // try // { // //Map order services from the add order request . // orderToAdd.OrderServices = new List<OrderService>(); // orderToAdd.OrderServices = mapper.Map<List<OrderService>>(addOrderRequest.OrderServices); // //Set queue waiting times to check for inactive orders. // await SetQueueWaitingTimes(); // //Fetch barber queue to add order to . // var barberQueueResult = await unitOfWork.BarbersQueuesManager.GetAsync(b => b.Id == addOrderRequest.BarberQueueId, includeProperties: "Orders"); // currentQueue = barberQueueResult.FirstOrDefault(); // //Fetch customer. // currentCustomer = await unitOfWork.CustomersManager.GetByIdAsync(addOrderRequest.CustomerId); // //Calculate order total and total service time . // orderToAdd.TotalServicesWaitingTime = 0; // orderToAdd.OrderTotal = 0; // orderToAdd.CustomerId = currentCustomer.Id; // orderToAdd.CustomerName = currentCustomer.Name; // orderToAdd.CustomerMobile = currentCustomer.PhoneNumber; // orderToAdd.BarberQueueId = currentQueue.Id; // orderToAdd.OrderDate = DateTime.Now; // orderToAdd.WaitingTimeInMinutes = 0; // for (int i = 0; i < orderToAdd.OrderServices.Count; i++) // { // orderToAdd.TotalServicesWaitingTime += orderToAdd.OrderServices[i].Time; // orderToAdd.OrderTotal += orderToAdd.OrderServices[i].Price; // orderToAdd.OrderServices[i].IsConfirmed = false; // } // if (currentQueue.Orders.Count == 0) // if queue is empty . // { // orderToAdd.FinishTime = DateTime.Now.AddMinutes(Convert.ToDouble(orderToAdd.TotalServicesWaitingTime)); // orderToAdd.Status = "Inprogress"; // currentQueue.QueueStatus = "busy"; // var addOrderResult = await unitOfWork.OrdersManager.CreateAsync(orderToAdd); // if (addOrderResult != null) // { // int addOrderServiceErrorCounter = 0; // for (int i = 0; i < orderToAdd.OrderServices.Count; i++) // { // var addOrderServiceResult = await unitOfWork.OrderServicesManager.CreateAsync(orderToAdd.OrderServices[i]); // if (addOrderServiceResult == null) // { // addOrderServiceErrorCounter++; // } // } // if (addOrderServiceErrorCounter == 0) // { // currentCustomer.LastVisitDate = DateTime.Now; // await unitOfWork.SaveChangesAsync(); // await SetQueueWaitingTimes(); // adjust queue time // result.Succeeded = true; // return result; // } // else // { // result.Succeeded = false; // result.Errors.Add("Error adding order services !"); // return result; // } // } // else // { // result.Succeeded = false; // result.Errors.Add("Failed to add order !"); // return result; // } // } // else // Queue has orders // { // // Calculate order waiting time from last order in queue // waitingTime = currentQueue.Orders[currentQueue.Orders.Count - 1].FinishTime.Value - orderToAdd.OrderDate; // if (waitingTime.HasValue) // { // orderToAdd.WaitingTimeInMinutes = Convert.ToInt32(waitingTime.Value.TotalMinutes); // orderToAdd.FinishTime = currentQueue.Orders[currentQueue.Orders.Count - 1].FinishTime.Value.AddMinutes(Convert.ToDouble(orderToAdd.WaitingTimeInMinutes.Value)); // } // orderToAdd.Status = "Pending"; // var addOrderResult = await unitOfWork.OrdersManager.CreateAsync(orderToAdd); // if (addOrderResult != null) // { // int addOrderServiceErrorCounter = 0; // for (int i = 0; i < orderToAdd.OrderServices.Count; i++) // { // var addOrderServiceResult = await unitOfWork.OrderServicesManager.CreateAsync(orderToAdd.OrderServices[i]); // if (addOrderServiceResult == null) // { // addOrderServiceErrorCounter++; // } // } // if (addOrderServiceErrorCounter == 0) // { // await unitOfWork.SaveChangesAsync(); // await SetQueueWaitingTimes(); //adjust queue waiting time. // result.Succeeded = true; // return result; // } // else // { // result.Succeeded = false; // result.Errors.Add("Error adding order services !"); // return result; // } // } // else // { // result.Succeeded = false; // result.Errors.Add("Error creating Order !"); // return result; // } // } // } // catch (Exception ex) // { // result.Succeeded = false; // result.Errors.Add(ex.Message); // return result; // } //} public async Task <ApiResponse <bool> > AddOrderToQueue(OrderToAddDTO addOrderRequest) { var result = new ApiResponse <bool>(); BarberQueue currentQueue; Customer currentCustomer; Order orderToAdd = new Order(); try { //Map order services from the add order request . orderToAdd.OrderServices = new List <OrderService>(); orderToAdd.OrderServices = mapper.Map <List <OrderService> >(addOrderRequest.OrderServices); //Fetch barber queue to add order to . var barberQueueResult = await unitOfWork.BarbersQueuesManager.GetAsync(b => b.Id == addOrderRequest.BarberQueueId, includeProperties : "Orders"); currentQueue = barberQueueResult.FirstOrDefault(); //Fetch customer. currentCustomer = await unitOfWork.CustomersManager.GetByIdAsync(addOrderRequest.CustomerId); //Calculate order total and total service time . orderToAdd.TotalServicesWaitingTime = 0; orderToAdd.OrderTotal = 0; orderToAdd.CustomerId = currentCustomer.Id; orderToAdd.CustomerName = currentCustomer.Name; orderToAdd.CustomerMobile = currentCustomer.PhoneNumber; orderToAdd.BarberQueueId = currentQueue.Id; orderToAdd.WaitingTimeInMinutes = currentQueue.QueueWaitingTime; orderToAdd.DiscountRate = addOrderRequest.DiscountRate; for (int i = 0; i < orderToAdd.OrderServices.Count; i++) { orderToAdd.TotalServicesWaitingTime += orderToAdd.OrderServices[i].Time; orderToAdd.OrderTotal += orderToAdd.OrderServices[i].Price; orderToAdd.OrderServices[i].IsConfirmed = false; } orderToAdd.OrderTotal = orderToAdd.OrderTotal - (orderToAdd.OrderTotal * (addOrderRequest.DiscountRate / 100)); // if queue is empty . if (currentQueue.Orders.Count == 0) { orderToAdd.OrderDate = DateTime.Now; orderToAdd.FinishTime = DateTime.Now.AddMinutes(Convert.ToDouble(orderToAdd.TotalServicesWaitingTime)); orderToAdd.Status = "Inprogress"; currentQueue.QueueStatus = "busy"; var addOrderResult = await unitOfWork.OrdersManager.CreateAsync(orderToAdd); if (addOrderResult != null) { int addOrderServiceErrorCounter = 0; for (int i = 0; i < orderToAdd.OrderServices.Count; i++) { var addOrderServiceResult = await unitOfWork.OrderServicesManager.CreateAsync(orderToAdd.OrderServices[i]); if (addOrderServiceResult == null) { addOrderServiceErrorCounter++; } } if (addOrderServiceErrorCounter == 0) { currentCustomer.LastVisitDate = DateTime.Now; await unitOfWork.SaveChangesAsync(); await SetQueueWaitingTimes(); // adjust queue time result.Succeeded = true; return(result); } else { result.Succeeded = false; result.Errors.Add("Error adding order services !"); return(result); } } else { result.Succeeded = false; result.Errors.Add("Failed to add order !"); return(result); } } // Queue has orders else { orderToAdd.OrderDate = currentQueue.Orders[currentQueue.Orders.Count - 1].FinishTime; orderToAdd.FinishTime = orderToAdd.OrderDate.Value.AddMinutes(Convert.ToDouble(orderToAdd.TotalServicesWaitingTime)); orderToAdd.Status = "Pending"; var addOrderResult = await unitOfWork.OrdersManager.CreateAsync(orderToAdd); if (addOrderResult != null) { int addOrderServiceErrorCounter = 0; for (int i = 0; i < orderToAdd.OrderServices.Count; i++) { var addOrderServiceResult = await unitOfWork.OrderServicesManager.CreateAsync(orderToAdd.OrderServices[i]); if (addOrderServiceResult == null) { addOrderServiceErrorCounter++; } } if (addOrderServiceErrorCounter == 0) { await unitOfWork.SaveChangesAsync(); //await SetQueueWaitingTimes(); //adjust queue waiting time. result.Succeeded = true; return(result); } else { result.Succeeded = false; result.Errors.Add("Error adding order services !"); return(result); } } else { result.Succeeded = false; result.Errors.Add("Error creating Order !"); return(result); } } } catch (Exception ex) { result.Succeeded = false; result.Errors.Add(ex.Message); return(result); } }
public ResultDTO_AddOrder AddOrder(OrderToAddDTO order) { var result = new ResultDTO_AddOrder { SkuIdsOverStock = order.OrderItems.Join(_DbContext.Skus, oi => oi.SkuId, sk => sk.SkuId, (oi, sk) => new { skuId = oi.SkuId, quantityToOrder = oi.Quantity, quantityInStock = sk.Quantity }) .Where(z => z.quantityInStock < z.quantityToOrder) .Select(z => z.skuId) }; var styleIds = order.OrderItems.Join(_DbContext.Skus, oi => oi.SkuId, sk => sk.SkuId, (oi, sk) => new { styleId = sk.StyleId }).Distinct().ToList(); var skus = styleIds.Join(_DbContext.Skus, st => st.styleId, sk => sk.StyleId, (st, sk) => new SkuStyleDTO { SkuId = sk.SkuId, StyleId = st.styleId, Size = sk.Size, Quantity = sk.Quantity }).ToList(); var styleStates = styleIds.Join(_DbContext.StyleStates, st => st.styleId, ss => ss.StyleId, (st, ss) => new StyleState { StyleId = st.styleId, AverageRatings = ss.AverageRatings, ReviewCount = ss.ReviewCount, SoldOut = ss.SoldOut }).ToList(); if (result.SkuIdsOverStock.Any()) { result.OrderId = -1; result.Skus = skus; result.StyleStates = styleStates; return(result); } var orderToAdd = _mapper.Map <Order>(order); SaveChange(orderToAdd); result.OrderId = orderToAdd.OrderId; result.CustomerOrderId = orderToAdd.CustomerOrderId; result.Skus = skus.GroupJoin(order.OrderItems, sk => sk.SkuId, oi => oi.SkuId, (sk, ois) => { var orderItems = ois.ToList(); return(new SkuStyleDTO { SkuId = sk.SkuId, StyleId = sk.StyleId, Size = sk.Size, Quantity = orderItems.Any() ? (short)(sk.Quantity - orderItems.First().Quantity) : sk.Quantity }); }).ToList(); result.StyleStates = styleStates.Select(ss => { var sum = result.Skus.Where(sku => sku.StyleId == ss.StyleId).ToList().Sum(sku => sku.Quantity); return(new StyleState { StyleId = ss.StyleId, AverageRatings = ss.AverageRatings, ReviewCount = ss.ReviewCount, SoldOut = sum == 0 }); }); return(result); }