Example #1
0
        public static ICreateQuestionResult CreateQuestion(CreateQuestionCmd createQuestionCommand)
        {
            if (string.IsNullOrWhiteSpace(createQuestionCommand.Title))
            {
                var errors = new List <string>()
                {
                    "Title is missing"
                };
                return(new QuestionValidationFailed(errors));
            }

            if (createQuestionCommand.Title.Length < 15 && !string.IsNullOrWhiteSpace(createQuestionCommand.Title))
            {
                var errors = new List <string>()
                {
                    "Title cannot be shorter than 15 characters."
                };
                return(new QuestionValidationFailed(errors));
            }

            if (createQuestionCommand.Title.Length > 150)
            {
                var errors = new List <string>()
                {
                    "Title cannot be longer than 150 characters."
                };
                return(new QuestionValidationFailed(errors));
            }

            if (string.IsNullOrWhiteSpace(createQuestionCommand.Body))
            {
                var errors = new List <string>()
                {
                    "Body is missing"
                };
                return(new QuestionValidationFailed(errors));
            }

            if (createQuestionCommand.Body.Length < 30 && !string.IsNullOrWhiteSpace(createQuestionCommand.Title))
            {
                var errors = new List <string>()
                {
                    "Body cannot be shorter than 30 characters."
                };
                return(new QuestionValidationFailed(errors));
            }

            if (createQuestionCommand.Body.Length > 30000)
            {
                var errors = new List <string>()
                {
                    "Body is limited to 30000 characters; you entered 100124."
                };
                return(new QuestionValidationFailed(errors));
            }

            if (createQuestionCommand.Tags.Length < 1)
            {
                var errors = new List <string>()
                {
                    "Please enter at least one tag; see a list of popular tags."
                };
                return(new QuestionValidationFailed(errors));
            }

            if (new Random().Next(10) > 1)
            {
                return(new QuestionNotCreated("Question could not be verified"));
            }

            var questionId = Guid.NewGuid();
            var result     = new QuestionCreated(questionId, createQuestionCommand.Title, "*****@*****.**", true);

            if (result.MLApproved)
            {
                result.SetQuestionProfileCount();
                Console.WriteLine(result.QuestionProfileNumber);
            }
            else
            {
                Console.WriteLine(result.QuestionProfileNumber);
                QuestionNotCreated feedback = new QuestionNotCreated("Question closed. Please edit it or delete it.");
            }

            //execute logic
            return(result);
        }