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("User not found")); } var message = _mapper.Map <Message>(messageForCreationDto); _repo.Add <Message>(message); if (await _repo.SaveAll()) { var messageToReturn = _mapper.Map <MessageToReturnDto>(message); if (messageForCreationDto.ConnectionId != "") { await _hub.Clients.Client(messageForCreationDto.ConnectionId).SendAsync("SendMessageToUser", messageToReturn); } return(CreatedAtRoute("GetMessage", new { userId, id = message.Id }, messageToReturn)); } throw new Exception("Error while creating message"); }
public async Task <IActionResult> UploadPhoto(int userId, [FromForm] PhotoForCreationDto photoForCreation) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId); var file = photoForCreation.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); } } photoForCreation.Url = uploadresult.Url.ToString(); photoForCreation.PublicId = uploadresult.PublicId; var photo = _mapper.Map <Photo>(photoForCreation); if (!userFromRepo.Photos.Any(x => x.IsMain)) { photo.IsMain = true; } userFromRepo.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("Upload failed")); }
public async Task <IActionResult> UpdateUser(int id, UserforUpdateDto userforUpdate) { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(id); _mapper.Map(userforUpdate, userFromRepo); if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception($"Updating user {id} failed on save"); }