public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto) { var sender = await _datingService.GetUser(userId); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDto.SenderId = userId; var recipient = await _datingService.GetUser(messageForCreationDto.RecipientId); if (recipient == null) { return(BadRequest("Could not find user")); } var message = _mapper.Map <Message>(messageForCreationDto); try { _messageService.Add(message); return(StatusCode(201)); } catch { throw new Exception("Creating the message failed on save"); } }
public async Task <IActionResult> GetUser(int id) { var user = await datingService.GetUser(id); var usersToReturn = mapper.Map <UserForDetailedDto>(user); return(Ok(usersToReturn)); }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _datingService.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.Name, stream), Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face") }; uploadResult = _cloudinary.Upload(uploadParams); } } photoForCreationDto.Url = uploadResult.Url.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoForCreationDto); photo.UserId = userId; photo.User = userFromRepo; if (!userFromRepo.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } try { _photoService.Add(photo); var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo); return(StatusCode(201)); } catch { return(BadRequest("Could not add the photo")); } }
public async Task <IActionResult> GetUser(Guid id) { User user = await _datingService.GetUser(id); UserForDetailsDTO userToReturn = _mapper.Map <UserForDetailsDTO>(user); return(Ok(userToReturn)); }
public async Task <IActionResult> GetUsers([FromQuery] UserParams userParams) { var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); var userFromrepo = await _datingService.GetUser(currentUserId); userParams.UserId = currentUserId; if (string.IsNullOrEmpty(userParams.Gender)) { userParams.Gender = userFromrepo.Gender == "male" ? "female" : "male"; } var users = await _datingService.GetUsers(userParams); var usersToReturn = _mapper.Map <IEnumerable <UserForListDto> >(users); Response.AddPagination(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages); return(Ok(usersToReturn)); }