Exemple #1
0
        public IHttpActionResult Accept(Guid id)
        {
            Answer   answer   = db.Answers.Find(id);
            Question question = answer.Question;

            if (answer.Accepted == false)
            {
                var questionHasAnwser = question.Answers.Any(x => x.Accepted);
                if (!questionHasAnwser)
                {
                    string userId = User.Identity.GetUserId();
                    if (question.UserID != userId)
                    {
                        return(Unauthorized());
                    }

                    answer.AspNetUser.Credits        += question.Bounty;
                    answer.Accepted                   = true;
                    db.Entry(answer.AspNetUser).State = EntityState.Modified;
                    db.Entry(answer).State            = EntityState.Modified;
                    PointsUtils.AddCreditsAndXP(answer.AspNetUser.Id, answer.Question.CategoryID, 25, 40);
                    db.SaveChanges();
                }
            }

            return(Ok(new AnswerDTO(answer)));
        }
Exemple #2
0
        public IHttpActionResult DeleteComment(Guid id)
        {
            Comment comment = db.Comments.Find(id);

            if (comment == null)
            {
                return(NotFound());
            }
            Guid categoryId = Guid.Empty;

            if (comment.Answer != null)
            {
                categoryId = comment.Answer.Question.CategoryID;
            }
            else if (comment.Question != null)
            {
                categoryId = comment.Question.CategoryID;
            }
            PointsUtils.AddCreditsAndXP(comment.UserID, categoryId, -30, 0);

            db.Comments.Remove(comment);
            db.SaveChanges();

            return(Ok(comment));
        }
        public IHttpActionResult DeleteQuestion(Guid id)
        {
            Question question = db.Questions.Find(id);

            if (question == null)
            {
                return(NotFound());
            }
            PointsUtils.AddCreditsAndXP(question.UserID, question.CategoryID, -100, 0);
            db.Questions.Remove(question);
            db.SaveChanges();

            return(Ok(question));
        }
Exemple #4
0
        public IHttpActionResult DeleteAnswer(Guid id)
        {
            Answer answer = db.Answers.Find(id);

            if (answer == null)
            {
                return(NotFound());
            }
            Category category = db.Categories.Single(x => x.ID == answer.Question.CategoryID);

            PointsUtils.AddCreditsAndXP(answer.UserID, category.ID, -50, 0);

            db.Answers.Remove(answer);
            db.SaveChanges();

            return(Ok(answer));
        }
Exemple #5
0
        public IHttpActionResult PostComment(Comment comment)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            comment.ID     = Guid.NewGuid();
            comment.Added  = DateTime.Now;
            comment.UserID = User.Identity.GetUserId();

            db.Comments.Add(comment);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (CommentExists(comment.ID))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }
            db      = new OutOfRangeEntities();
            comment = db.Comments.Find(comment.ID);

            Guid categoryId = Guid.Empty;

            if (comment.Answer != null)
            {
                categoryId = comment.Answer.Question.CategoryID;
            }
            else if (comment.Question != null)
            {
                categoryId = comment.Question.CategoryID;
            }

            PointsUtils.AddCreditsAndXP(comment.UserID, categoryId, 4, 7);

            return(CreatedAtRoute("DefaultApi", new { id = comment.ID }, CommentDTO.FromEntity(comment)));
        }
Exemple #6
0
        public IHttpActionResult PostAnswer(Answer answer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            answer.ID     = Guid.NewGuid();
            answer.Added  = DateTime.Now;
            answer.UserID = User.Identity.GetUserId();

            db.Answers.Add(answer);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (AnswerExists(answer.ID))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            db     = new OutOfRangeEntities();
            answer = db.Answers.Find(answer.ID);

            Category category = db.Categories.Single(x => x.ID == answer.Question.CategoryID);

            PointsUtils.AddCreditsAndXP(answer.UserID, category.ID, 10, 15);

            return(CreatedAtRoute("DefaultApi", new { id = answer.ID }, AnswerDTO.FromEntity(answer)));
        }
        public IHttpActionResult PostQuestion(Question question)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            string     userId = User.Identity.GetUserId();
            AspNetUser user   = db.AspNetUsers.Single(x => x.Id == userId);

            if (question.Bounty > user.Credits && question.Bounty > 0)
            {
                return(BadRequest("Cannot add a bounty with more points than you have"));
            }
            user.Credits        -= question.Bounty;
            db.Entry(user).State = EntityState.Modified;
            List <Tag> removingTags = new List <Tag>();
            List <Tag> addingTags   = new List <Tag>();

            foreach (var tag in question.Tags)
            {
                var currentTag = db.Tags.SingleOrDefault(x => x.Name.ToLower() == tag.Name.ToLower());
                if (currentTag == null)
                {
                    tag.ID = Guid.NewGuid();

                    //Tag newTag = new Tag()
                    //{
                    //    Name = tag.Name.ToLower(),
                    //    Description = tag.Description,
                    //    ID = Guid.NewGuid()
                    //};
                    //db.Tags.Add(newTag);
                }
                else
                {
                    removingTags.Add(tag);
                    addingTags.Add(currentTag);
                }
            }

            foreach (var removingTag in removingTags)
            {
                question.Tags.Remove(removingTag);
            }

            foreach (var addingTag in addingTags)
            {
                question.Tags.Add(addingTag);
            }



            question.ID = Guid.NewGuid();

            //guid hardcodat al unui user
            if (User.Identity.IsAuthenticated)
            {
                question.UserID = userId;
            }
            else
            {
                question.UserID = "9e03ab56-d1c8-460a-ad48-fa7c6e69bf18";
            }

            question.Added    = DateTime.Now;
            question.Modified = DateTime.Now;
            //Category ca sa mearga (bine ca nu's puse FK inca)
            db.Questions.Add(question);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException e)
            {
                if (QuestionExists(question.ID))
                {
                    return(Conflict());
                }
                else
                {
                    throw e;
                }
            }
            PointsUtils.AddCreditsAndXP(userId, question.CategoryID, 10, 15);

            int questionsNumber = user.Questions.Where(x => x.CategoryID == question.CategoryID).Count();
            int badgeRarity     = PointsUtils.getQuestionBadgeRarity(questionsNumber);

            if (badgeRarity != -1)
            {
                PointsUtils.giveBadge(userId, question.CategoryID, "question", badgeRarity, question.ID);
            }

            db       = new OutOfRangeEntities();
            question = db.Questions.Find(question.ID);
            return(CreatedAtRoute("DefaultApi", new { id = question.ID }, QuestionDTO.FromEntity(question)));
        }