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 { id = message.Id }, messageToReturn)); } throw new Exception("Creating the message failed on save"); }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreateDTO photoForCreateDTO) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId); var file = photoForCreateDTO.File; var uploudResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()){ var uploadparm = new ImageUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation() .Width(500).Height(500).Crop("fill").Gravity("face") }; uploudResult = _cloudinary.Upload(uploadparm); } } photoForCreateDTO.Url = uploudResult.Uri.ToString(); photoForCreateDTO.PublicId = uploudResult.PublicId; var photo = Mapper.Map <Photo>(photoForCreateDTO); if (!userFromRepo.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } userFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { var photoToReturn = _mapper.Map <PhotoForReturnDTO>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photo)); } return(BadRequest("Error Uplod of 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 Exception($"Updateing user {id} failed on save"); }