public IActionResult AddQuestion(AddQuestionRequest model) { string tokenString = Request.Headers["Authorization"]; int currentUserId = JwtAthentication.GetCurrentUserId(tokenString); model.UserId = currentUserId; var questionId = _questionService.AddQuestion(model); return(Ok(new { questionId = questionId })); }
public IActionResult AddVoteForAnswer(UpdateVoteForAnswer model) { string tokenString = Request.Headers["Authorization"]; int currentUserId = JwtAthentication.GetCurrentUserId(tokenString); model.UserId = currentUserId; var totalVoteForAnswer = _answerService.UpdateVoteForAnswer(model); return(Ok(new { totalVoteForAnswer = totalVoteForAnswer })); }
public IActionResult CommentForAnswer(AddAnswerCommentRequest model) { string tokenString = Request.Headers["Authorization"]; int currentUserId = JwtAthentication.GetCurrentUserId(tokenString); var comments = _commentService.CommentForAnswer(currentUserId, model.AnswerId, model.CommentContent); comments.ForEach(x => x.AvatarUrl = Url.Content(string.Format("~/Images/UserAvatars/{0}", x.Avatar))); return(Ok(new { comments = comments })); }
public IActionResult AddAnswer(AddAnswerRequest model) { string tokenString = Request.Headers["Authorization"]; int currentUserId = JwtAthentication.GetCurrentUserId(tokenString); model.UserId = currentUserId; var answers = _answerService.AddAnswer(model); answers.ForEach(x => x.AvatarUrl = Url.Content(string.Format("~/Images/UserAvatars/{0}", x.Avatar))); return(Ok(new { answers = answers })); }
public IActionResult Login([FromBody] LoginModel login) { var user = _userService.Login(login); if (user == null) { return(BadRequest(new { message = "Username or password is incorrect" })); } var tokenString = JwtAthentication.GenerateJSONWebToken(_config["Jwt:Key"], _config["Jwt:Issuer"], user.Email, user.UserId); return(Ok(new { token = tokenString })); }
public async Task <IActionResult> GetQuestion([FromQuery] int pageIndex = 1) { string tokenString = Request.Headers["Authorization"]; int currentUserId = JwtAthentication.GetCurrentUserId(tokenString); var userId = _distributedCache.GetString(currentUserId.ToString()); int NoOfItem = Convert.ToInt32(_configuration["Setting:NumberOfItemNeedToBeCache"]); int pageSize = Convert.ToInt32(_configuration["Setting:PageSize"]); int totalQuestion = 0; string totalQuestionKey = "totalQuestion" + currentUserId.ToString(); List <Question> questions = new List <Question>(); if (!string.IsNullOrEmpty(userId)) { var output = await _distributedCache.GetAsync <IEnumerable <Question> >(currentUserId.ToString()); questions = output.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList(); questions.ForEach(x => x.AvatarUrl = Url.Content(string.Format("~/Images/UserAvatars/{0}", x.Avatar))); questions.ForEach(x => x.CategoryImageUrl = Url.Content(string.Format("~/Images/Categories/{0}", x.ImageName))); string totalQues = await _distributedCache.GetAsync <string>(totalQuestionKey); totalQuestion = Convert.ToInt32(totalQues); } else { var questionsToCache = _questionService.GetQuestions(currentUserId, NoOfItem, 1, out totalQuestion); questions = questionsToCache.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList(); questions.ForEach(x => x.AvatarUrl = Url.Content(string.Format("~/Images/UserAvatars/{0}", x.Avatar))); questions.ForEach(x => x.CategoryImageUrl = Url.Content(string.Format("~/Images/Categories/{0}", x.ImageName))); await _distributedCache.SetAsync <IEnumerable <Question> >(currentUserId.ToString(), questionsToCache, new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(2) }); await _distributedCache.SetAsync <string>(totalQuestionKey, totalQuestion.ToString(), new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(2) }); } GetQuestionsResponse response = new GetQuestionsResponse() { Questions = questions, TotalQuestion = totalQuestion }; return(Ok(new { response = response })); }
public IActionResult Profile() { string tokenString = Request.Headers["Authorization"]; try { int userId = JwtAthentication.GetCurrentUserId(tokenString); var user = _userService.GetUserById(userId); user.AvatarURL = Url.Content(string.Format("~/Images/UserAvatars/{0}", user.Avatar)); return(Ok(user)); } catch (Exception) { return(NotFound()); } }
public IActionResult GetQuestion([FromQuery] int pageIndex = 1) { string tokenString = Request.Headers["Authorization"]; int currentUserId = JwtAthentication.GetCurrentUserId(tokenString); int pageSize = Convert.ToInt32(_configuration["Setting:PageSize"]); int totalQuestion = 0; var questions = _questionService.GetQuestions(currentUserId, pageSize, pageIndex, out totalQuestion).ToList(); questions.ForEach(x => x.AvatarUrl = Url.Content(string.Format("~/Images/UserAvatars/{0}", x.Avatar))); questions.ForEach(x => x.CategoryImageUrl = Url.Content(string.Format("~/Images/Categories/{0}", x.ImageName))); GetQuestionsResponse response = new GetQuestionsResponse() { Questions = questions, TotalQuestion = totalQuestion }; return(Ok(new { response = response })); }