Пример #1
0
        public async Task <IActionResult> AddBannerImage([FromForm] ImageForCreateDto imageForCreateDto)
        {
            var file         = imageForCreateDto.File;
            var uploadResult = new ImageUploadResult();

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation().Width(1920).Height(600).Crop("fill")
                    };
                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }

            imageForCreateDto.Url      = uploadResult.Url.ToString();
            imageForCreateDto.PublicId = uploadResult.PublicId;

            var image = _mapper.Map <Image>(imageForCreateDto);

            image.IsThumbnail = false;
            image.IsBanner    = true;
            _repo.Add(image);

            if (await _repo.SaveAll())
            {
                return(StatusCode(201));
            }

            return(BadRequest("Could not add the image"));
        }
Пример #2
0
        public async Task <IActionResult> AddImageForProduct(int productId, [FromForm] ImageForCreateDto imageForCreateDto)
        {
            var product = await _repo.GetProductById(productId);

            if (product == null)
            {
                return(NotFound());
            }

            var file         = imageForCreateDto.File;
            var uploadResult = new ImageUploadResult();

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File = new FileDescription(file.Name, stream)
                               // Transformation = new Transformation().Width(200).Height(200).Crop("fill")
                    };

                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }

            imageForCreateDto.Url      = uploadResult.Url.ToString();
            imageForCreateDto.PublicId = uploadResult.PublicId;

            var image = _mapper.Map <Image>(imageForCreateDto);

            image.IsThumbnail = product.Images.Any(i => i.IsThumbnail) ? false : true;

            product.Images.Add(image);

            if (await _repo.SaveAll())
            {
                var routeValues = new { productId, image.Id };
                return(CreatedAtRoute(nameof(GetPrductImageById), routeValues, image));
            }

            return(BadRequest("Could not add the image"));
        }
Пример #3
0
        public async Task <IActionResult> AddImageForBrand(int brandId, [FromForm] ImageForCreateDto imageForCreateDto)
        {
            var brand = await _repo.GetBrandById(brandId);

            if (brand == null)
            {
                return(NotFound());
            }

            var file         = imageForCreateDto.File;
            var uploadResult = new ImageUploadResult();

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File = new FileDescription(file.Name, stream)
                    };
                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }

            imageForCreateDto.Url      = uploadResult.Url.ToString();
            imageForCreateDto.PublicId = uploadResult.PublicId;

            var image = _mapper.Map <Image>(imageForCreateDto);

            image.IsThumbnail = true;
            brand.Image       = image;

            if (await _repo.SaveAll())
            {
                var routeValues = new { brandId, image.Id };
                return(CreatedAtRoute(nameof(GetBrandImageById), routeValues, image));
            }

            return(BadRequest("Could not add the image"));
        }