Ejemplo n.º 1
0
        public async Task <Guid?> CreateAsync(CreateUpdateProductModel model)
        {
            var brand = catalogContext.Brands.FirstOrDefault(x => x.SellerId == model.OrganisationId.Value && x.IsActive);

            if (brand == null)
            {
                throw new CustomException(this.productLocalizer.GetString("BrandNotFound"), (int)HttpStatusCode.NotFound);
            }

            var category = catalogContext.Categories.FirstOrDefault(x => x.Id == model.CategoryId && x.IsActive);

            if (category == null)
            {
                throw new CustomException(this.productLocalizer.GetString("CategoryNotFound"), (int)HttpStatusCode.NotFound);
            }

            var product = new Product
            {
                IsNew            = model.IsNew,
                IsPublished      = model.IsPublished,
                IsProtected      = model.IsProtected,
                Sku              = model.Sku,
                BrandId          = brand.Id,
                CategoryId       = category.Id,
                PrimaryProductId = model.PrimaryProductId
            };

            await this.catalogContext.Products.AddAsync(product.FillCommonProperties());

            var productTranslation = new ProductTranslation
            {
                Language    = model.Language,
                Name        = model.Name,
                Description = model.Description,
                FormData    = model.FormData,
                ProductId   = product.Id
            };

            await this.catalogContext.ProductTranslations.AddAsync(productTranslation.FillCommonProperties());

            foreach (var imageId in model.Images.OrEmptyIfNull())
            {
                var productImage = new ProductImage
                {
                    MediaId   = imageId,
                    ProductId = product.Id
                };

                await this.catalogContext.ProductImages.AddAsync(productImage.FillCommonProperties());
            }

            foreach (var videoId in model.Videos.OrEmptyIfNull())
            {
                var productVideo = new ProductVideo
                {
                    MediaId   = videoId,
                    ProductId = product.Id
                };

                await this.catalogContext.ProductVideos.AddAsync(productVideo.FillCommonProperties());
            }

            foreach (var fileId in model.Files.OrEmptyIfNull())
            {
                var productFile = new ProductFile
                {
                    MediaId   = fileId,
                    ProductId = product.Id
                };

                await this.catalogContext.ProductFiles.AddAsync(productFile.FillCommonProperties());
            }

            await this.catalogContext.SaveChangesAsync();

            await this.productIndexingRepository.IndexAsync(product.Id);

            return(product.Id);
        }
Ejemplo n.º 2
0
        public async Task <Guid?> UpdateAsync(CreateUpdateProductModel model)
        {
            var brand = catalogContext.Brands.FirstOrDefault(x => x.SellerId == model.OrganisationId.Value && x.IsActive);

            if (brand == null)
            {
                throw new CustomException(this.productLocalizer.GetString("BrandNotFound"), (int)HttpStatusCode.NotFound);
            }

            var category = catalogContext.Categories.FirstOrDefault(x => x.Id == model.CategoryId && x.IsActive);

            if (category == null)
            {
                throw new CustomException(this.productLocalizer.GetString("CategoryNotFound"), (int)HttpStatusCode.NotFound);
            }

            var product = await this.catalogContext.Products.FirstOrDefaultAsync(x => x.Id == model.Id && x.Brand.SellerId == model.OrganisationId && x.IsActive);

            if (product == null)
            {
                throw new CustomException(this.productLocalizer.GetString("ProductNotFound"), (int)HttpStatusCode.NotFound);
            }

            product.IsNew            = model.IsNew;
            product.IsPublished      = model.IsPublished;
            product.IsProtected      = model.IsProtected;
            product.Sku              = model.Sku;
            product.BrandId          = brand.Id;
            product.CategoryId       = category.Id;
            product.PrimaryProductId = model.PrimaryProductId;
            product.LastModifiedDate = DateTime.UtcNow;

            var productTranslation = await this.catalogContext.ProductTranslations.FirstOrDefaultAsync(x => x.ProductId == product.Id && x.Language == model.Language && x.IsActive);

            if (productTranslation != null)
            {
                productTranslation.Name        = model.Name;
                productTranslation.Description = model.Description;
                productTranslation.FormData    = model.FormData;
            }
            else
            {
                var newProductTranslation = new ProductTranslation
                {
                    Language    = model.Language,
                    ProductId   = product.Id,
                    Name        = model.Name,
                    Description = model.Description,
                    FormData    = model.FormData
                };

                this.catalogContext.ProductTranslations.Add(newProductTranslation.FillCommonProperties());
            }

            var productImages = this.catalogContext.ProductImages.Where(x => x.ProductId == model.Id && x.IsActive);

            foreach (var productImage in productImages.OrEmptyIfNull())
            {
                this.catalogContext.ProductImages.Remove(productImage);
            }

            foreach (var imageId in model.Images.OrEmptyIfNull())
            {
                var productImage = new ProductImage
                {
                    MediaId   = imageId,
                    ProductId = product.Id
                };

                await this.catalogContext.ProductImages.AddAsync(productImage.FillCommonProperties());
            }

            var productVideos = this.catalogContext.ProductVideos.Where(x => x.ProductId == model.Id && x.IsActive);

            foreach (var productVideo in productVideos.OrEmptyIfNull())
            {
                this.catalogContext.ProductVideos.Remove(productVideo);
            }

            foreach (var videoId in model.Videos.OrEmptyIfNull())
            {
                var productVideo = new ProductVideo
                {
                    MediaId   = videoId,
                    ProductId = product.Id
                };

                await this.catalogContext.ProductVideos.AddAsync(productVideo.FillCommonProperties());
            }

            var productFiles = this.catalogContext.ProductFiles.Where(x => x.ProductId == model.Id && x.IsActive);

            foreach (var productFile in productFiles.OrEmptyIfNull())
            {
                this.catalogContext.ProductFiles.Remove(productFile);
            }

            foreach (var fileId in model.Files.OrEmptyIfNull())
            {
                var productFile = new ProductFile
                {
                    MediaId   = fileId,
                    ProductId = product.Id
                };

                await this.catalogContext.ProductFiles.AddAsync(productFile.FillCommonProperties());
            }

            var message = new UpdatedProductIntegrationEvent
            {
                OrganisationId = model.OrganisationId,
                Language       = model.Language,
                Username       = model.Username,
                ProductId      = model.Id,
                ProductName    = model.Name,
                ProductSku     = model.Sku
            };

            this.eventBus.Publish(message);

            await this.catalogContext.SaveChangesAsync();

            await this.productIndexingRepository.IndexAsync(product.Id);

            return(product.Id);
        }