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> GetUser(int id) { var user = await _repo.GetUser(id); var userToReturn = _mapper.Map <UserForDetailedDto>(user); return(Ok(userToReturn)); }
public async Task <IActionResult> GetUsers([FromQuery] UserParams userParams) { var userID = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); var userfromRepo = await _dating.GetUser(userID); userParams.UserID = userID; if (string.IsNullOrEmpty(userParams.Gender)) { userParams.Gender = userfromRepo.Gender == "male" ? "female" : "male"; } var Users = await _dating.GetAllUsers(userParams); var UsersToReturn = _mapper.Map <IEnumerable <UsersforListDto> >(Users); Response.AddPaginationHeader(Users.CurrentPage, userParams.pageSize, Users.TotalCount, Users.TotalPages); return(Ok(UsersToReturn)); }
public async Task <IActionResult> AddPhotoForUser(int userId, PhotoForCreationDto photoDto) { var user = await _repo.GetUser(userId); if (user == null) { return(BadRequest("Could not find user")); } var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); if (currentUserId != user.Id) { return(Unauthorized()); } var file = photoDto.File; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream) }; uploadResult = _cloudinary.Upload(uploadParams); } } photoDto.Url = uploadResult.Uri.ToString(); photoDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoDto); photo.User = user; if (!user.Photos.Any(m => m.IsMain)) { photo.IsMain = true; } user.Photos.Add(photo); if (await _repo.SaveAllAsync()) { var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn)); } return(BadRequest("Could not add the photo")); }
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> 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")); }
public async Task <IActionResult> GetUser(int id) { return(Ok(_mapper.Map <UserForDetailsVM>(await _service.GetUser(id)))); }