コード例 #1
0
        public async Task <IActionResult> GetListing(int id)
        {
            var listing = await _repo.GetListing(id);

            var listingtoreturn = _mapper.Map <ListingForDetailDto>(listing);

            return(Ok(listingtoreturn));
        }
コード例 #2
0
        public async Task <IActionResult> AddPhotoForListing(int listingId, [FromForm] PhotoForCreationDto photoForCreationDto)
        {
            var listingfromrepo = await _repo.GetListing(listingId);

            var file          = photoForCreationDto.File;
            var uploadResults = 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(500).Height(500).Crop("fill").Gravity("face")
                    };
                    uploadResults = _cloudinary.Upload(uploadParams);
                }
            }

            photoForCreationDto.PhotoUrl = uploadResults.Uri.ToString();
            photoForCreationDto.PublicId = uploadResults.PublicId;

            var photo = _mapper.Map <Photo>(photoForCreationDto);

            if (!listingfromrepo.Photos.Any(u => u.IsMain))
            {
                photo.IsMain = true;
            }
            if (listingfromrepo.Photos.Count == 0)
            {
                listingfromrepo.PhotoUrl = photo.PhotoUrl;
            }

            listingfromrepo.Photos.Add(photo);

            if (await _repo.SaveAll())
            {
                var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn));
            }
            return(BadRequest("Could not add photo"));
        }