From cd675d40db125bad4da9d20b60e062f21e1a17db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=BA=D0=B0=D1=82=D0=B5=D1=80=D0=B8=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=A0=D0=BE=D0=B3=D0=B0=D1=88=D0=BE=D0=B2=D0=B0?= Date: Mon, 8 May 2023 22:43:33 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8=20=D1=81?= =?UTF-8?q?=20=D0=B4=D0=BE=D0=B1=20=D0=B0=D0=B2=D1=82=D0=BE=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BookShop/Implements/BookStorage.cs | 2 + ... 20230508181531_InitialCreate.Designer.cs} | 6 +- ...ate.cs => 20230508181531_InitialCreate.cs} | 0 .../BookShopDatabaseModelSnapshot.cs | 4 +- BookShop/Models/Book.cs | 22 ++++-- BookShopContracts/ViewModels/BookViewModel.cs | 3 + BookShopView/FormBook.Designer.cs | 23 +++++++ BookShopView/FormBook.cs | 67 ++++++------------- BookShopView/FormBook.resx | 6 ++ 9 files changed, 79 insertions(+), 54 deletions(-) rename BookShop/Migrations/{20230507194055_InitialCreate.Designer.cs => 20230508181531_InitialCreate.Designer.cs} (98%) rename BookShop/Migrations/{20230507194055_InitialCreate.cs => 20230508181531_InitialCreate.cs} (100%) diff --git a/BookShop/Implements/BookStorage.cs b/BookShop/Implements/BookStorage.cs index 004445f..0591b85 100644 --- a/BookShop/Implements/BookStorage.cs +++ b/BookShop/Implements/BookStorage.cs @@ -18,6 +18,7 @@ namespace BookShopDataBaseImplement.Implements { using var context = new BookShopDatabase(); return context.Books + .Include(x => x.Genre) .Include(x => x.Authors) .ThenInclude(x => x.Author) .ToList() @@ -96,6 +97,7 @@ namespace BookShopDataBaseImplement.Implements { using var context = new BookShopDatabase(); var element = context.Books + .Include(x => x.Genre) .Include(x => x.Authors) .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) diff --git a/BookShop/Migrations/20230507194055_InitialCreate.Designer.cs b/BookShop/Migrations/20230508181531_InitialCreate.Designer.cs similarity index 98% rename from BookShop/Migrations/20230507194055_InitialCreate.Designer.cs rename to BookShop/Migrations/20230508181531_InitialCreate.Designer.cs index 9af2189..50f76eb 100644 --- a/BookShop/Migrations/20230507194055_InitialCreate.Designer.cs +++ b/BookShop/Migrations/20230508181531_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace BookShopDataBaseImplement.Migrations { [DbContext(typeof(BookShopDatabase))] - [Migration("20230507194055_InitialCreate")] + [Migration("20230508181531_InitialCreate")] partial class InitialCreate { /// @@ -181,11 +181,13 @@ namespace BookShopDataBaseImplement.Migrations modelBuilder.Entity("BookShopDataBaseImplement.Models.Book", b => { - b.HasOne("BookShopDataBaseImplement.Models.Genre", null) + b.HasOne("BookShopDataBaseImplement.Models.Genre", "Genre") .WithMany("Books") .HasForeignKey("GenreId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + + b.Navigation("Genre"); }); modelBuilder.Entity("BookShopDataBaseImplement.Models.BookAuthor", b => diff --git a/BookShop/Migrations/20230507194055_InitialCreate.cs b/BookShop/Migrations/20230508181531_InitialCreate.cs similarity index 100% rename from BookShop/Migrations/20230507194055_InitialCreate.cs rename to BookShop/Migrations/20230508181531_InitialCreate.cs diff --git a/BookShop/Migrations/BookShopDatabaseModelSnapshot.cs b/BookShop/Migrations/BookShopDatabaseModelSnapshot.cs index 611f48c..d6d2917 100644 --- a/BookShop/Migrations/BookShopDatabaseModelSnapshot.cs +++ b/BookShop/Migrations/BookShopDatabaseModelSnapshot.cs @@ -178,11 +178,13 @@ namespace BookShopDataBaseImplement.Migrations modelBuilder.Entity("BookShopDataBaseImplement.Models.Book", b => { - b.HasOne("BookShopDataBaseImplement.Models.Genre", null) + b.HasOne("BookShopDataBaseImplement.Models.Genre", "Genre") .WithMany("Books") .HasForeignKey("GenreId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + + b.Navigation("Genre"); }); modelBuilder.Entity("BookShopDataBaseImplement.Models.BookAuthor", b => diff --git a/BookShop/Models/Book.cs b/BookShop/Models/Book.cs index 68967ce..53729a6 100644 --- a/BookShop/Models/Book.cs +++ b/BookShop/Models/Book.cs @@ -23,7 +23,7 @@ namespace BookShopDataBaseImplement.Models public double Cost { get; set; } [Required] public int GenreId { get; set; } - + public virtual Genre Genre { get; set; } private Dictionary? _bookAuthors = null; [NotMapped] public Dictionary BookAuthors @@ -49,6 +49,7 @@ namespace BookShopDataBaseImplement.Models BookName = model.BookName, Cost = model.Cost, Count = model.Count, + GenreId = model.GenreId, Authors = model.BookAuthors.Select(x => new BookAuthor { Author = context.Authors.First(y => y.Id == x.Key) @@ -59,14 +60,27 @@ namespace BookShopDataBaseImplement.Models { BookName = model.BookName; Cost = model.Cost; + Cost = model.Cost; + GenreId = model.GenreId; } - public BookViewModel GetViewModel => new() + public BookViewModel GetViewModel { - Id = Id, + + get + + { + using var context = new BookShopDatabase(); + return new BookViewModel + { + Id = Id, BookName = BookName, Cost = Cost, + GenreId= GenreId, + GenreName = context.Genres.FirstOrDefault(x => x.Id == GenreId)?.GenreName ?? string.Empty, BookAuthors = BookAuthors - }; +}; + } +} public void UpdateAuthors(BookShopDatabase context, BookBindingModel model) { var bookAuthors = context.BookAuthors.Where(rec => diff --git a/BookShopContracts/ViewModels/BookViewModel.cs b/BookShopContracts/ViewModels/BookViewModel.cs index b9e382d..a599071 100644 --- a/BookShopContracts/ViewModels/BookViewModel.cs +++ b/BookShopContracts/ViewModels/BookViewModel.cs @@ -1,6 +1,7 @@ using BookShopDataModels.Models; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -14,6 +15,8 @@ namespace BookShopContracts.ViewModels public double Cost { get; set; } public int Count { get; set; } public int GenreId { get; set; } + [DisplayName("GenreName")] + public string GenreName { get; set; } = string.Empty; public Dictionary BookAuthors { get; set; } } } diff --git a/BookShopView/FormBook.Designer.cs b/BookShopView/FormBook.Designer.cs index a5a8937..ec02738 100644 --- a/BookShopView/FormBook.Designer.cs +++ b/BookShopView/FormBook.Designer.cs @@ -44,6 +44,8 @@ this.buttonSave = new System.Windows.Forms.Button(); this.buttonCancel = new System.Windows.Forms.Button(); this.comboBoxGenre = new System.Windows.Forms.ComboBox(); + this.ColumnID = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.ColumnAuthorSurname = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.groupBox.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); @@ -163,6 +165,9 @@ // this.dataGridView.BackgroundColor = System.Drawing.SystemColors.Control; this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { + this.ColumnID, + this.ColumnAuthorSurname}); this.dataGridView.Location = new System.Drawing.Point(6, 26); this.dataGridView.Name = "dataGridView"; this.dataGridView.RowHeadersWidth = 51; @@ -198,6 +203,21 @@ this.comboBoxGenre.Size = new System.Drawing.Size(261, 28); this.comboBoxGenre.TabIndex = 10; // + // ColumnID + // + this.ColumnID.HeaderText = "ID"; + this.ColumnID.MinimumWidth = 6; + this.ColumnID.Name = "ColumnID"; + this.ColumnID.Visible = false; + this.ColumnID.Width = 125; + // + // ColumnAuthorSurname + // + this.ColumnAuthorSurname.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; + this.ColumnAuthorSurname.HeaderText = "Автор"; + this.ColumnAuthorSurname.MinimumWidth = 6; + this.ColumnAuthorSurname.Name = "ColumnAuthorSurname"; + // // FormBook // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); @@ -216,6 +236,7 @@ this.Controls.Add(this.labelName); this.Name = "FormBook"; this.Text = "Книга"; + this.Load += new System.EventHandler(this.FormBook_Load); this.groupBox.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); this.ResumeLayout(false); @@ -241,5 +262,7 @@ private Button buttonSave; private Button buttonCancel; private ComboBox comboBoxGenre; + private DataGridViewTextBoxColumn ColumnID; + private DataGridViewTextBoxColumn ColumnAuthorSurname; } } \ No newline at end of file diff --git a/BookShopView/FormBook.cs b/BookShopView/FormBook.cs index 4fe7242..a571e6d 100644 --- a/BookShopView/FormBook.cs +++ b/BookShopView/FormBook.cs @@ -32,38 +32,26 @@ namespace BookShopView _bookAuthors = new Dictionary(); _logicG = logicG; } - private readonly List? _list; - public int GenreId - { - get - { - return Convert.ToInt32(comboBoxGenre.SelectedValue); - } - set - { - comboBoxGenre.SelectedValue = value; - } - } - public IGenreModel? GenreModel - { - get - { - if (_list == null) - { - return null; - } - foreach (var elem in _list) - { - if (elem.Id == GenreId) - { - return elem; - } - } - return null; - } - } + private void FormBook_Load(object sender, EventArgs e) { + _logger.LogInformation("Загрузка жанров"); + try + { + var list = _logicG.ReadList(null); + if (list != null) + { + comboBoxGenre.DisplayMember = "Genre"; + comboBoxGenre.ValueMember = "ID"; + comboBoxGenre.DataSource = list.Select(c => c.GenreName).ToList(); + comboBoxGenre.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки списка жанров"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } if (_id.HasValue) { _logger.LogInformation("Загрузка книги"); @@ -93,28 +81,13 @@ namespace BookShopView } private void LoadData() { - _logger.LogInformation("Загрузка жанров"); - try - { - var list = _logicG.ReadList(null); - if (list != null) - { - comboBoxGenre.DisplayMember = "GenreName"; - comboBoxGenre.ValueMember = "ID"; - comboBoxGenre.DataSource = list; - comboBoxGenre.SelectedItem = null; - } - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка загрузки списка жанров"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + _logger.LogInformation("Загрузка автора книги"); try { if (_bookAuthors != null) { + dataGridView.Rows.Clear(); foreach (var pc in _bookAuthors) { diff --git a/BookShopView/FormBook.resx b/BookShopView/FormBook.resx index f298a7b..adad73e 100644 --- a/BookShopView/FormBook.resx +++ b/BookShopView/FormBook.resx @@ -57,4 +57,10 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + True + + + True + \ No newline at end of file