예제 #1
0
        private void addTextButton_Click(object sender, EventArgs e)
        {
            var title               = textTitleTextBox.Text;
            var authorsStr          = textAuthorsTextBox.Text;
            var genresStr           = textGenresTextBox.Text;
            var yearOfPublishingStr = textYearOfPublishingTextBox.Text;
            var version             = textVersionTextBox.Text;

            if (string.IsNullOrWhiteSpace(title) || title == titleTextBoxText || string.IsNullOrWhiteSpace(version))
            {
                MessageBox.Show("Title and version cannot be empty.");
                return;
            }

            var authorsStrList = authorsStr.Split(';');
            var genresStrList  = genresStr.Split(';');

            Console.WriteLine(string.Join(", ", genresStrList));
            Console.WriteLine(string.Join(", ", authorsStrList.Select(program.AuthorDao.GetByName).ToList()));
            Console.WriteLine(string.Join(", ", genresStrList.Select(program.GenreDao.GetByName).ToList()));

            var authors = authorsStrList.Select(program.AuthorDao.GetByName).Where(obj => obj != null).ToList();
            var genres  = genresStrList.Select(program.GenreDao.GetByName).Where(obj => obj != null).ToList();

            if (authors.Count == 0)
            {
                MessageBox.Show("Invalid Author(s)");
                return;
            }
            if (genres.Count == 0)
            {
                MessageBox.Show("Invalid Genre(s)");
                return;
            }

            try
            {
                DateTime yearOfPublishing = DateTime.Parse(yearOfPublishingStr);

                var text = new Text {
                    Title = title, YearOfPublishing = yearOfPublishing, Version = version
                };
                text.AddAuthors(authors);
                text.AddGenres(genres);

                program.TextDao.Create(text);

                MessageBox.Show($"Successfully created text {title}");

                var textsForm = new TextsForm(program);
                Visible = false;
                textsForm.ShowDialog();
                this.Close();
            } catch (FormatException) {
                MessageBox.Show("Year Of Publishing has an invalid date.");
            }
        }