public async Task Delete(ProductPhotoDto dto)
        {
            var productPhoto = await _productPhotoRepository.GetById(dto.Id);

            if (productPhoto != null)
            {
                var delete = await _productPhotoRepository.GetById(productPhoto.Id);

                if (delete.ErasedState)
                {
                    delete.ErasedState = false;

                    await _productPhotoRepository.Update(delete);
                }
                else
                {
                    delete.ErasedState = true;

                    await _productPhotoRepository.Update(delete);
                }
            }
            else
            {
                throw new Exception("This Product Photo not exist");
            }
        }
Beispiel #2
0
 public static int Create(ProductView productView)
 {
     try
     {
         int proid = new ProductDto().Create(productView);
         if (proid == 0)
         {
             return(proid);
         }
         //Photo product
         List <ProductPhotoView> photos = new List <ProductPhotoView>();
         productView.ListPhoto.ForEach(s =>
         {
             photos.Add(new ProductPhotoView
             {
                 Photo = s,
                 ProId = proid,
                 Main  = false
             });
         });
         photos[0].Main = true;
         //End photos
         bool checkPhotos = new ProductPhotoDto().Create(photos);
         if (!checkPhotos)
         {
             return(-2); //Insert ảnh bị lỗi
         }
         return(proid);
     }
     catch (Exception e)
     {
         Debug.WriteLine(e.Message);
         return(0); // insert product bị lỗi
     }
 }
        public async Task Create(ProductPhotoDto dto)
        {
            var productPhoto = new ProductPhoto
            {
                Url         = dto.Url,
                ProductId   = dto.ProductId,
                ErasedState = false
            };

            await _productPhotoRepository.Create(productPhoto);
        }
Beispiel #4
0
        public async Task <ActionResult> AddCarouselPhoto([FromForm] ProductPhotoDto photoDto)
        {
            if (photoDto.Photo.Length > 0)
            {
                var photo = await _photoService.SaveCarouselToDiskAsync(photoDto.Photo);

                if (photo != null)
                {
                    _unitOfWork.Repository <Carousel>().Add(photo);
                    await _unitOfWork.Complete();

                    return(Ok());
                }
            }
            return(BadRequest(new { Error = "Greška pri dodavanju slike" }));
        }
Beispiel #5
0
        public bool UploadPhotos(List <int> listIdPhotoCurrent, List <string> listPhotoNameNew, int productId)
        {
            List <int> listIdPhotoOld    = new ProductPhotoDto().GetDataByProductId(productId).Select(s => s.Id).ToList();
            List <int> listIdPhotoRemove = listIdPhotoOld.Except(listIdPhotoCurrent).ToList();

            listIdPhotoRemove.ForEach(s =>
            {
                new ProductPhotoDto().Remove(s);
            });
            List <ProductPhotoView> listProPhotos = new List <ProductPhotoView>();

            listPhotoNameNew.ForEach(s =>
            {
                listProPhotos.Add(new ProductPhotoView
                {
                    Photo = s,
                    ProId = productId
                });
            });
            return(new ProductPhotoDto().Create(listProPhotos));
        }
        public async Task Update(ProductPhotoDto dto)
        {
            using (var context = new DataContext())
            {
                var updateProductPhoto = context.ProductPhotos.FirstOrDefault(x => x.Id == dto.Id);

                if (updateProductPhoto == null)
                {
                    throw new Exception("The Product Photo to modify was not found");
                }
                else
                {
                    if (updateProductPhoto.ErasedState)
                    {
                        throw new Exception("The Product Photo is eliminated");
                    }

                    updateProductPhoto.Url       = dto.Url;
                    updateProductPhoto.ProductId = dto.ProductId;

                    await _productPhotoRepository.Update(updateProductPhoto);
                }
            }
        }
        public async Task <ActionResult <ProductToReturnDto> > AddProductPhoto(int id, [FromForm] ProductPhotoDto photoDto)
        {
            var spec    = new ProductsWithTypesAndBrandsSpecification(id);
            var product = await _unitOfWork.Repository <Product>().GetEntitiyWithSpec(spec);

            if (photoDto.Photo.Length > 0)
            {
                var photo = await _photoService.SaveToDiskAsync(photoDto.Photo);

                if (photoDto != null)
                {
                    product.AddPhoto(photo.PictureUrl, photo.FileName);
                    _unitOfWork.Repository <Product>().Update(product);
                    var result = await _unitOfWork.Complete();

                    if (result <= 0)
                    {
                        return(BadRequest(new ApiResponse(400, "Problem adding photo product")));
                    }
                }
                else
                {
                    return(BadRequest(new ApiResponse(400, "Problem saving photo to disk")));
                }
            }
            return(_mapper.Map <Product, ProductToReturnDto>(product));
        }