public async Task <IActionResult> AddPhotoProduct(int productId, [FromForm] PhotoForCreateDto photoForCreateDto)
        {
            var product = await _repo.GetProduct(productId);

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

            if (file != null && file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation()
                                         .Width(500).Height(500).Crop("scale")
                    };
                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }
            photoForCreateDto.Url      = uploadResult.Uri.ToString();
            photoForCreateDto.PublicId = uploadResult.PublicId;
            var photo = _mapper.Map <PhotoForProduct>(photoForCreateDto);

            photo.ProductId = productId;
            _repo.Add(photo);
            if (await _repo.SaveAll())
            {
                var photoToRetrun = _mapper.Map <PhotoForReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { id = photo.PhotoId }, photoToRetrun));
            }

            return(BadRequest("خطا في اضافة الصورة"));
        }
示例#2
0
        [HttpGet("{id}", Name = "GetProduct")]//done
        public async Task <IActionResult> GetProduct(int id)
        {
            var categoryFroRepo = await _repo.GetProduct(id);

            var category = _mapper.Map <ProductReturnDTO>(categoryFroRepo);

            return(Ok(category));
        }