public async Task <IActionResult> CreateMessage(int userId, UserForMessageDto userForMessageDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } userForMessageDto.SenderId = userId; var recipient = await _repository.GetUser(userForMessageDto.RecipientId); if (recipient == null) { BadRequest("Could not find user!!"); } var message = _mapper.Map <Message>(userForMessageDto); _repository.Add(message); // var messageToReturn = _mapper.Map<messageforEndUser>(message); if (await _repository.SaveAll()) { var messageforEndUser = _repository.GetMessage(message.Id); var messageToReturn = _mapper.Map <UserForMessageDto>(messageforEndUser.Result); return(CreatedAtRoute("GetMessage", new { userId, id = message.Id }, messageToReturn)); } throw new System.Exception("Creating the message failed on save!!"); }
public async Task <IActionResult> AddPhotosForUser(int userId, [FromForm] PhotoForCreationDto photoForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = 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.FileName, 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(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("Could not add 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"); }