Exemplo n.º 1
0
        /// <summary>
        /// Attempts to add a new medicine type to the persisted store.
        /// </summary>
        /// <param name="item">The medicine type that should be added.</param>
        /// <param name="cancellationToken">A token that can be used to signal operation cancellation.</param>
        /// <returns>The added medicine type.</returns>
        public virtual async Task <IMedicineType> AddAsync(IMedicineType item, CancellationToken cancellationToken)
        {
            Logger.LogInformation("Adding a medicine type...");

            var entity = new MedicineTypeModel
            {
                Description = item.Description
            };
            var changes = await LivestockContext.AddAsync(entity, cancellationToken)
                          .ConfigureAwait(false);

            await LivestockContext.SaveChangesAsync(cancellationToken)
            .ConfigureAwait(false);

            Logger.LogDebug($"Medicine type added with detail {changes.Entity}");
            return(Mapper.Map(changes.Entity));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Updates the properties of an medicine type in the persisted store.
        /// </summary>
        /// <param name="item">The medicine type with its desired values.</param>
        /// <param name="cancellationToken">A token that can be used to signal operation cancellation.</param>
        /// <returns>The updated medicine type.</returns>
        /// <exception cref="EntityNotFoundException{IMedicineType}">When the medicine type with the given key is not found.</exception>
        public virtual async Task <IMedicineType> UpdateAsync(IMedicineType item, CancellationToken cancellationToken)
        {
            Logger.LogInformation($"Updating the medicine type with ID {item.Id}...");

            var entity = await LivestockContext.MedicineTypes
                         .FindAsync(new object[] { item.Id }, cancellationToken)
                         .ConfigureAwait(false);

            if (entity == null)
            {
                throw new EntityNotFoundException <MedicineTypeModel>(item.Id);
            }

            entity.Description = item.Description;

            var changes = LivestockContext.MedicineTypes.Update(entity);
            await LivestockContext.SaveChangesAsync(cancellationToken)
            .ConfigureAwait(false);

            return(Mapper.Map(changes.Entity));
        }