public bool UpdateShoesSize(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); if (quantity == 0) { Shoes shoes = _shoesRepository.GetById(shoesId); shoes.IsAvaiable = false; _shoesRepository.Update(shoes); } else { Shoes shoes = _shoesRepository.GetById(shoesId); shoes.IsAvaiable = true; _shoesRepository.Update(shoes); } return(true); } return(false); }
public bool DeleteShoes(int id) { Shoes shoes = _shoesRepository.GetById(id); if (shoes != null) { _shoesRepository.Delete(shoes); return(true); } return(false); }
public List <OrderDetail> GetOrderDetails(int orderId) { List <OrderDetail> orderDetails = _orderDetailRepository .GetAll() .Where(od => od.OrderId == orderId) .ToList(); foreach (var od in orderDetails) { od.Shoes = _shoesRepository.GetById(od.ShoesId.Value); od.Shoes.OrderDetail = null; od.Size = _sizeRepository.GetById(od.SizeId.Value); od.Size.OrderDetail = null; } return(orderDetails); }
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); }