public JsonResult <IEnumerable <QuestionDTO> > GetQuestionsTag(string tag)
 {
     return
         (Json(
              db.Questions.Where(question => question.Tags.Select(x => x.Name).Contains(tag))
              .OrderByDescending(question => question.Bounty)
              .ThenByDescending(question => question.Added).ToList()
              .Select(question => QuestionDTO.FromEntity(question, 1))));
 }
 public JsonResult <IEnumerable <QuestionDTO> > GetQuestionsCategory(Guid id)
 {
     return
         (Json(
              db.Questions.OrderByDescending(question => question.Bounty)
              .ThenByDescending(question => question.Added).Where(question => question.CategoryID == id)
              .ToList()
              .Select(question => QuestionDTO.FromEntity(question, 1))));
 }
 public JsonResult <IEnumerable <QuestionDTO> > GetBounties()
 {
     return
         (Json(
              db.Questions.Where(question => question.Bounty > 0)
              .OrderByDescending(x => x.Bounty)
              .ToList()
              .Select(question => QuestionDTO.FromEntity(question))));
 }
        public JsonResult <IEnumerable <QuestionDTO> > GetSearch(string q)
        {
            var searchResult           = db.Search(q);
            var en                     = searchResult.GetEnumerator();
            List <QuestionDTO> results = new List <QuestionDTO>();

            while (en.MoveNext())
            {
                results.Add(QuestionDTO.FromEntity(en.Current));
            }
            return(Json(results.AsEnumerable()));
        }
        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)));
        }