public void ValidQuestionTag()
 {
     QuestionTag q = new QuestionTag { QuestionId = 1, TagId = 3 };
     db.QuestionTags.Add(q);
     db.SaveChanges();
 }
 public void InvalidQuestionTag1()
 {
     QuestionTag q = new QuestionTag { QuestionId = 1 };
     db.QuestionTags.Add(q);
     db.SaveChanges();
 }
 public void InvalidQuestionTag2()
 {
     QuestionTag q = new QuestionTag { TagId = 3 };
     db.QuestionTags.Add(q);
     db.SaveChanges();
 }
        public ActionResult Nieuw(Vraag info)
        {
            HTMLSanitizer hs = new HTMLSanitizer();
            Markdown md = new Markdown();
            // to mark the first time (meaning don't check the tags if there are some new ones)
            bool firstTime = false;
            // check where we came from
            if (Request.UrlReferrer.LocalPath == "/vraag/check")
                firstTime = true; // set the first time if we came from there
            if (info.vraag == null) // check if the question isn't empty
                ModelState.AddModelError("", "Ga terug en voer een titel voor je vraag in.");
            else if (info.vraag != null && (info.tags == null || info.content == null) && !firstTime) // check if all the fields are filled in
                ModelState.AddModelError("", "Vul alle velden in aub.");
            else if (!firstTime)
            {
                // Fields are met hmm let's see if we can find all the tags perhaps :D
                List<string> tagsList = new List<string>(); // new generic list which will contain our tags which we aren't in the database
                info.tags = info.tags.Trim(',', '!', '?', ':', ';', '.');
                string[] tags = info.tags.Split(' '); // assume all tags are splitted by a space (beg)
                if (tags.Count() > 5)
                    ModelState.AddModelError("", " U mag niet meer dan 5 tags meegeven.");
                List<string> foundTags = new List<string>();
                // check if the given tag exits in the datebase
                foreach (string tag in tags)
                {
                    var temp = from taggies in db.Tags
                               where taggies.Name.Contains(tag)
                               select taggies;
                    // if a tag is in the database the above query should find it. Else it is a new one.
                    if (temp.Count() == 0)  // not found add it to the list of new tags
                        tagsList.Add(tag);
                    else // else mark it as one we have already
                        foundTags.Add(tag);
                }

                // get the user information
                var userPosting = from postingUser in db.Users
                                  where postingUser.UserName == User.Identity.Name
                                  join userCombine in db.UserMeta on postingUser.UserID equals userCombine.UserId
                                  select userCombine;
                if (userPosting.Count() != 1)
                {
                    ModelState.AddModelError("", "U bent niet ingelogd."); // aan name dat de join lukt
                    ViewBag.LoggedIn = false;
                }
                // check if we got all new tags filled in and everything is valid
                if ((tagsList.Count() - info.CountTagsSumbitted()) == 0 && ModelState.IsValid)
                {
                    // add the stuff since we are done :D
                    // also check if we have tags to add. If so. Let's add those first (so we can link stuff)
                    int UserID = userPosting.First().UserId;
                    var userPostingMeta = (from usermeta in db.UserMeta
                                           where usermeta.UserId == UserID
                                           select usermeta).Single();

                    // add each new tag to the database
                    for (int i = 0; i < tagsList.Count(); i++)
                    {
                        Tag newTag = new Tag() { Description = info.returnTagContent(i), Name = tagsList.ElementAt(i), Count = 1 };
                        userPostingMeta.Tags += 1;
                        db.Tags.Add(newTag);
                    }
                    // inceremnt the amount of questions that there are with this tag
                    foreach (string tagName in foundTags)
                    {
                        var tagAdd = from tagSelected in db.Tags
                                     where tagSelected.Name.Contains(tagName)
                                     select tagSelected;
                        tagAdd.First().Count += 1;
                    }

                    // add the amount of questions asked by this person with one
                    userPosting.First().Questions += 1;
                    UserMeta userInfo = userPosting.First();
                    // create the question
                    Question newQuestion = new Question() { Title = info.vraag, UserId = userInfo.UserId, Content = info.content };
                    // add it to the db
                    db.Questions.Add(newQuestion);
                    db.SaveChanges(); // to make sure that if there were new tags they are added propperly (so we can query the id's right)

                    // ####################################
                    //     check if we got a new badge
                    // ####################################
                    if (TagBadge.badgeAchieve(userInfo.UserId))
                        TagBadge.awardBadge(userInfo.UserId);
                    if (TagCreatorBadge.badgeAchieve(userInfo.UserId))
                        TagCreatorBadge.awardBadge(userInfo.UserId);
                    if (TagLordBadge.badgeAchieve(userInfo.UserId))
                        TagLordBadge.awardBadge(userInfo.UserId);
                    // ####################################

                    // query back our last question
                    // should be the first of this list
                    var justAsked = from questionAsked in db.Questions
                                    where questionAsked.UserId == userInfo.UserId
                                    orderby questionAsked.Created_At descending
                                    select questionAsked;
                    // query all the id's needed
                    Question justAskedQuestion = justAsked.First();
                    // foreach tag that there in on this question craete a tagID - QuestionID line
                    foreach (string tagje in tags)
                    {
                        // get the id and created an new QuestionTag for it
                        var tagIDPost = from tagIDje in db.Tags
                                        where tagIDje.Name.Contains(tagje)
                                        select tagIDje.TagID;
                        int postTag = tagIDPost.First();

                        QuestionTag newQuestTag = new QuestionTag() { QuestionId = justAskedQuestion.QuestionID, TagId = postTag };
                        db.QuestionTags.Add(newQuestTag);
                    }
                    db.SaveChanges();

                    // #################################
                    //    Check if we got a new badge
                    // #################################
                    if (QuestionBadge.badgeAchieve(userInfo.UserId))
                        QuestionBadge.awardBadge(userInfo.UserId);
                    if (QuestionCreatorBadge.badgeAchieve(userInfo.UserId))
                        QuestionCreatorBadge.awardBadge(userInfo.UserId);
                    if (QuestionLordBadge.badgeAchieve(userInfo.UserId))
                        QuestionLordBadge.awardBadge(userInfo.UserId);
                    // #################################

                    // if we get here we are done :D so take us to our just asked question
                    return RedirectToAction("view", "vraag", new { id = justAskedQuestion.QuestionID });
                }
                else
                {
                    // missing tags :<
                    // create a list for the missing tags :<
                    ViewBag.MissingTagList = tagsList;
                    // give allong a counter (since viewbag is doing nasty and won't let me count :<)
                    ViewBag.MissingTagListCount = tagsList.Count();
                }

            }
            return View();
        }