Exemplo 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);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Save([FromBody] ProductRequestModel request)
        {
            var sellerClaim = this.User.Claims.FirstOrDefault(x => x.Type == AccountConstants.Claims.OrganisationIdClaim);

            var serviceModel = new CreateUpdateProductModel
            {
                Id = request.Id,
                PrimaryProductId = request.PrimaryProductId,
                IsNew            = request.IsNew,
                IsPublished      = request.IsPublished,
                CategoryId       = request.CategoryId,
                IsProtected      = request.IsProtected,
                Videos           = request.Videos,
                Files            = request.Files,
                Images           = request.Images,
                Sku            = request.Sku,
                Name           = request.Name,
                Description    = request.Description,
                FormData       = request.FormData,
                Username       = this.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value,
                OrganisationId = GuidHelper.ParseNullable(sellerClaim?.Value),
                Language       = CultureInfo.CurrentCulture.Name
            };

            if (request.Id.HasValue)
            {
                var validator = new UpdateProductModelValidator();

                var validationResult = await validator.ValidateAsync(serviceModel);

                if (validationResult.IsValid)
                {
                    var productId = await this.productService.UpdateAsync(serviceModel);

                    if (productId != null)
                    {
                        return(this.StatusCode((int)HttpStatusCode.OK, new { Id = productId }));
                    }
                }

                throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity);
            }
            else
            {
                var validator = new CreateProductModelValidator();

                var validationResult = await validator.ValidateAsync(serviceModel);

                if (validationResult.IsValid)
                {
                    var productId = await this.productService.CreateAsync(serviceModel);

                    if (productId != null)
                    {
                        return(this.StatusCode((int)HttpStatusCode.Created, new { Id = productId }));
                    }
                }

                throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity);
            }
        }
Exemplo n.º 3
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);
        }