public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDto.SenderId = userId; var recipient = await _repo.GetUser(messageForCreationDto.RecipientId); if (recipient == null) { return(BadRequest("Não foi possível encontrar o usuário!")); } var message = _mapper.Map <Messages>(messageForCreationDto); _repo.Add(message); var messageToReturn = _mapper.Map <MessageForCreationDto>(message); if (await _repo.SaveAll()) { return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn)); } throw new System.Exception("Falha ao salvar a mensagem"); }
public async Task <IActionResult> UpdateImovel(int id, ImovelForUpdateDto imovelForUpdateDto) { // if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) // return Unauthorized(); var imovelFromRepo = await _repo.GetImovel(id); _mapper.Map(imovelForUpdateDto, imovelFromRepo); if (await _repo.SaveAll()) { return(NoContent()); } throw new System.Exception($"Updating imovel {id} failed on save"); }
public async Task <IActionResult> AddPhotoForImovel(int imovelId, [FromForm] PhotoForCreationDto photoForCreationDto) { { //if (imovelId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) // return Unauthorized(); var imovelFromRepo = await _repo.GetImovel(imovelId); 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), 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 (!imovelFromRepo.Fotos.Any(i => i.Principal)) { photo.Principal = true; } imovelFromRepo.Fotos.Add(photo); if (await _repo.SaveAll()) { var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn)); } return(BadRequest("Não foi possível inserir esta imagem!")); } }
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"); }