Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        private static void WriteComment(Guid articleId)
        {
            while (true)
            {
                Console.WriteLine("Желаете оставить комментарий? (1 - да, 2 - нет)");
                int answer = int.Parse(Console.ReadLine());

                var comment = new Comment();
                switch (answer)
                {
                case 1:
                    while (comment.AuthorName == null || comment.AuthorName == string.Empty)
                    {
                        Console.Write("Введите свое имя: ");
                        comment.AuthorName = Console.ReadLine();
                    }
                    while (comment.Text == null || comment.Text == string.Empty)
                    {
                        Console.Write("Введите ваш комментарий: ");
                        comment.Text = Console.ReadLine();
                    }
                    comment.Date      = DateTime.Now;
                    comment.ArticleId = articleId;

                    using (var repository = new CommentsRepository())
                    {
                        repository.Add(comment);
                    }

                    break;

                case 2: break;

                default:
                    Console.WriteLine("Такого варианта нет");
                    continue;
                }
                break;
            }
        }