예제 #1
0
        public ActionResult SuccesfullyCreated(Questions.Models.CreateQuestionModel model)
        {
            if (ModelState.IsValid)
            {
                Question question = new Question
                {
                    QuestionNote      = model.QuestionNote,
                    QuestionRating    = 0,
                    QuestionSentence  = model.QuestionSentence,
                    UserId            = User.Identity.GetUserId(),
                    AnswerAmount      = 0,
                    DateOfPublication = DateTime.Now
                };

                db.Questions.Add(question);
                db.SaveChanges();

                if (model.Tags != null)
                {
                    String[] substrings  = model.Tags.Split(' ');
                    String[] substrings2 = substrings.Distinct().ToArray();
                    Tag      someTag     = new Tag();
                    foreach (var tag in substrings2)
                    {
                        if (tag != " ")
                        {
                            if (db.Tags.Any(i => i.Name == tag))
                            {
                                someTag         = db.Tags.First(i => i.Name.Equals(tag));
                                someTag.Amount += 1;

                                db.Entry(someTag).State = System.Data.Entity.EntityState.Modified;
                                db.SaveChanges();
                            }
                            else
                            {
                                someTag = new Tag
                                {
                                    Amount = 1,
                                    Name   = tag
                                };
                                db.Tags.Add(someTag);
                                db.SaveChanges();
                            }
                        }
                        QuestionTags = new QuestionTags
                        {
                            QuestionID = question.ID,
                            TagID      = someTag.ID
                        };
                        db.QuetionTags.Add(QuestionTags);
                        db.SaveChanges();
                    }
                }

                db.SaveChanges();
                return(RedirectToAction("Question", "Questions", new { id = question.ID }));
            }
            return(View());
        }
        public int SaveEditedQuestion(String q, String a, String n, List <int> t, int id)
        {
            var qa = c.QuestionAnswerContext.First(qu => qu.Id == id);

            qa.Question = q;
            qa.Answer   = a;
            qa.Notes    = n;
            try
            {
                c.SaveChanges();
            }catch (DbUpdateException e)
            {
                Debug.WriteLine(e);
                return(0);
            }
            c.QuestionTagsContext.RemoveRange(c.QuestionTagsContext.Where(ta => ta.QId == id));
            try
            {
                c.SaveChanges();
            }
            catch (DbUpdateException e)
            {
                Debug.WriteLine(e);
                return(0);
            }
            foreach (int tid in t)
            {
                QuestionTags qt = new QuestionTags()
                {
                    QId = id, TagId = tid
                };
                c.QuestionTagsContext.Add(qt);
                try
                {
                    c.SaveChanges();
                }
                catch (DbUpdateException e)
                {
                    return(0);
                }
            }
            return(1);
        }
예제 #3
0
        public string GetValidationError(string propertyName)
        {
            string result = null;

            switch (propertyName)
            {
            case "QuestionTitle":
                if (QuestionTitle.Count() > 50 || QuestionTitle.Count() == 0)
                {
                    result = "Title can't be longer that 50 characters";
                }
                break;

            case "QuestionContent":
                if (QuestionContent.Count() > 500 || QuestionContent.Count() == 0)
                {
                    result = "Content can't be longer that 500 characters";
                }
                break;

            case "QuestionTags":
                foreach (var tag in QuestionTags.Split(' ').ToList())
                {
                    if (tag.Count() > 10 || QuestionTags.Count() == 0)
                    {
                        result = "Tags must be separated with spaces and can't be longer that 10 characters";
                        break;
                    }
                }
                break;

            case "QuestionAuthor":
                if (QuestionAuthor.Count() > 15 || !QuestionAuthor.All(char.IsLetterOrDigit) || QuestionAuthor.Count() == 0)
                {
                    result = "Username can't be longer that 15 characters";
                }
                break;
            }

            return(result);
        }
        public bool PutQuestion(String question, String answer, int dept, int mod, String notes, List <int> tags)
        {
            QuestionAnswer qa = new QuestionAnswer()
            {
                Question = question, Answer = answer, Status = true, Department = dept, Module = mod, Notes = notes, Timespend = 0
            };

            c.QuestionAnswerContext.Add(qa);
            c.SaveChanges();
            var id = qa.Id;

            foreach (int tid in tags)
            {
                QuestionTags qt = new QuestionTags()
                {
                    QId = id, TagId = tid
                };
                c.QuestionTagsContext.Add(qt);
                c.SaveChanges();
            }
            return(true);
        }
예제 #5
0
 public static Port <IAddQuestionResult> AddQuestion(string title, QuestionLength body, QuestionTags tags) =>
 NewPort <AddQuestionCmd, IAddQuestionResult>(new AddQuestionCmd(title, body, tags));