public async Task <ApiResponse> AddOrder([FromBody] OrderPassDto obj) { try { if (obj.OrderDto.UserId != Guid.Empty) { var userId = await _userService.GetByIdAsync(obj.OrderDto.UserId); if (userId == null) { return(new ApiResponse("Không tồn tại khách hàng", 400)); } var order = _mapper.Map <Order>(obj.OrderDto); await _orderService.AddAsync(order); if (order != null) { //var newOrderDetails = new List<OrderDetailDto>(); foreach (var or in obj.OrderDetailDtos) { var product = await _productService.GetByIdAsync(or.ProductId); or.OrderId = order.Id; } var orderDetails = _mapper.Map <List <OrderDetail> >(obj.OrderDetailDtos); await _orderDetailService.AddManyAsync(orderDetails); //return new ApiResponse($"Success", 201); userId.AccumulatedPoints = userId.AccumulatedPoints - order.AccumulatedPoints; if (userId.AccumulatedPoints >= 0) { await _userService.UpdateAsync(userId); } else { return(new ApiResponse("Lỗi", 404)); } } var vm = _mapper.Map <OrderViewModel>(order); return(new ApiResponse("success", vm, 200)); } return(new ApiResponse("not found user", 404)); } catch (Exception ex) { return(new ApiResponse($"{ex}", 400)); } }