/// <summary> /// Updates the feeding transaction entity instance with the values of the feeding transaction DTO instance. /// </summary> /// <param name="entity">The database entity instance of the feeding transaction.</param> /// <param name="dto">The DTO instance of the feeding transaction.</param> protected virtual void UpdateEntityValues(FeedingTransactionModel entity, IFeedingTransaction dto) { entity.FeedTypeId = dto.FeedTypeId; entity.Quantity = dto.Quantity; entity.TransactionDate = dto.TransactionDate; entity.UnitId = dto.UnitId; }
/// <summary> /// Attempts to add a feeding transaction to the database context. /// </summary> /// <param name="item">The object that contains the information for the new feeding transaction.</param> /// <param name="cancellationToken">A token that can be used to signal operation cancellation.</param> /// <returns>The added feeding transaction.</returns> public virtual async Task <IFeedingTransaction> AddAsync(IFeedingTransaction item, CancellationToken cancellationToken) { Logger.LogInformation("Adding a feeding transaction..."); var entity = Mapper.Map(item); var changes = await LivestockContext.FeedingTransactions.AddAsync(entity, cancellationToken).ConfigureAwait(false); await LivestockContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); return(Mapper.Map(changes.Entity)); }
/// <summary> /// Updates a feeding transaction with the given values. /// </summary> /// <param name="item">The property values with which to update the feeding transaction.</param> /// <param name="cancellationToken">A token that can be used to signal operation cancellation.</param> /// <returns>The updated feeding transaction.</returns> public virtual async Task <IFeedingTransaction> UpdateAsync(IFeedingTransaction item, CancellationToken cancellationToken) { Logger.LogInformation($"Updating feeding transaction with ID {item.Id}..."); var feedingTransaction = await LivestockContext.FeedingTransactions .FindAsync(new object[] { item.Id }, cancellationToken) .ConfigureAwait(false); if (feedingTransaction == null) { throw new EntityNotFoundException <IFeedingTransaction>(item.Id); } UpdateEntityValues(feedingTransaction, item); var changes = LivestockContext.Update(feedingTransaction); await LivestockContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); return(Mapper.Map(changes.Entity)); }