Ejemplo n.º 1
0
        public ResponseMessageModels GetQuestionComment(Question mQuestion)
        {
            ResponseMessageModels result = new ResponseMessageModels();
            var comments = from u in db.QuestionComments
                           where mQuestion.Id == u.QuestionId
                           select new
            {
                id           = u.Id,
                message      = u.Message,
                questionid   = u.QuestionId,
                created_date = u.CreatedDate,
                user         = (from user in db.Users where user.Id == u.UserId
                                select new
                {
                    id = user.Id,
                    email = user.Email,
                    username = user.UserName,
                    thumbnail = user.Thumbnail,
                }).FirstOrDefault()
            };

            result.status  = 1;
            result.message = "Get Comment List Successfully";
            result.data    = comments;
            return(result);
        }
Ejemplo n.º 2
0
        public ResponseMessageModels GetCreatedQuestion(User mUser, [FromUri] int limit, [FromUri] int skip, [FromUri] DateTime dateTime)
        {
            ResponseMessageModels result = new ResponseMessageModels();
            var question = (from u in db.Questions
                            where u.UserId == mUser.Id && u.CreatedDate <= dateTime
                            orderby u.CreatedDate descending
                            select new
            {
                id = u.Id,
                title = u.Title,
                thumbnail = u.Thumbnail,
                level = u.Level,
                colorid = u.ColorId,
                content = u.Content,
                type = u.Type,
                comment_count = u.CommentCount,
                like_count = u.LikeCount,
                answer_count = u.AnswerCount,
                created_date = u.CreatedDate,
                is_liked = (from like in db.QuestionLikes where mUser.Id == like.UserId && like.QuestionId == u.Id select like.UserId).Count() > 0,
            }).Take(limit).Skip(skip);

            result.status  = 1;
            result.message = "";
            result.data    = question;
            return(result);
        }
Ejemplo n.º 3
0
        public ResponseMessageModels EditProfile(User mUser)
        {
            ResponseMessageModels result = new ResponseMessageModels();
            User user = db.Users.Find(mUser.Id);

            if (user == null)
            {
                result.status  = 0;
                result.message = "Error: User ID Invalid";
                result.data    = null;
                return(result);
            }
            user.UserName  = mUser.UserName;
            user.Thumbnail = mUser.Thumbnail;
            db.SaveChanges();
            var putBackUser = (from u in db.Users
                               where u.Id == user.Id && u.Email == user.Email
                               select new
            {
                id = u.Id,
                email = u.Email,
                username = u.UserName,
                thumbnail = u.Thumbnail,
                type = u.Type
            }).FirstOrDefault();

            result.status  = 1;
            result.message = "Get User Successfully";
            result.data    = putBackUser;
            return(result);
        }
Ejemplo n.º 4
0
        public ResponseMessageModels Login(User mUser)
        {
            ResponseMessageModels result = new ResponseMessageModels();

            if (mUser.Email == null || mUser.HashPass == null)
            {
                result.status  = 0;
                result.message = "Email or Password is empty";
                result.data    = null;
                return(result);
            }
            String mEmail = mUser.Email + ".com";
            var    user   = (from u in db.Users
                             where u.Email == mEmail && u.HashPass == mUser.HashPass
                             select new {
                id = u.Id,
                email = u.Email,
                username = u.UserName,
                thumbnail = u.Thumbnail,
                type = u.Type,
                created_date = u.CreatedDate
            }).FirstOrDefault();

            if (user == null)
            {
                result.status  = 0;
                result.message = "Invailid Email or Passsword";
                result.data    = null;
                return(result);
            }
            result.status  = 1;
            result.message = "Login Successfully";
            result.data    = user;
            return(result);
        }
Ejemplo n.º 5
0
        public ResponseMessageModels LikeQuestion(QuestionLike mQuestionLike)
        {
            ResponseMessageModels result = new ResponseMessageModels();
            var likes = (from u in db.QuestionLikes
                         where u.UserId == mQuestionLike.UserId && u.QuestionId == mQuestionLike.QuestionId
                         select u).FirstOrDefault();
            Question question = db.Questions.Find(mQuestionLike.QuestionId);

            if (likes == null)
            {
                db.QuestionLikes.Add(mQuestionLike);
                question.LikeCount++;
                db.SaveChanges();
                result.status  = 1;
                result.message = "Liked";
                result.data    = (from like in db.QuestionLikes where like.QuestionId == mQuestionLike.QuestionId select mQuestionLike.Id).Count();
                return(result);
            }
            db.QuestionLikes.Remove(likes);
            question.LikeCount--;
            question.LikeCount = question.LikeCount > 0 ? question.LikeCount-- : 0;
            db.SaveChanges();
            result.status  = 2;
            result.message = "Dislike";
            result.data    = question.LikeCount;
            return(result);
        }
Ejemplo n.º 6
0
        public ResponseMessageModels GetChoices()
        {
            ResponseMessageModels result = new ResponseMessageModels();
            var choice = from u in db.Choices
                         select new
            {
                id            = u.Id,
                message       = u.Message,
                thumbnail     = u.Thumbnail,
                isTrue        = u.IsTrue,
                selectedCount = u.SelectedCount,
                questionId    = u.QuestionId
            };

            if (choice == null)
            {
                result.status  = 0;
                result.message = "Is Empty";
                result.data    = null;
            }
            result.status  = 1;
            result.message = "Get List Choice Successfully";
            result.data    = choice;
            return(result);
        }
Ejemplo n.º 7
0
        public ResponseMessageModels CrateQuestion(Question question)
        {
            ResponseMessageModels result = new ResponseMessageModels();

            question.AnswerCount = 0;
            for (int i = 0; i < question.Choices.Count; i++)
            {
                question.Choices.ElementAt(i).SelectedCount = 0;
            }
            question.CreatedDate = DateTime.Now;
            db.Questions.Add(question);
            db.SaveChanges();
            var mQuestion = from u in db.Questions
                            where u.Id == question.Id
                            select new
            {
                id           = u.Id,
                title        = u.Title,
                thumbnail    = u.Thumbnail,
                level        = u.Level,
                answercount  = u.AnswerCount,
                ishaveanswer = u.isHaveAnswer,
                colorid      = u.ColorId,
                content      = u.Content,
                type         = u.Type,
                created_date = u.CreatedDate,
                choices      = from choice in db.Choices
                               where choice.QuestionId == u.Id
                               select new
                {
                    id            = choice.Id,
                    message       = choice.Message,
                    istrue        = choice.IsTrue,
                    thumbnail     = choice.Thumbnail,
                    selectedcount = choice.SelectedCount
                },
                user = (from user in db.Users
                        where user.Id == u.UserId
                        select new
                {
                    username = user.UserName,
                    id = user.Id,
                    thumbnail = user.Thumbnail
                }).FirstOrDefault()
            };

            result.status  = 1;
            result.message = "Question is Created";
            result.data    = mQuestion;
            return(result);
        }
Ejemplo n.º 8
0
        public ResponseMessageModels GetAnsweredQuestion(User mUser, [FromUri] int limit, [FromUri] int skip, [FromUri] DateTime dateTime)
        {
            ResponseMessageModels result = new ResponseMessageModels();
            var question = (from u in db.Questions
                            join a in (from temp in db.QuestionAnswers where temp.UserId == mUser.Id select temp) on u.Id equals a.QuestionId
                            where a.UserId == mUser.Id && a.AnsweredDate <= dateTime
                            orderby u.CreatedDate descending
                            select new
            {
                id = u.Id,
                title = u.Title,
                thumbnail = u.Thumbnail,
                level = u.Level,
                colorid = u.ColorId,
                content = u.Content,
                type = u.Type,
                answered_date = a.AnsweredDate,
                selected_choice_id = a.ChoiceId,
                is_answered = true,
                is_have_answer = u.isHaveAnswer,
                comment_count = u.CommentCount,
                like_count = u.LikeCount,
                answer_count = u.AnswerCount,
                created_date = u.CreatedDate,
                is_liked = (from like in db.QuestionLikes where mUser.Id == like.UserId && like.QuestionId == u.Id select like.UserId).Count() > 0,
                choices = from choice in db.Choices
                          where choice.QuestionId == u.Id
                          select new
                {
                    id = choice.Id,
                    message = choice.Message,
                    istrue = choice.IsTrue,
                    thumbnail = choice.Thumbnail,
                    selectedcount = choice.SelectedCount
                },
                user = (from user in db.Users
                        where user.Id == u.UserId
                        select new
                {
                    username = user.UserName,
                    id = user.Id,
                    thumbnail = user.Thumbnail
                }).FirstOrDefault()
            }).Take(limit).Skip(skip);

            result.status  = 1;
            result.message = "Get List User Successfully";
            result.data    = question;
            return(result);
        }
Ejemplo n.º 9
0
        public ResponseMessageModels AddCommentQuestion(QuestionComment mComment)
        {
            ResponseMessageModels result = new ResponseMessageModels();

            mComment.CreatedDate = DateTime.Now;
            db.QuestionComments.Add(mComment);
            Question question = db.Questions.Find(mComment.QuestionId);

            question.CommentCount++;
            db.SaveChanges();
            result.status   = 1;
            result.message  = "Added Comment Successfully";
            result.data     = question.CommentCount;
            result.datetime = mComment.CreatedDate;
            return(result);
        }
Ejemplo n.º 10
0
        public ResponseMessageModels GetQuestionByID(Question mQuestion)
        {
            ResponseMessageModels result = new ResponseMessageModels();
            Question question            = db.Questions.Find(mQuestion.Id);

            if (question == null)
            {
                result.status  = 0;
                result.message = "Not Found";
                result.data    = null;
                return(result);
            }
            result.status  = 1;
            result.message = "Get Question Successfully";
            result.data    = question;
            return(result);
        }
Ejemplo n.º 11
0
        public ResponseMessageModels AnswerQuestion(QuestionAnswer mQuestionAnswer)
        {
            ResponseMessageModels result = new ResponseMessageModels();

            mQuestionAnswer.AnsweredDate = DateTime.Now;
            db.QuestionAnswers.Add(mQuestionAnswer);
            Choice   choice   = db.Choices.Find(mQuestionAnswer.ChoiceId);
            Question question = db.Questions.Find(mQuestionAnswer.QuestionId);

            choice.SelectedCount++;
            question.AnswerCount++;
            db.SaveChanges();
            result.status   = 1;
            result.message  = "Answered";
            result.data     = question.AnswerCount;
            result.datetime = mQuestionAnswer.AnsweredDate;
            return(result);
        }
Ejemplo n.º 12
0
        public ResponseMessageModels EditComment(QuestionComment mComment)
        {
            ResponseMessageModels result  = new ResponseMessageModels();
            QuestionComment       comment = db.QuestionComments.Find(mComment.Id);

            if (comment == null || mComment.UserId != comment.UserId)
            {
                result.status  = 0;
                result.message = "Unknown Error";
                result.data    = null;
                return(result);
            }
            comment.Message = mComment.Message;
            db.SaveChanges();
            result.status  = 1;
            result.message = "Edit Comment Successfully";
            result.data    = null;
            return(result);
        }
Ejemplo n.º 13
0
        public ResponseMessageModels EditQuestion(Question mQuestion)
        {
            ResponseMessageModels result = new ResponseMessageModels();
            Question question            = db.Questions.Find(mQuestion.Id);

            if (question == null || mQuestion.UserId != question.UserId)
            {
                result.status  = 0;
                result.message = "Unknown Error";
                result.data    = null;
                return(result);
            }
            question.Title = question.Title;
            db.SaveChanges();
            result.status  = 1;
            result.message = "Edit Question Successfully";
            result.data    = null;
            return(result);
        }
Ejemplo n.º 14
0
        public ResponseMessageModels Register(User mUser)
        {
            ResponseMessageModels result = new ResponseMessageModels();

            mUser.Email       = mUser.Email + ".com";
            mUser.CreatedDate = DateTime.Now;
            if (UserExists(mUser.Email))
            {
                result.status  = 0;
                result.message = "Register Fail, This is Email is used";
                result.data    = null;
                return(result);
            }
            db.Users.Add(mUser);
            db.SaveChanges();
            result.status  = mUser.Id;
            result.message = "Regsiter Successfully";
            result.data    = null;
            return(result);
        }
Ejemplo n.º 15
0
        public ResponseMessageModels Remove(Question mQuestion)
        {
            ResponseMessageModels result = new ResponseMessageModels();
            Question question            = db.Questions.Where(a => a.Id == mQuestion.Id).SingleOrDefault();

            if (question == null)
            {
                result.status  = 0;
                result.message = "Not Found";
                result.data    = null;
                return(result);
            }
            var choices = db.Choices.Where(b => b.QuestionId == question.Id).AsEnumerable();

            foreach (var choice in choices)
            {
                db.Choices.Remove(choice);
            }

            var comments = db.QuestionComments.Where(b => b.QuestionId == question.Id).AsEnumerable();

            foreach (var comment in comments)
            {
                db.QuestionComments.Remove(comment);
            }

            var likes = db.QuestionLikes.Where(b => b.QuestionId == question.Id).AsEnumerable();

            foreach (var like in likes)
            {
                db.QuestionLikes.Remove(like);
            }

            db.SaveChanges();
            db.Questions.Remove(question);
            db.SaveChanges();
            result.status  = 1;
            result.message = "Remove Successfuly";
            result.data    = null;
            return(result);
        }
Ejemplo n.º 16
0
        public ResponseMessageModels RemoveCommentQuestion(QuestionComment mComment)
        {
            ResponseMessageModels result  = new ResponseMessageModels();
            QuestionComment       comment = db.QuestionComments.Find(mComment.Id);

            if (comment.UserId == mComment.UserId)
            {
                db.QuestionComments.Remove(comment);
                db.SaveChanges();
                result.status  = 1;
                result.message = "Remove Comment Successfully";
                result.data    = null;
            }
            else
            {
                result.status  = 0;
                result.message = "Can't Remove Comment";
                result.data    = null;
            }
            return(result);
        }
Ejemplo n.º 17
0
        public ResponseMessageModels GetLike()
        {
            ResponseMessageModels result = new ResponseMessageModels();
            var likes = from u in db.QuestionLikes
                        select new
            {
                id         = u.Id,
                questionid = u.QuestionId,
                userid     = u.UserId
            };

            if (likes == null)
            {
                result.status  = 1;
                result.message = "NULL";
                result.data    = null;
                return(result);
            }
            result.status  = 2;
            result.message = "Successfully";
            result.data    = likes;
            return(result);
        }
Ejemplo n.º 18
0
        public ResponseMessageModels GetUser(User mUser)
        {
            ResponseMessageModels result = new ResponseMessageModels();
            var user = (from u in db.Users
                        where u.Id == mUser.Id && u.Email == mUser.Email
                        select new
            {
                id = u.Id,
                email = u.Email,
                username = u.UserName,
                thumbnail = u.Thumbnail
            }).FirstOrDefault();

            if (user == null)
            {
                result.status  = 0;
                result.message = "Login Session Time Out";
                result.data    = null;
            }
            result.status  = 1;
            result.message = "Get User Successfully";
            result.data    = user;
            return(result);
        }
Ejemplo n.º 19
0
        public ResponseMessageModels GetQuestion(User mUser, [FromUri] int limit, [FromUri] int skip, [FromUri] DateTime dateTime)
        {
            ResponseMessageModels result = new ResponseMessageModels();
            var mAnswer = from answer
                          in db.QuestionAnswers
                          where answer.UserId == mUser.Id
                          select new
            {
                AnswerQuestionId = answer.QuestionId,
                AnswerUserId     = answer.UserId
            };
            var mQuestions = from mQuestion in db.Questions
                             join a in mAnswer on mQuestion.Id equals a.AnswerQuestionId into answerTemp
                             from a in answerTemp.DefaultIfEmpty()
                             select new
            {
                Id           = mQuestion.Id,
                Title        = mQuestion.Title,
                Thumbnail    = mQuestion.Thumbnail,
                Level        = mQuestion.Level,
                ColorId      = mQuestion.ColorId,
                Content      = mQuestion.Content,
                Type         = mQuestion.Type,
                isHaveAnswer = mQuestion.isHaveAnswer,
                UserId       = mQuestion.UserId,
                AnswerCount  = mQuestion.AnswerCount,
                CommentCount = mQuestion.CommentCount,
                LikeCount    = mQuestion.LikeCount,
                CreatedDate  = mQuestion.CreatedDate,
                AnswerUserID = a == null ? -1 : a.AnswerUserId
            };
            var question = (from u in mQuestions
                            where u.AnswerUserID == -1 && u.CreatedDate <= dateTime
                            orderby u.CreatedDate descending
                            select new
            {
                id = u.Id,
                title = u.Title,
                thumbnail = u.Thumbnail,
                level = u.Level,
                colorid = u.ColorId,
                content = u.Content,
                type = u.Type,
                is_have_answer = u.isHaveAnswer,
                comment_count = u.CommentCount,
                like_count = u.LikeCount,
                answer_count = u.AnswerCount,
                created_date = u.CreatedDate,
                is_liked = (from like in db.QuestionLikes where mUser.Id == like.UserId && like.QuestionId == u.Id select like.UserId).Count() > 0,
                choices = from choice in db.Choices
                          where choice.QuestionId == u.Id
                          select new
                {
                    id = choice.Id,
                    message = choice.Message,
                    istrue = choice.IsTrue,
                    thumbnail = choice.Thumbnail,
                    selectedcount = choice.SelectedCount
                },
                user = (from user in db.Users
                        where user.Id == u.UserId
                        select new { username = user.UserName,
                                     id = user.Id,
                                     thumbnail = user.Thumbnail }).FirstOrDefault()
            }).Take(limit).Skip(skip);

            if (question == null)
            {
                result.status  = 0;
                result.message = "Is Empty";
                result.data    = null;
            }
            result.status  = 1;
            result.message = "Get List User Successfully";
            result.data    = question;
            return(result);
        }
Ejemplo n.º 20
0
        public ResponseMessageModels LoginWithSNS(User mUser)
        {
            ResponseMessageModels result = new ResponseMessageModels();

            if (mUser.SnsId == null)
            {
                result.status  = 0;
                result.message = "Login Faild";
                result.data    = null;
                return(result);
            }
            mUser.Email = mUser.Email + ".com";
            int id = SnsIdExists(mUser);

            if (id == -1)
            {
                if (UserExists(mUser.Email))
                {
                    result.status  = 3;
                    result.message = "Email is Exists";
                    result.data    = null;
                    return(result);
                }
                mUser.CreatedDate = DateTime.Now;
                db.Users.Add(mUser);
                db.SaveChanges();
                var putBack = (from u in db.Users
                               where u.Id == mUser.Id
                               select new
                {
                    id = u.Id,
                    email = u.Email,
                    username = u.UserName,
                    thumbnail = u.Thumbnail,
                    snsid = u.SnsId,
                    type = u.Type,
                    created_date = u.CreatedDate
                }).FirstOrDefault();
                result.status  = 1;
                result.message = "Register With Facebook Success";
                result.data    = putBack;
                return(result);
            }
            mUser.Id = id;
            User user = db.Users.SingleOrDefault(u => u.Id == mUser.Id);

            if (user == null)
            {
                result.status  = 2;
                result.message = "Error: User ID Invalid";
                result.data    = null;
                return(result);
            }
            user.Email     = mUser.Email;
            user.UserName  = mUser.UserName;
            user.Thumbnail = mUser.Thumbnail;
            db.SaveChanges();
            var putBackUser = (from u in db.Users
                               where u.Id == mUser.Id
                               select new
            {
                id = u.Id,
                email = u.Email,
                username = u.UserName,
                thumbnail = u.Thumbnail,
                snsid = u.SnsId,
                type = u.Type,
                created_date = u.CreatedDate
            }).FirstOrDefault();

            result.status  = 1;
            result.message = "Login With Facebook Success";
            result.data    = putBackUser;
            return(result);
        }