public async Task <Result <OrderItem> > AddNewOrderItem(OrderItemPreparationContext context) { var target = await _dbContext.OrderItems.FindAsync(context.OrderItemId); if (target != null) { _logger.LogInformation($"Order item {context.OrderId} already exists."); return(new Result <OrderItem> { Value = null, IsSuccessful = false }); } var entity = MapRequest(context); await _dbContext.OrderItems.AddAsync(entity); int changes = await _dbContext.SaveChangesAsync(); if (changes <= 0) { _logger.LogInformation($"Order item {context.OrderItemId} of Order {context.OrderId} could not be saved."); return(new Result <OrderItem> { Reason = ReasonType.DatabaseError, ChangeCount = changes, IsSuccessful = false }); } _logger.LogInformation($"Order {context.OrderItemId} was saved."); return(new Result <OrderItem> { Value = MapToOrderItem(entity), ChangeCount = changes, IsSuccessful = true }); }
OrderItemEntity MapRequest(OrderItemPreparationContext context) =>