public bool PutShoesSize(int shoesId, int sizeId, int quantity) { ShoesHasSize shoesHasSize = _shoesHasSizeRepository .GetAll() .Where(s => s.ShoesId == shoesId && s.SizeId == sizeId) .FirstOrDefault(); if (shoesHasSize != null) { shoesHasSize.Quantity += quantity; _shoesHasSizeRepository.Update(shoesHasSize); return(true); } return(false); }
public async Task <Order> CreateOrderAsync(OrderAddViewModel orderAddVM) { Order order = new Order() { ContactPhone = orderAddVM.ContactPhone, CreateDate = DateTime.Now, CusName = orderAddVM.CusName, Description = orderAddVM.Description, ShipAddress = orderAddVM.ShipAddress, ShipDate = orderAddVM.ShipDate, State = "pending", DiscountCode = orderAddVM.DiscountCode, Sum = 0, Total = 0 }; order = _orderRepository.Add(order); OrderDetail orderDetail = null; foreach (var odd in orderAddVM.OrderDetailAdds) { ShoesHasSize shoesHasSize = _shoesHasSizeRepository.GetAll() .Where(h => h.SizeId == odd.SizeId && h.ShoesId == odd.ShoesId).FirstOrDefault(); if (shoesHasSize != null) { if (shoesHasSize.Quantity >= odd.Quantity) { orderDetail = new OrderDetail() { OrderId = order.Id, Quantity = odd.Quantity, ShoesId = odd.ShoesId, SizeId = odd.SizeId }; _orderDetailRepository.Add(orderDetail); double price = _shoesRepository.GetById(odd.ShoesId.Value).Price.Value; order.Sum += price * odd.Quantity; shoesHasSize.Quantity -= odd.Quantity.Value; _shoesHasSizeRepository.Update(shoesHasSize); if (shoesHasSize.Quantity == 0) { Shoes shoes = _shoesRepository.GetById(odd.ShoesId.Value); shoes.IsAvaiable = false; _shoesRepository.Update(shoes); } } } } order.Total = order.Sum; if (order.DiscountCode != "") { order.Discount = _promotionRepository.GetAll() .Where(p => p.DiscountCode == order.DiscountCode).FirstOrDefault().Discount; order.Total -= order.Sum * order.Discount / 100; } if (order.Total == 0) { _orderRepository.Delete(order); return(null); } else { order = _orderRepository.Update(order); OrderViewModel orderVM = _mapper.Map <OrderViewModel>(order); FirebaseSerivce <OrderViewModel> firebaseSerivce = new FirebaseSerivce <OrderViewModel>(); await firebaseSerivce.SetDataAsync("orders", orderVM); } return(order); }