public async Task <IActionResult> CreateMessage(int userid, MessageForCreationDto messageForcreation) { var sender = await _repo.GetUser(userid); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForcreation.SenderId = userid; var recipient = await _repo.GetUser(messageForcreation.RecipientId); if (recipient == null) { return(BadRequest("Could not find user")); } var message = _mapper.Map <Message>(messageForcreation); _repo.Add(message); if (await _repo.SaveAll()) { var messagetoreturn = _mapper.Map <MessageToReturnDto>(message); return(CreatedAtRoute("GetMessage", new { id = message.Id }, messagetoreturn)); } throw new Exception("Could not create a message"); }
public async Task <IActionResult> AddPhotoUser(int userId, [FromForm] PhotoCreatingDto photodto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId); var file = photodto.File; var Uploadresults = 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") }; Uploadresults = _cloudinary.Upload(uploadparams); } } photodto.URL = Uploadresults.Uri.ToString(); photodto.PublicId = Uploadresults.PublicId; var photo = _maper.Map <Photo>(photodto); if (!userFromRepo.Photos.Any(p => p.mainphoto)) { photo.mainphoto = true; } userFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { var returnphoto = _maper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, returnphoto)); } return(BadRequest("Could not add the photo")); }
public async Task <IActionResult> Updateuser(int id, UserForUpdateDto updateuserdto) { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(id); _mapper.Map(updateuserdto, userFromRepo); if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception($"Updating user with id {id} faild"); }