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 }));
        }
Beispiel #2
0
        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 }));
        }
Beispiel #3
0
        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 }));
        }
Beispiel #4
0
        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 }));
        }
Beispiel #5
0
        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 }));
        }
Beispiel #6
0
        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 }));
        }
Beispiel #7
0
        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 }));
        }