public async Task <IActionResult> CreateMessage(int userId, [FromBody] MessageForCreationDto messageForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDto.SenderId = userId; var recipent = await _repo.GetUser(messageForCreationDto.RecipientId); var sender = await _repo.GetUser(messageForCreationDto.SenderId); if (recipent == null) { return(BadRequest("Could not find user")); } var message = _mapper.Map <Message>(messageForCreationDto); _repo.Add(message); var messageToReturn = _mapper.Map <MessageToReturnDto>(message); if (await _repo.SaveAll()) { return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn)); } throw new Exception("Create message failed"); }
public async Task <IActionResult> GetUsers(UserParams userParams) { var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); var userFromRepo = await _repo.GetUser(currentUserId); userParams.UserId = currentUserId; if (string.IsNullOrEmpty(userParams.Gender)) { // userParams.Gender = userFromRepo.Gender == "male" ? "female" : "male"; } var users = await _repo.GetUsers(userParams); var usersToReturn = _mapper.Map <IEnumerable <UserForListDto> >(users); Response.AddPagination(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages); return(Ok(usersToReturn)); }