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> GetUser(int id) { var user = await _repo.GetUser(id); var userToReturn = _mapper.Map <UserForDetailedDto>(user); return(Ok(userToReturn)); }
public async Task <IActionResult> AddImageForUser(int userId, [FromForm] ImageForCreationDto imageForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId); var file = imageForCreationDto.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("limit") }; uploadResult = _cloudinary.Upload(uploadParams); } } imageForCreationDto.Url = uploadResult.Uri.ToString(); imageForCreationDto.PublicId = uploadResult.PublicId; var image = _mapper.Map <Image>(imageForCreationDto); if (!userFromRepo.Images.Any(i => i.IsMain)) { image.IsMain = true; } userFromRepo.Images.Add(image); if (await _repo.SaveAll()) { var imageToReturn = _mapper.Map <ImageForReturnDto>(image); return(CreatedAtRoute("GetImage", new { id = image.Id }, imageToReturn)); } return(BadRequest("Could not add the photo")); }