示例#1
0
        public async Task ModifyProductAsync(EditProductOutDto dto)
        {
            Product product = await productsRepository.All()
                              .Include(x => x.Votes)
                              .Include(x => x.Characteristics)
                              .Include(x => x.ProductPictures)
                              .Include(x => x.ProductComments)
                              .FirstOrDefaultAsync(x => x.Id == dto.Id);

            product.Name           = dto.Name;
            product.CategoryId     = dto.CategoryId;
            product.ManufacturerId = dto.ManufacturerId;
            product.Price          = dto.Price;
            product.Discount       = dto.Discount;
            product.Quantity       = dto.Quantity;
            product.Weight         = dto.Weight;
            product.MonthsWarranty = dto.MonthsWarranty;
            product.MainPicURL     = dto.MainPicURL.Contains("res.cloudinary.com") ? dto.MainPicURL : cloudineryService.RelocateImgToCloudinary(product.Name + "mainPic", dto.MainPicURL, info: Guid.NewGuid().ToString());

            product.ReviewURL   = dto.ReviewURL;
            product.Description = dto.Description;
            product.IsDeleted   = dto.IsDeleted;

            if (!dto.VotesAny && product.Votes.Any())
            {
                product.Votes = new HashSet <ProductVote>();
            }

            product.Characteristics = new HashSet <ProductCharacteristic>();
            foreach (NewProductCharacteristicDto dtoChar in dto.Characteristics)
            {
                product.Characteristics.Add(mapper.Map <ProductCharacteristic>(dtoChar));
            }

            for (int i = 0; i < dto.ProductPictures.Count(); i++)
            {
                NewProductPictureDto pictureDto = dto.ProductPictures[i];
                if (!pictureDto.PictureURL.Contains("res.cloudinary.com"))
                {
                    pictureDto.PictureURL = cloudineryService.RelocateImgToCloudinary(product.Id + dto.Id + dto.Name + "altPic" + i, pictureDto.PictureURL, Guid.NewGuid().ToString());
                }
            }
            product.ProductPictures = dto.ProductPictures.Select(x => mapper.Map <ProductPicture>(x)).ToList();

            foreach (var dtoComment in dto.ProductComments)
            {
                var productComment = product.ProductComments.FirstOrDefault(x => x.Id == dtoComment.Id);
                if (productComment is null)
                {
                    continue;
                }
                productComment.IsDeleted = dtoComment.IsDeleted;
                productComment.Comment   = dtoComment.Comment;
            }

            //lock (ConcurencyMaster.LockProductsObj)
            //{
            productsRepository.SaveChangesAsync().GetAwaiter().GetResult();
            //}
        }
        public void Execute(NewProductPictureDto request)
        {
            var paths = UploadService.UploadImages(request.Pictures);

            for (int i = 0; i < paths.Count; i++)
            {
                var productPicture = new Picture
                {
                    ImagePath = paths[i],
                    ProductId = request.ProductId
                };
                _context.Pictures.Add(productPicture);
            }
            _context.SaveChanges();
        }
示例#3
0
 public void Post([FromForm] NewProductPictureDto dto, [FromServices] ICreateNewPictureCommand command)
 {
     executor.ExecuteCommand(command, dto);
 }