public async Task <IActionResult> UpdateUser(int id, UserForUpdateDto userForUpdateDto) { //Determine if the user editing the profile matches the user being edited: if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(id); //Check if any changes were made in our DTO: if (userForUpdateDto.Introduction == userFromRepo.Introduction) { return(NoContent()); } _mapper.Map(userForUpdateDto, userFromRepo); // Map data from our dto to model if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception($"Updating user {id} failed on save..."); }
public async Task <IActionResult> AddToUserArchive(int userId, ArchivedPhotoForCreationDto archivedPhotoForProfileDto) { //Determine if the user adding the photo is authorized: if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) //access the current user's token and compare Ids with the profile being accessed { return(Unauthorized()); } //Check if the photo is already archived for this user if (await _repo.ArchivedPhotoExists(userId, archivedPhotoForProfileDto.PhotoId)) { return(BadRequest("Photo is already archived!")); } var userFromRepo = await _repo.GetUser(userId); var archivedPhoto = _mapper.Map <ArchivedPhoto>(archivedPhotoForProfileDto); userFromRepo.Archived.Add(archivedPhoto); if (await _repo.SaveAll()) { return(NoContent()); } return(BadRequest("Could not add photo to archive")); }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreationDto) { //Determine if the user adding the photo is authorized: if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) //access the current user's token and compare Ids with the profile being accessed { return(Unauthorized()); } //Get our user var userFromRepo = await _repo.GetUser(userId); var file = photoForCreationDto.File; var uploadResult = new ImageUploadResult(); // to store the result from cloudinary if (file.Length > 0) { using (var stream = file.OpenReadStream()) // use a 'using' statment to dispose of data read { var uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation().Width(600).Crop("scale") // scale our images to a decent viewable size }; uploadResult = _cloudinary.Upload(uploadParams); // upload our file and get the link associated with that file }; // dispose of photo data in memory / close stream when done } photoForCreationDto.Url = uploadResult.Uri.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; photoForCreationDto.Author = userFromRepo.Username; photoForCreationDto.AuthorPhotoUrl = userFromRepo.PhotoUrl; var photo = _mapper.Map <Photo>(photoForCreationDto); userFromRepo.Posts.Add(photo); if (await _repo.SaveAll()) { var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { userId = userId, id = photo.Id }, photoToReturn)); } return(BadRequest("Could not add photo")); }
public async Task <IActionResult> UpdateLikes(int photoId, PhotoForGalleryDto photoForGalleryDto) { //Determine if photo exists: // Get photo from repo var photoFromRepo = await _repo.GetPhoto(photoId); if (photoFromRepo == null) { return(BadRequest("Photo doesn't exist")); } //Update the number of likes: photoForGalleryDto.Likes += 1; _mapper.Map(photoForGalleryDto, photoFromRepo); if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception($"Updating photo {photoId} failed on save"); }