public async Task <IActionResult> GetListing(int id) { var listing = await _repo.GetListing(id); var listingToReturn = _mapper.Map <ListingDetailsDto>(listing); listingToReturn.watcherCount = listing.Watchers.Count; return(Ok(listingToReturn)); }
public async Task <IActionResult> AddPhotoForListing(int listingId, [FromForm] PhotoForCreationDto photoForCreationDto) { var listingFromRepo = await _repo.GetListing(listingId); if (listingFromRepo.UserId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var file = photoForCreationDto.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); } } photoForCreationDto.Url = uploadResult.Uri.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <ListingPhoto>(photoForCreationDto); if (!listingFromRepo.Photos.Any(l => l.IsMain)) { photo.IsMain = true; } listingFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { var photoToReturn = _mapper.Map <ListingPhotoForReturnDto>(photo); return(CreatedAtRoute("GetListingPhoto", new { listingId, id = photo.Id }, photoToReturn)); } return(BadRequest("Could not save the listing photo")); }
public async Task <IActionResult> WatchListing(int id, int listingId) { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } if (await _repo.GetListing(listingId) == null) { return(NotFound()); } var listingWatch = await _repo.GetListingWatch(id, listingId); string action; if (listingWatch != null) { action = "Unwatch"; _repo.Delete <ListingWatch>(listingWatch); } else { listingWatch = new ListingWatch { WatcherId = id, WatchingId = listingId }; action = "Watch"; _repo.Add <ListingWatch>(listingWatch); } var message = $"{action} listing successful. [WatcherId={listingWatch.WatcherId}, WatchingID={listingWatch.WatchingId}]"; if (await _repo.SaveAll()) { return(Ok(new { action, message })); } return(BadRequest($"Failed to {action} listing")); }