Exemplo n.º 1
0
        private static void WriteArticle()
        {
            var article = new Article();

            while (article.AuthorName == null || article.AuthorName == string.Empty)
            {
                Console.Write("Введите ваше имя: ");
                article.AuthorName = Console.ReadLine();
            }

            while (article.Theme == null || article.Theme == string.Empty)
            {
                Console.Write("Введите заголовок: ");
                article.Theme = Console.ReadLine();
            }

            while (article.Text == null || article.Text == string.Empty)
            {
                Console.WriteLine("Вводите текст новости: ");
                article.Text = Console.ReadLine();
            }

            article.Date = DateTime.Now;

            using (var repository = new ArticlesRepository())
            {
                repository.Add(article);
            }
        }
Exemplo n.º 2
0
        private static void ShowArticles()
        {
            List <Article> articles = new List <Article>();

            using (var repository = new ArticlesRepository())
            {
                articles = repository.Select();
            }
            for (int i = 0; i < articles.Count; i++)
            {
                Console.WriteLine($"{i + 1}: {articles[i].Theme}");
            }

            var selectedArticle = new Article();

            while (true)
            {
                Console.Write("Выберите номер статьи: ");
                int answer = int.Parse(Console.ReadLine());

                if (answer <= 0 || answer > articles.Count)
                {
                    Console.WriteLine("Такого номера нет");
                    continue;
                }

                selectedArticle = articles[answer - 1];
                break;
            }

            Console.WriteLine($"{selectedArticle.AuthorName} {selectedArticle.Date}\n{selectedArticle.Text}");

            var comments = new List <Comment>();

            using (var repository = new CommentsRepository())
            {
                comments = (from comment
                            in repository.Select()
                            where comment.ArticleId == selectedArticle.Id
                            select comment).AsList();
            }

            Console.WriteLine("Комментарии:");
            foreach (var comment in comments)
            {
                Console.WriteLine($"({comment.Date}) {comment.AuthorName}: {comment.Text}");
            }

            WriteComment(selectedArticle.Id);
        }