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 the user")); } var message = _mapper.Map <Message>(messageForCreationDto); _repo.Add(message); if (await _repo.SaveAll()) { var MessageToReturn = _mapper.Map <MessageToReturnDto>(message); return(CreatedAtRoute("GetMessage", new { userId, id = message.Id }, MessageToReturn)); } throw new Exception("Creating the message failed"); }
public async Task <IActionResult> UpdateUser(int id, UserForUpdate user) { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var UserFromRepo = await this._dating.GetUser(id); _mapper.Map(user, UserFromRepo); if (await _dating.SaveAll()) { return(NoContent()); } throw new Exception($"Update User {id} failed on save"); }
public async Task <IActionResult> AddPhotosforUser(int userId, [FromForm] PhotosForCreationDto PhotoCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var UserFromRepo = await _dating.GetUser(userId); var file = PhotoCreationDto.File; var uploadResult = new ImageUploadResult(); if (file.Length >= 0) { using (var stream = file.OpenReadStream()) { var UploadParam = new ImageUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face") }; uploadResult = _cloudinary.Upload(UploadParam); } PhotoCreationDto.Url = uploadResult.Uri.ToString(); PhotoCreationDto.PublicId = uploadResult.PublicId; var Photo = _autoMapper.Map <Photo>(PhotoCreationDto); if (!UserFromRepo.Photos.Any(q => q.IsMain)) { Photo.IsMain = true; } UserFromRepo.Photos.Add(Photo); if (await _dating.SaveAll()) { var photoToReturn = _autoMapper.Map <PhotoForReturnDto>(Photo); return(CreatedAtRoute("GetPhoto", new { UserId = userId, id = Photo.Id }, photoToReturn)); } } return(BadRequest("could not add the photo")); }
public async Task <IActionResult> UpdateUser(int id, UserForUpdateVM user) { if (id == 0) { return(Unauthorized()); } var userRepo = await _service.GetUser(id); _mapper.Map(user, userRepo); if (await _service.SaveAll()) { return(NoContent()); } throw new Exception($"Updating user {id} failed on save"); }
public async Task <IActionResult> AddPhotoForUser(int userId, PhotoForCreationVM photoCreation) { if (userId == 0) { return(Unauthorized()); } var userRepo = await _repository.GetUser(userId); var file = photoCreation.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); } } photoCreation.Url = uploadResult.Url.ToString(); photoCreation.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoCreation); if (!userRepo.Photos.Any(e => e.IsMain)) { photo.IsMain = true; } userRepo.Photos.Add(photo); if (await _repository.SaveAll()) { var photoToReturn = _mapper.Map <PhotoForReturnVM>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.ID }, photoToReturn)); } return(BadRequest("Could not add a photo")); }