コード例 #1
0
        public async Task <ServiceResponse <int> > CreateAsync(DeliveryItemDto deliveryItem, int userId)
        {
            if (deliveryItem == null)
            {
                throw new ArgumentNullException(nameof(deliveryItem));
            }

            var deliveryItemEntity = deliveryItem.ToEntity();

            deliveryItemEntity.UpdateCreatedFields(userId).UpdateModifiedFields(userId);
            deliveryItemEntity.Recommendations.UpdateCreatedFields(userId).UpdateModifiedFields(userId);
            deliveryItemEntity.Attachments.UpdateCreatedFields(userId).UpdateModifiedFields(userId);

            var saved = UnitOfWork.Get <DeliveryItem>().Add(deliveryItemEntity);

            var deliveryEntity = await UnitOfWork.Get <Delivery>().GetAsync(deliveryItem.DeliveryID);

            deliveryEntity.TotalGrossPrice      = deliveryEntity.TotalGrossPrice + deliveryItem.GrossPrice;
            deliveryEntity.TotalDiscount        = deliveryEntity.TotalDiscount + deliveryItem.Discount;
            deliveryEntity.TotalDiscountedPrice = deliveryEntity.TotalDiscountedPrice + deliveryItem.DiscountedPrice;

            UnitOfWork.Get <Delivery>().Update(deliveryEntity);

            await UnitOfWork.SaveAsync();

            return(new SuccessResponse <int>(saved.ID));
        }
コード例 #2
0
        public async Task <ServiceResponse <DeliveryItemDto> > UpdateAsync(DeliveryItemDto deliveryItem, int userId)
        {
            if (deliveryItem == null)
            {
                throw new ArgumentNullException(nameof(deliveryItem));
            }

            var existentEntity = await UnitOfWork.Get <DeliveryItem>().GetAsync(di => di.ID == deliveryItem.ID);

            if (existentEntity == null)
            {
                return(new ServiceResponse <DeliveryItemDto>(ServiceResponseStatus.NotFound));
            }

            var deliveryEntity = await UnitOfWork.Get <Delivery>().GetAsync(deliveryItem.DeliveryID);

            deliveryEntity.TotalGrossPrice      = deliveryEntity.TotalGrossPrice - existentEntity.GrossPrice;
            deliveryEntity.TotalDiscount        = deliveryEntity.TotalDiscount - existentEntity.Discount;
            deliveryEntity.TotalDiscountedPrice = deliveryEntity.TotalDiscountedPrice - existentEntity.DiscountedPrice;

            var entity = deliveryItem.ToEntity();

            existentEntity.Attachments     = GetAttachments(existentEntity.ID).ToList();
            existentEntity.Recommendations = GetRecommendations(existentEntity.ID).ToList();

            existentEntity
            .UpdateFields(entity)
            .UpdateAttachments(entity, UnitOfWork, userId)
            .UpdateRecommendations(entity, UnitOfWork, userId)
            .UpdateModifiedFields(userId);

            var updated = UnitOfWork.Get <DeliveryItem>().Update(existentEntity);

            deliveryEntity.TotalGrossPrice      = deliveryEntity.TotalGrossPrice + deliveryItem.GrossPrice;
            deliveryEntity.TotalDiscount        = deliveryEntity.TotalDiscount + deliveryItem.Discount;
            deliveryEntity.TotalDiscountedPrice = deliveryEntity.TotalDiscountedPrice + deliveryItem.DiscountedPrice;

            UnitOfWork.Get <Delivery>().Update(deliveryEntity);

            await UnitOfWork.SaveAsync();

            return(new SuccessResponse <DeliveryItemDto>(updated.ToDto()));
        }