public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto) { var sender = await _repo.GetUser(userId); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDto.SenderId = userId; var recipient = await _repo.GetUser(messageForCreationDto.RecipientId); if (recipient == null) { return(BadRequest("Could not find user")); } var message = _mapper.Map <Message>(messageForCreationDto); _repo.Add(message); if (await _repo.SaveAll()) { var messageToReturn = _mapper.Map <MessageToReturnDto>(message); return(CreatedAtRoute("GetMessage", new { userId, id = message.Id }, messageToReturn)); } throw new Exception("Creating the message failed on save"); }
public async Task <IActionResult> CreateMessage(int userId, // metodo per inviare un messaggio MessageForCreationDto messageForCreationDto) { var sender = await _repo.GetUser(userId); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDto.SenderId = userId; var recipient = await _repo.GetUser(messageForCreationDto.RecipientId); if (recipient == null) { return(BadRequest("Impossibile trovare utente")); } var message = _mapper.Map <Message>(messageForCreationDto); // message -> destinazione _repo.Add(message); if (await _repo.SaveAll()) { var messageToReturn = _mapper.Map <MessageToReturnDto>(message); return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn)); } throw new Exception("Creazione messaggio fallita in salvataggio"); }
[HttpPost] // serve per aggiungere foto public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) // serve per vedere se siamo autorizzati { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId); var file = photoForCreationDto.File; var uploadResult = new ImageUploadResult(); // archiviamo il risultato che ci da il cloud 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") }; uploadResult = _cloudinary.Upload(uploadParams); } } photoForCreationDto.Url = uploadResult.Uri.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoForCreationDto); if (!userFromRepo.Photos.Any(u => u.IsMain)) // se questo da FALSE vuol dire che l'utente non ha una foto principale { photo.IsMain = true; } userFromRepo.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 the photo")); }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFormRepo = await _repo.GetUser(userId); 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), // to fetch the file name Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face") // if the image soze is high then we need to crop the image }; uploadResult = _cloudinary.Upload(uploadParams); } } photoForCreationDto.Url = uploadResult.Uri.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoForCreationDto); if (!userFormRepo.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } userFormRepo.Photos.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 the Photo")); }
public async Task <IActionResult> UpdateUser(int id, UserForUpdateDto userForUpdateDto) { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(id); _mapper.Map(userForUpdateDto, userFromRepo); if (await _repo.SaveAll()) { return(NoContent()); } throw new System.Exception($"Updating user {id} failed on save"); }
[HttpPut("{id}")] // viene usato per aggiornare risorse nella API public async Task <IActionResult> UpdateUser(int id, UserForUpdateDto userForUpdateDto) // il modo per vedere se l'utente è colui che ha inviato il token al server { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(id); _mapper.Map(userForUpdateDto, userFromRepo); if (await _repo.SaveAll()) // se la modfica è andata a buon fine allora ritorna NoContent { return(NoContent()); } throw new Exception($"Updating user {id} failed on save"); }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto) { //We are fetching this sender info becasue we want the senderPhotoUrl. // It is getting set with the help of AutoMaaper where the values of URL and knownAs is getting set automatically var sender = await _repo.GetUser(userId); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDto.SenderId = userId; var recipient = await _repo.GetUser(messageForCreationDto.RecipientId); //Added by saurabh --start-- // We can use this method of just fetching back the photo and assigning it to messageToReturn // But we do not do that since AUto Mapper as a special magic which automatically sets matching parmaters // from other objects. in Our case recipentPhotoUrl, recipientKnowAs, SenderPhotoUrl, SenderKnownAs. //var photo = await _repo.GetPhoto(userId); if (recipient == null) { return(BadRequest("Could not find user")); } var message = _mapper.Map <Message>(messageForCreationDto); _repo.Add(message); if (await _repo.SaveAll()) { var messageToReturn = _mapper.Map <MessageToReturnDto>(message); //Added by saurabh //messageToReturn.SenderPhotoUrl = photo.Url; return(CreatedAtRoute("GetMessage", new { userId = userId, id = message.Id }, messageToReturn)); } throw new System.Exception("Creating the message failed on save"); }