public async Task <Response> CreateOrderDetailBatch(createOrderDetailViewModel model) { if (model.Quantity < 0) { return(_apiResponse.Error(ShoerserException.OrderDetailException.OD01, nameof(ShoerserException.OrderDetailException.OD01))); } var product = _unitOfWork.ProductRepository.Get(x => x.IsDelete == false && x.Id == model.IdProduct).FirstOrDefault(); if (product == null) { return(_apiResponse.Error(ShoerserException.ProductException.P03, nameof(ShoerserException.ProductException.P03))); } if (model.Quantity > product.Quantity) { return(_apiResponse.Error(ShoerserException.OrderDetailException.OD02, nameof(ShoerserException.OrderDetailException.OD02))); } OrderDetail orderdetail = new OrderDetail(); orderdetail.Quantity = model.Quantity; orderdetail.IdProduct = model.IdProduct; orderdetail.IdOrder = model.IdOrder; orderdetail.CreatedAt = DateTime.UtcNow; _unitOfWork.OrderDetailRepository.Add(orderdetail); return(_apiResponse.Ok("Success Add batch order detail")); }
public async Task <object> Register(RegisterViewModel model) { var result = _unitOfWork.AccountRepository.Get(c => c.Username.Equals(model.Username)); if (result.FirstOrDefault() != null) { return(_apiResponse.Error(ShoerserException.AccountException.A05, nameof(ShoerserException.AccountException.A05))); } var account = new Account { Username = model.Username, Password = model.Password, Role = Role.Customer, FullName = model.FullName, Address = model.Address, }; _unitOfWork.AccountRepository.Add(account); return(_apiResponse.Ok(_unitOfWork.Save())); }
public async Task <Object> CreateOrder(List <createOrderDetailViewModel> listModel, Guid IdAccount) { var account = _unitOfWork.AccountRepository.Get(x => x.IsDelete == false && x.Id == IdAccount).FirstOrDefault(); if (account == null) { return(_apiResponse.Error(ShoerserException.AccountException.A02, nameof(ShoerserException.AccountException.A02))); } if (listModel.Count() <= 0) { return(_apiResponse.Error(ShoerserException.OrderException.O02, nameof(ShoerserException.OrderException.O02))); } var totalPrice = 0; foreach (var item in listModel) { var product = _unitOfWork.ProductRepository.Get(x => x.IsDelete == false && x.Id == item.IdProduct).FirstOrDefault(); if (product != null) { totalPrice += (product.Price * item.Quantity); } } Order order = new Order(); order.NameOrder = account.Username; order.TotalPrice = totalPrice; order.CreatedAt = DateTime.Now; order.IdAccount = IdAccount; var addedOrder = _unitOfWork.OrderRepository.Add(order); listModel = listModel.Select(x => new createOrderDetailViewModel { IdOrder = addedOrder.Entity.Id, IdProduct = x.IdProduct, Quantity = x.Quantity, }).ToList(); foreach (var item in listModel) { var checkBatch = await CreateOrderDetailBatch(item); if (!checkBatch.Code.Equals("200")) { return(checkBatch); } } var result = _apiResponse.Ok(_unitOfWork.Save()); return(result); }
public async Task <Object> ShowProductDetail(Guid id) { Product product = _unitOfWork.ProductRepository.GetByID(id); if (product == null) { return(_apiResponse.Error(ShoerserException.ProductException.P03, nameof(ShoerserException.ProductException.P03))); } var result = _apiResponse.Ok(product); return(result); }