예제 #1
0
        public async Task <IActionResult> AddListing(ListingForAddDto l)
        {
            int userid = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            var userfromrepo = await _repo.GetUser(userid);

            var listingtocreate = _mapper.Map <Listing>(l);

            if (userfromrepo != null)
            {
                userfromrepo.Listings.Add(listingtocreate);
            }

            if (await _repo.SaveAll())
            {
                return(CreatedAtRoute("GetListing", new { id = listingtocreate.Id }, listingtocreate));
            }
            return(BadRequest("Could not add listing"));
        }
예제 #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"));
        }