public async Task <Int32ServiceResult> AddOrderAsync(CreateOrderRequest request) { try { using (var transaction = _unitofWork.BeginTransaction()) { var book = _unitofWork.BookRepository.Table.Find(request.BookId); if (book.Stock == 0) { return(Int32ServiceResult.Error("Book is out of stock.")); } book.Stock = book.Stock - 1; _unitofWork.SaveChanges(); var order = _mapper.Map <Order>(request); order.CreateDate = DateTime.Now; _unitofWork.OrderRepository.Add(order); await _unitofWork.SaveChangesAsync(); await transaction.CommitAsync(); return(Int32ServiceResult.Success(order.Id)); } } catch (Exception ex) { _logger.LogError(ex, ex.Message); return(Int32ServiceResult.Error("An error occurred while create order.")); } }
public async Task <Int32ServiceResult> AddCustomerAsync(CreateCustomerRequest request) { try { var customer = _mapper.Map <Customer>(request); await _unitofWork.CustomerRepository.CreateAsync(customer); return(Int32ServiceResult.Success(customer.Id)); } catch (Exception ex) { _logger.LogError(ex, ex.Message); return(Int32ServiceResult.Error("An error occurred while create customer.")); } }