diff --git a/LibraryDBproject/LDBproj/AdditionalForms/BookF.cs b/LibraryDBproject/LDBproj/AdditionalForms/BookF.cs index 355d389..c40236e 100644 --- a/LibraryDBproject/LDBproj/AdditionalForms/BookF.cs +++ b/LibraryDBproject/LDBproj/AdditionalForms/BookF.cs @@ -1,12 +1,26 @@ -namespace LDBproject.AdditionalForms; +using LDBproject.Entities.Enums; +using LDBproject.Entities; +using LDBproject.Repositories; + +namespace LDBproject.AdditionalForms; public partial class BookF : Form { + private readonly IBookRep _bookRepository; private int? _bookID; - public BookF() + public BookF(IBookRep bookR) { InitializeComponent(); + _bookRepository = bookR ?? throw new ArgumentNullException(nameof(bookR)); + + StatusCbox.DataSource = Enum.GetValues(typeof(BookStat)); + + // Populate the checkbox list with enum values as before + foreach (var elem in Enum.GetValues(typeof(Genres))) + { + GenresChBoxList.Items.Add(elem); + } } public int ID @@ -15,6 +29,37 @@ public partial class BookF : Form { try { + var book = _bookRepository.GetBookByID(value); + + if (book == null) + { + throw new InvalidDataException(nameof(book)); + } + + // Clear all checkboxes + for (int i = 0; i < GenresChBoxList.Items.Count; i++) + { + GenresChBoxList.SetItemChecked(i, false); + } + + // Check the checkboxes based on the book's GenreIDs + foreach (Genres genre in Enum.GetValues(typeof(Genres))) + { + if ((genre & book.GenreMask) != 0) + { + int index = GenresChBoxList.Items.IndexOf(genre); + if (index != -1) + { + GenresChBoxList.SetItemChecked(index, true); + } + } + } + + TitleTb.Text = book.Title; + AuthorTb.Text = book.Author; + YearNud.Value = book.PublishYear; + StatusCbox.SelectedItem = book.Status; + _bookID = value; } catch (Exception ex) @@ -38,11 +83,11 @@ public partial class BookF : Form if (_bookID.HasValue) { - + _bookRepository.UpdateBook(CreateBook(_bookID.Value)); } else { - + _bookRepository.AddBook(CreateBook(0)); } Close(); @@ -56,5 +101,16 @@ public partial class BookF : Form private void DiscardBtn_Click(object sender, EventArgs e) => Close(); - private object CreateBook(int id) { return null; } + private Book CreateBook(int id) + { + Genres selectedGenres = Genres.None; + foreach (var item in GenresChBoxList.CheckedItems) + { + if (item is Genres genre) + selectedGenres |= genre; + } + + return Book.AddBook(id, TitleTb.Text, AuthorTb.Text, + (int)YearNud.Value, selectedGenres, (BookStat)StatusCbox.SelectedItem!); + } } diff --git a/LibraryDBproject/LDBproj/AdditionalForms/BookListF.cs b/LibraryDBproject/LDBproj/AdditionalForms/BookListF.cs index a6d9f5f..9d1ca7e 100644 --- a/LibraryDBproject/LDBproj/AdditionalForms/BookListF.cs +++ b/LibraryDBproject/LDBproj/AdditionalForms/BookListF.cs @@ -1,14 +1,17 @@ -using Unity; +using LDBproject.Repositories; +using Unity; namespace LDBproject.AdditionalForms; public partial class BookListF : Form { private readonly IUnityContainer _container; + private readonly IBookRep _bookR; - public BookListF(IUnityContainer container) + public BookListF(IUnityContainer container, IBookRep bookR) { InitializeComponent(); + _bookR = bookR ?? throw new ArgumentNullException(nameof(bookR)); _container = container ?? throw new ArgumentNullException(nameof(container)); } @@ -37,43 +40,6 @@ public partial class BookListF : Form } } - private void DelBtn_Click(object sender, EventArgs e) - { - if (!GetIDFromRow(out var foundID)) - { - return; - } - - if (MessageBox.Show("Remove element?", "Deleting", MessageBoxButtons.YesNo) != DialogResult.Yes) - { - return; - } - - try - { - ReloadList(); - } - catch (Exception ex) - { - MessageBox.Show(ex.Message, "Book List Form [ Error while removing element ]", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - - private void ReloadList() { } - - private bool GetIDFromRow(out int id) - { - id = 0; - if (DataGV.SelectedRows.Count < 1) - { - MessageBox.Show("[ Error : Book doesn't exist ]", "", - MessageBoxButtons.OK, MessageBoxIcon.Error); - } - - id = Convert.ToInt32(DataGV.SelectedRows[0].Cells["BookID"].Value); - return true; - } - private void UpdBtn_Click(object sender, EventArgs e) { if (!GetIDFromRow(out var findID)) @@ -93,4 +59,42 @@ public partial class BookListF : Form MessageBox.Show(ex.Message, "Book List Form [ Error while updating element ]", MessageBoxButtons.OK, MessageBoxIcon.Error); } } + + private void ReloadList() => DataGV.DataSource = _bookR.GetBookList(); + + private void DelBtn_Click(object sender, EventArgs e) + { + if (!GetIDFromRow(out var foundID)) + { + return; + } + + if (MessageBox.Show("Remove element?", "Deleting", MessageBoxButtons.YesNo) != DialogResult.Yes) + { + return; + } + + try + { + _bookR.DeleteBook(foundID); + ReloadList(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Book List Form [ Error while removing element ]", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private bool GetIDFromRow(out int id) + { + id = 0; + if (DataGV.SelectedRows.Count < 1) + { + MessageBox.Show("[ Error : Book doesn't exist ]", "", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + id = Convert.ToInt32(DataGV.SelectedRows[0].Cells["BookID"].Value); + return true; + } } diff --git a/LibraryDBproject/LDBproj/AdditionalForms/CustomerF.cs b/LibraryDBproject/LDBproj/AdditionalForms/CustomerF.cs index 80b2a3d..53f88d0 100644 --- a/LibraryDBproject/LDBproj/AdditionalForms/CustomerF.cs +++ b/LibraryDBproject/LDBproj/AdditionalForms/CustomerF.cs @@ -1,12 +1,17 @@ -namespace LDBproject.AdditionalForms; +using LDBproject.Entities; +using LDBproject.Repositories; + +namespace LDBproject.AdditionalForms; public partial class CustomerF : Form { + private readonly ICustomerCardsRep _customeRepository; private int? _custID; - public CustomerF() + public CustomerF(ICustomerCardsRep customeR) { InitializeComponent(); + _customeRepository = customeR ?? throw new ArgumentNullException(nameof(customeR)); } public int ID @@ -15,6 +20,16 @@ public partial class CustomerF : Form { try { + var reader = _customeRepository.GetCardByID(value); + + if (reader == null) + { + throw new InvalidDataException(nameof(reader)); + } + + FIOEnterTb.Text = reader.FIO; + BirthdayDTPicker.Value = reader.AgeBirthday; + _custID = value; } catch (Exception ex) @@ -37,11 +52,11 @@ public partial class CustomerF : Form if (_custID.HasValue) { - + _customeRepository.UpdateCard(CreateCustomer(_custID.Value)); } else { - + _customeRepository.AddCard(CreateCustomer(0)); } Close(); @@ -55,5 +70,8 @@ public partial class CustomerF : Form private void CancelBtn_Click(object sender, EventArgs e) => Close(); - private object CreateCustomer(int id) { return null; } + private CustomerCard CreateCustomer(int id) + { + return CustomerCard.AddCustomer(id, FIOEnterTb.Text, BirthdayDTPicker.Value); + } } diff --git a/LibraryDBproject/LDBproj/AdditionalForms/CustomerListF.cs b/LibraryDBproject/LDBproj/AdditionalForms/CustomerListF.cs index 3a8cf6c..2315f47 100644 --- a/LibraryDBproject/LDBproj/AdditionalForms/CustomerListF.cs +++ b/LibraryDBproject/LDBproj/AdditionalForms/CustomerListF.cs @@ -1,14 +1,17 @@ -using Unity; +using LDBproject.Repositories; +using Unity; namespace LDBproject.AdditionalForms; public partial class CustomerListF : Form { private readonly IUnityContainer _container; + private readonly ICustomerCardsRep _custR; - public CustomerListF(IUnityContainer container) + public CustomerListF(IUnityContainer container, ICustomerCardsRep customeR) { InitializeComponent(); + _custR = customeR ?? throw new ArgumentNullException(nameof(customeR)); _container = container ?? throw new ArgumentNullException(nameof(container)); } @@ -51,6 +54,7 @@ public partial class CustomerListF : Form try { + _custR.DeleteCard(foundID); ReloadList(); } catch (Exception ex) @@ -59,7 +63,8 @@ public partial class CustomerListF : Form } } - private void ReloadList() { } + private void ReloadList() => + DataGV.DataSource = _custR.GetCards(); private bool GetIDFromRow(out int id) { diff --git a/LibraryDBproject/LDBproj/AdditionalForms/EmployeeF.cs b/LibraryDBproject/LDBproj/AdditionalForms/EmployeeF.cs index d505054..15ea8cb 100644 --- a/LibraryDBproject/LDBproj/AdditionalForms/EmployeeF.cs +++ b/LibraryDBproject/LDBproj/AdditionalForms/EmployeeF.cs @@ -1,12 +1,22 @@ -namespace LDBproject.AdditionalForms; +using LDBproject.Entities.Enums; +using LDBproject.Entities; +using LDBproject.Repositories; + +namespace LDBproject.AdditionalForms; public partial class EmployeeF : Form { + private readonly ILibrarianRep _libRepository; private int? _librarianID; - public EmployeeF() + public EmployeeF(ILibrarianRep librarianR) { InitializeComponent(); + _libRepository = librarianR ?? throw new ArgumentNullException(nameof(librarianR)); + foreach (var elem in Enum.GetValues(typeof(Genres))) + { + GenresCheckedBL.Items.Add(elem); + } } public int ID @@ -15,6 +25,30 @@ public partial class EmployeeF : Form { try { + var librarian = _libRepository.GetCardByID(value); + if (librarian == null) + { + throw new InvalidDataException(nameof(librarian)); + } + + // Uncheck all genres in the checklist box + for (int i = 0; i < GenresCheckedBL.Items.Count; i++) + { + GenresCheckedBL.SetItemChecked(i, false); + } + // Check the genres that are in the bitmask + foreach (Genres genre in Enum.GetValues(typeof(Genres))) + { + if ((genre & librarian.GenreMask) != 0 && genre != Genres.None) + { + int index = GenresCheckedBL.Items.IndexOf(genre); + if (index != -1) + { + GenresCheckedBL.SetItemChecked(index, true); + } + } + } + FIOEnterTb.Text = librarian.FIO; _librarianID = value; } catch (Exception ex) @@ -37,11 +71,11 @@ public partial class EmployeeF : Form if (_librarianID.HasValue) { - + _libRepository.ChangeCardInfo(CreateWorker(_librarianID.Value)); } else { - + _libRepository.AddCard(CreateWorker(0)); } Close(); } @@ -54,6 +88,16 @@ public partial class EmployeeF : Form private void CancelBtn_Click(object sender, EventArgs e) => Close(); - private object CreateWorker(int id) { return null; } + private LibrarianCard CreateWorker(int id) + { + Genres selectedGenres = Genres.None; + foreach (var item in GenresCheckedBL.CheckedItems) + { + if (item is Genres genre) + selectedGenres |= genre; + } + + return LibrarianCard.AddWorker(id, FIOEnterTb.Text, selectedGenres); + } } diff --git a/LibraryDBproject/LDBproj/AdditionalForms/EmployeesF.cs b/LibraryDBproject/LDBproj/AdditionalForms/EmployeesF.cs index ea47543..19a1e97 100644 --- a/LibraryDBproject/LDBproj/AdditionalForms/EmployeesF.cs +++ b/LibraryDBproject/LDBproj/AdditionalForms/EmployeesF.cs @@ -1,14 +1,17 @@ -using Unity; +using LDBproject.Repositories; +using Unity; namespace LDBproject.AdditionalForms; public partial class EmployeesF : Form { private readonly IUnityContainer _container; + private readonly ILibrarianRep _libR; - public EmployeesF(IUnityContainer container) + public EmployeesF(IUnityContainer container, ILibrarianRep libR) { InitializeComponent(); + _libR = libR ?? throw new ArgumentNullException(nameof(libR)); _container = container ?? throw new ArgumentNullException(nameof(container)); } @@ -71,6 +74,7 @@ public partial class EmployeesF : Form try { + _libR.DeleteCard(foundID); ReloadList(); } catch (Exception ex) @@ -79,7 +83,7 @@ public partial class EmployeesF : Form } } - private void ReloadList() { } + private void ReloadList() => DataGV.DataSource = _libR.GetCards(); private bool GetiDFromRow(out int id) { diff --git a/LibraryDBproject/LDBproj/AdditionalForms/OrderRegistrations.cs b/LibraryDBproject/LDBproj/AdditionalForms/OrderRegistrations.cs index ad9fe83..dc808c6 100644 --- a/LibraryDBproject/LDBproj/AdditionalForms/OrderRegistrations.cs +++ b/LibraryDBproject/LDBproj/AdditionalForms/OrderRegistrations.cs @@ -1,14 +1,17 @@ -using Unity; +using LDBproject.Repositories; +using Unity; namespace LDBproject.AdditionalForms; public partial class OrderRegistrations : Form { private readonly IUnityContainer _container; + private readonly IOrderRep _regR; - public OrderRegistrations(IUnityContainer container) + public OrderRegistrations(IUnityContainer container, IOrderRep regR) { InitializeComponent(); + _regR = regR ?? throw new ArgumentNullException(nameof(regR)); _container = container ?? throw new ArgumentNullException(nameof(container)); } @@ -51,6 +54,7 @@ public partial class OrderRegistrations : Form try { + _regR.DeleteOrderinfo(foundID); ReloadList(); } catch (Exception ex) @@ -59,7 +63,8 @@ public partial class OrderRegistrations : Form } } - private void ReloadList() { } + private void ReloadList() => + DataGV.DataSource = _regR.GetOrdersInfo(); private bool GetIDFromRow(out int id) { diff --git a/LibraryDBproject/LDBproj/AdditionalForms/OrdersF.Designer.cs b/LibraryDBproject/LDBproj/AdditionalForms/OrdersF.Designer.cs new file mode 100644 index 0000000..dc0da81 --- /dev/null +++ b/LibraryDBproject/LDBproj/AdditionalForms/OrdersF.Designer.cs @@ -0,0 +1,112 @@ +namespace LDBproject.AdditionalForms +{ + partial class OrdersF + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + panel1 = new Panel(); + DelBtn = new Button(); + AddBtn = new Button(); + DataGV = new DataGridView(); + panel1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)DataGV).BeginInit(); + SuspendLayout(); + // + // panel1 + // + panel1.BackColor = Color.Transparent; + panel1.Controls.Add(DelBtn); + panel1.Controls.Add(AddBtn); + panel1.Dock = DockStyle.Right; + panel1.Location = new Point(430, 0); + panel1.Name = "panel1"; + panel1.Size = new Size(246, 364); + panel1.TabIndex = 4; + // + // DelBtn + // + DelBtn.BackColor = Color.DarkSlateBlue; + DelBtn.BackgroundImage = Properties.Resources.DelImg; + DelBtn.BackgroundImageLayout = ImageLayout.Stretch; + DelBtn.ForeColor = Color.Transparent; + DelBtn.Location = new Point(129, 34); + DelBtn.Name = "DelBtn"; + DelBtn.Size = new Size(78, 71); + DelBtn.TabIndex = 6; + DelBtn.UseVisualStyleBackColor = false; + DelBtn.Click += DelBtn_Click; + // + // AddBtn + // + AddBtn.BackColor = Color.DarkSlateBlue; + AddBtn.BackgroundImage = Properties.Resources.AddImg; + AddBtn.BackgroundImageLayout = ImageLayout.Stretch; + AddBtn.ForeColor = Color.Transparent; + AddBtn.Location = new Point(24, 34); + AddBtn.Name = "AddBtn"; + AddBtn.Size = new Size(78, 71); + AddBtn.TabIndex = 4; + AddBtn.UseVisualStyleBackColor = false; + AddBtn.Click += AddBtn_Click; + // + // DataGV + // + DataGV.BackgroundColor = Color.DarkSlateBlue; + DataGV.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + DataGV.GridColor = Color.GreenYellow; + DataGV.Location = new Point(28, 86); + DataGV.Name = "DataGV"; + DataGV.RowHeadersWidth = 62; + DataGV.Size = new Size(357, 245); + DataGV.TabIndex = 5; + // + // OrderRegistrations + // + AutoScaleDimensions = new SizeF(10F, 25F); + AutoScaleMode = AutoScaleMode.Font; + BackgroundImage = Properties.Resources.RegistrationsFrameCover; + BackgroundImageLayout = ImageLayout.Stretch; + ClientSize = new Size(676, 364); + Controls.Add(DataGV); + Controls.Add(panel1); + DoubleBuffered = true; + Name = "OrderRegistrations"; + Text = "OrderRegistrations"; + Load += RegistrationsF_Load; + panel1.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)DataGV).EndInit(); + ResumeLayout(false); + } + + #endregion + + private Panel panel1; + private Button DelBtn; + private Button AddBtn; + private DataGridView DataGV; + } +} \ No newline at end of file diff --git a/LibraryDBproject/LDBproj/AdditionalForms/OrdersF.cs b/LibraryDBproject/LDBproj/AdditionalForms/OrdersF.cs new file mode 100644 index 0000000..df97440 --- /dev/null +++ b/LibraryDBproject/LDBproj/AdditionalForms/OrdersF.cs @@ -0,0 +1,83 @@ +using LDBproject.Repositories; +using Unity; + +namespace LDBproject.AdditionalForms; + +public partial class OrdersF : Form +{ + private readonly IUnityContainer _container; + private readonly IOrderRep _regR; + + public OrdersF(IUnityContainer container, IOrderRep regR) + { + InitializeComponent(); + _regR = regR ?? throw new ArgumentNullException(nameof(regR)); + _container = container ?? throw new ArgumentNullException(nameof(container)); + } + + private void RegistrationsF_Load(object sender, EventArgs e) + { + try + { + ReloadList(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "[ Error while saving ]", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void AddBtn_Click(object sender, EventArgs e) + { + try + { + _container.Resolve().ShowDialog(); + ReloadList(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "[ Error while adding element ]", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void DelBtn_Click(object sender, EventArgs e) + { + if (!GetIDFromRow(out var foundID)) + { + return; + } + + if (MessageBox.Show("Remove element?", "Deleting", MessageBoxButtons.YesNo) != DialogResult.Yes) + { + return; + } + + try + { + _regR.DeleteOrderinfo(foundID); + ReloadList(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "[ Error while removing element ]", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ReloadList() => + DataGV.DataSource = _regR.GetOrdersInfo(); + + private bool GetIDFromRow(out int id) + { + id = 0; + if (DataGV.SelectedRows.Count < 1) + { + MessageBox.Show("[ Error : element doesn't exist ]", "", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + id = Convert.ToInt32(DataGV.SelectedRows[0].Cells["OrderID"].Value); + return true; + } +} + + diff --git a/LibraryDBproject/LDBproj/AdditionalForms/OrdersF.resx b/LibraryDBproject/LDBproj/AdditionalForms/OrdersF.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/LibraryDBproject/LDBproj/AdditionalForms/OrdersF.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/LibraryDBproject/LDBproj/AdditionalForms/RegOrder.Designer.cs b/LibraryDBproject/LDBproj/AdditionalForms/RegOrder.Designer.cs index b24be4d..8e5b811 100644 --- a/LibraryDBproject/LDBproj/AdditionalForms/RegOrder.Designer.cs +++ b/LibraryDBproject/LDBproj/AdditionalForms/RegOrder.Designer.cs @@ -31,15 +31,15 @@ LibrarianCBox = new ComboBox(); LibLabel = new Label(); DateLabel = new Label(); - ReturnDTPicker = new DateTimePicker(); + BorrowDTPicker = new DateTimePicker(); groupBox1 = new GroupBox(); DataGV = new DataGridView(); - BookColumnCBox = new DataGridViewComboBoxColumn(); - AuthorColumn = new DataGridViewTextBoxColumn(); SaveBtn = new Button(); BackBtn = new Button(); ReaderLabel = new Label(); CardCBox = new ComboBox(); + BookColumnCBox = new DataGridViewComboBoxColumn(); + NoteColumn = new DataGridViewTextBoxColumn(); groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)DataGV).BeginInit(); SuspendLayout(); @@ -47,42 +47,48 @@ // LibrarianCBox // LibrarianCBox.FormattingEnabled = true; - LibrarianCBox.Location = new Point(119, 21); + LibrarianCBox.Location = new Point(155, 44); + LibrarianCBox.Margin = new Padding(4); LibrarianCBox.Name = "LibrarianCBox"; - LibrarianCBox.Size = new Size(182, 33); + LibrarianCBox.Size = new Size(235, 40); LibrarianCBox.TabIndex = 0; // // LibLabel // LibLabel.AutoSize = true; - LibLabel.Location = new Point(30, 24); + LibLabel.Location = new Point(39, 47); + LibLabel.Margin = new Padding(4, 0, 4, 0); LibLabel.Name = "LibLabel"; - LibLabel.Size = new Size(83, 25); + LibLabel.Size = new Size(110, 32); LibLabel.TabIndex = 1; LibLabel.Text = "Librarian:"; // // DateLabel // DateLabel.AutoSize = true; - DateLabel.Location = new Point(30, 128); + DateLabel.Location = new Point(39, 164); + DateLabel.Margin = new Padding(4, 0, 4, 0); DateLabel.Name = "DateLabel"; - DateLabel.Size = new Size(107, 25); + DateLabel.Size = new Size(148, 32); DateLabel.TabIndex = 2; - DateLabel.Text = "Return date:"; + DateLabel.Text = "Borrow date:"; // - // ReturnDTPicker + // BorrowDTPicker // - ReturnDTPicker.Location = new Point(37, 163); - ReturnDTPicker.Name = "ReturnDTPicker"; - ReturnDTPicker.Size = new Size(264, 31); - ReturnDTPicker.TabIndex = 3; + BorrowDTPicker.Location = new Point(48, 209); + BorrowDTPicker.Margin = new Padding(4); + BorrowDTPicker.Name = "BorrowDTPicker"; + BorrowDTPicker.Size = new Size(342, 39); + BorrowDTPicker.TabIndex = 3; // // groupBox1 // groupBox1.Controls.Add(DataGV); - groupBox1.Location = new Point(334, 24); + groupBox1.Location = new Point(434, 31); + groupBox1.Margin = new Padding(4); groupBox1.Name = "groupBox1"; - groupBox1.Size = new Size(423, 266); + groupBox1.Padding = new Padding(4); + groupBox1.Size = new Size(550, 340); groupBox1.TabIndex = 4; groupBox1.TabStop = false; groupBox1.Text = "BookListGBox"; @@ -90,32 +96,20 @@ // DataGV // DataGV.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - DataGV.Columns.AddRange(new DataGridViewColumn[] { BookColumnCBox, AuthorColumn }); - DataGV.Location = new Point(6, 41); + DataGV.Columns.AddRange(new DataGridViewColumn[] { BookColumnCBox, NoteColumn }); + DataGV.Location = new Point(8, 52); + DataGV.Margin = new Padding(4); DataGV.Name = "DataGV"; DataGV.RowHeadersWidth = 62; - DataGV.Size = new Size(411, 206); + DataGV.Size = new Size(534, 264); DataGV.TabIndex = 0; // - // BookColumnCBox - // - BookColumnCBox.HeaderText = "Book's title"; - BookColumnCBox.MinimumWidth = 8; - BookColumnCBox.Name = "BookColumnCBox"; - BookColumnCBox.Width = 150; - // - // AuthorColumn - // - AuthorColumn.HeaderText = "Author"; - AuthorColumn.MinimumWidth = 8; - AuthorColumn.Name = "AuthorColumn"; - AuthorColumn.Width = 150; - // // SaveBtn // - SaveBtn.Location = new Point(37, 216); + SaveBtn.Location = new Point(48, 276); + SaveBtn.Margin = new Padding(4); SaveBtn.Name = "SaveBtn"; - SaveBtn.Size = new Size(264, 34); + SaveBtn.Size = new Size(343, 44); SaveBtn.TabIndex = 5; SaveBtn.Text = "Save and give out"; SaveBtn.UseVisualStyleBackColor = true; @@ -123,9 +117,10 @@ // // BackBtn // - BackBtn.Location = new Point(37, 256); + BackBtn.Location = new Point(48, 328); + BackBtn.Margin = new Padding(4); BackBtn.Name = "BackBtn"; - BackBtn.Size = new Size(264, 34); + BackBtn.Size = new Size(343, 44); BackBtn.TabIndex = 6; BackBtn.Text = "Go back"; BackBtn.UseVisualStyleBackColor = true; @@ -134,34 +129,51 @@ // ReaderLabel // ReaderLabel.AutoSize = true; - ReaderLabel.Location = new Point(30, 65); + ReaderLabel.Location = new Point(39, 90); + ReaderLabel.Margin = new Padding(4, 0, 4, 0); ReaderLabel.Name = "ReaderLabel"; - ReaderLabel.Size = new Size(66, 50); + ReaderLabel.Size = new Size(87, 64); ReaderLabel.TabIndex = 7; ReaderLabel.Text = "Reader\r\ncard:"; // // CardCBox // CardCBox.FormattingEnabled = true; - CardCBox.Location = new Point(119, 82); + CardCBox.Location = new Point(155, 105); + CardCBox.Margin = new Padding(4); CardCBox.Name = "CardCBox"; - CardCBox.Size = new Size(182, 33); + CardCBox.Size = new Size(235, 40); CardCBox.TabIndex = 8; // + // BookColumnCBox + // + BookColumnCBox.HeaderText = "Book's title"; + BookColumnCBox.MinimumWidth = 8; + BookColumnCBox.Name = "BookColumnCBox"; + BookColumnCBox.Width = 150; + // + // NoteColumn + // + NoteColumn.HeaderText = "Note"; + NoteColumn.MinimumWidth = 8; + NoteColumn.Name = "NoteColumn"; + NoteColumn.Width = 150; + // // RegOrder // - AutoScaleDimensions = new SizeF(10F, 25F); + AutoScaleDimensions = new SizeF(13F, 32F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(785, 314); + ClientSize = new Size(1020, 402); Controls.Add(CardCBox); Controls.Add(ReaderLabel); Controls.Add(BackBtn); Controls.Add(SaveBtn); Controls.Add(groupBox1); - Controls.Add(ReturnDTPicker); + Controls.Add(BorrowDTPicker); Controls.Add(DateLabel); Controls.Add(LibLabel); Controls.Add(LibrarianCBox); + Margin = new Padding(4); Name = "RegOrder"; Text = "RegOrder"; groupBox1.ResumeLayout(false); @@ -175,7 +187,7 @@ private ComboBox LibrarianCBox; private Label LibLabel; private Label DateLabel; - private DateTimePicker ReturnDTPicker; + private DateTimePicker BorrowDTPicker; private GroupBox groupBox1; private DataGridView DataGV; private Button SaveBtn; @@ -183,6 +195,6 @@ private Label ReaderLabel; private ComboBox CardCBox; private DataGridViewComboBoxColumn BookColumnCBox; - private DataGridViewTextBoxColumn AuthorColumn; + private DataGridViewTextBoxColumn NoteColumn; } } \ No newline at end of file diff --git a/LibraryDBproject/LDBproj/AdditionalForms/RegOrder.cs b/LibraryDBproject/LDBproj/AdditionalForms/RegOrder.cs index a2915a7..9520304 100644 --- a/LibraryDBproject/LDBproj/AdditionalForms/RegOrder.cs +++ b/LibraryDBproject/LDBproj/AdditionalForms/RegOrder.cs @@ -1,17 +1,28 @@ -namespace LDBproject.AdditionalForms; +using LDBproject.Entities; +using LDBproject.Repositories; +using LDBproject.Repositories.Implementations; + +namespace LDBproject.AdditionalForms; public partial class RegOrder : Form { - public RegOrder() + private readonly IOrderRep _orderRepository; + + public RegOrder(IOrderRep orderRep, ICustomerCardsRep readerRep, + ILibrarianRep employeeRep, IBookRep bookRep) { InitializeComponent(); + _orderRepository = orderRep ?? throw new ArgumentNullException(nameof(orderRep)); + LibrarianCBox.DataSource = employeeRep.GetCards(); LibrarianCBox.DisplayMember = "FIO"; LibrarianCBox.ValueMember = "CardID"; + CardCBox.DataSource = readerRep.GetCards(); CardCBox.DisplayMember = "FIO"; CardCBox.ValueMember = "CardID"; + BookColumnCBox.DataSource = bookRep.GetBookList(); BookColumnCBox.DisplayMember = "Title"; BookColumnCBox.ValueMember = "BookID"; } @@ -24,27 +35,32 @@ public partial class RegOrder : Form { throw new Exception("[ Blanck space left ]"); } + _orderRepository.CreateOrder(Order.NewOrder(0, (int)CardCBox.SelectedValue, (int)LibrarianCBox.SelectedValue, + CreateBookListFromDG(), BorrowDTPicker.Value)); Close(); } catch (Exception ex) { - MessageBox.Show(ex.Message, "[ Saving error ]", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show(ex.Message, "Order Form [ Error while saving order ]", + MessageBoxButtons.OK, MessageBoxIcon.Error); } } + private List CreateBookListFromDG() + { + List registrations = new List(); + foreach (DataGridViewRow row in DataGV.Rows) + { + if (row.Cells["BookColumnCBox"].Value != null) + { + var bookId = (int)row.Cells["BookColumnCBox"].Value; + var notes = row.Cells["NoteColumn"].Value?.ToString(); + registrations.Add(Registration.OrderReg(0, bookId, notes)); + } + } + return registrations; + } + private void BackBtn_Click(object sender, EventArgs e) => Close(); - - private List CreateBookListFromDG() - { - var list = new List(); - foreach (DataGridViewRow row in DataGV.Rows) - { - if (row.Cells["BookColumn"].Value == null) - { - continue; - } - } - return list; - } } diff --git a/LibraryDBproject/LDBproj/AdditionalForms/RegOrder.resx b/LibraryDBproject/LDBproj/AdditionalForms/RegOrder.resx index ccd5dce..ef527f1 100644 --- a/LibraryDBproject/LDBproj/AdditionalForms/RegOrder.resx +++ b/LibraryDBproject/LDBproj/AdditionalForms/RegOrder.resx @@ -120,7 +120,7 @@ True - + True \ No newline at end of file diff --git a/LibraryDBproject/LDBproj/AdditionalForms/RegOrderF.Designer.cs b/LibraryDBproject/LDBproj/AdditionalForms/RegOrderF.Designer.cs new file mode 100644 index 0000000..484a6f5 --- /dev/null +++ b/LibraryDBproject/LDBproj/AdditionalForms/RegOrderF.Designer.cs @@ -0,0 +1,200 @@ +namespace LDBproject.AdditionalForms +{ + partial class RegOrderF + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + LibrarianCBox = new ComboBox(); + LibLabel = new Label(); + DateLabel = new Label(); + BorrowDTPicker = new DateTimePicker(); + groupBox1 = new GroupBox(); + DataGV = new DataGridView(); + SaveBtn = new Button(); + BackBtn = new Button(); + ReaderLabel = new Label(); + CardCBox = new ComboBox(); + BookColumnCBox = new DataGridViewComboBoxColumn(); + NoteColumn = new DataGridViewTextBoxColumn(); + groupBox1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)DataGV).BeginInit(); + SuspendLayout(); + // + // LibrarianCBox + // + LibrarianCBox.FormattingEnabled = true; + LibrarianCBox.Location = new Point(155, 44); + LibrarianCBox.Margin = new Padding(4); + LibrarianCBox.Name = "LibrarianCBox"; + LibrarianCBox.Size = new Size(235, 40); + LibrarianCBox.TabIndex = 0; + // + // LibLabel + // + LibLabel.AutoSize = true; + LibLabel.Location = new Point(39, 47); + LibLabel.Margin = new Padding(4, 0, 4, 0); + LibLabel.Name = "LibLabel"; + LibLabel.Size = new Size(110, 32); + LibLabel.TabIndex = 1; + LibLabel.Text = "Librarian:"; + // + // DateLabel + // + DateLabel.AutoSize = true; + DateLabel.Location = new Point(39, 164); + DateLabel.Margin = new Padding(4, 0, 4, 0); + DateLabel.Name = "DateLabel"; + DateLabel.Size = new Size(148, 32); + DateLabel.TabIndex = 2; + DateLabel.Text = "Borrow date:"; + // + // BorrowDTPicker + // + BorrowDTPicker.Location = new Point(48, 209); + BorrowDTPicker.Margin = new Padding(4); + BorrowDTPicker.Name = "BorrowDTPicker"; + BorrowDTPicker.Size = new Size(342, 39); + BorrowDTPicker.TabIndex = 3; + // + // groupBox1 + // + groupBox1.Controls.Add(DataGV); + groupBox1.Location = new Point(434, 31); + groupBox1.Margin = new Padding(4); + groupBox1.Name = "groupBox1"; + groupBox1.Padding = new Padding(4); + groupBox1.Size = new Size(550, 340); + groupBox1.TabIndex = 4; + groupBox1.TabStop = false; + groupBox1.Text = "BookListGBox"; + // + // DataGV + // + DataGV.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + DataGV.Columns.AddRange(new DataGridViewColumn[] { BookColumnCBox, NoteColumn }); + DataGV.Location = new Point(8, 52); + DataGV.Margin = new Padding(4); + DataGV.Name = "DataGV"; + DataGV.RowHeadersWidth = 62; + DataGV.Size = new Size(534, 264); + DataGV.TabIndex = 0; + // + // SaveBtn + // + SaveBtn.Location = new Point(48, 276); + SaveBtn.Margin = new Padding(4); + SaveBtn.Name = "SaveBtn"; + SaveBtn.Size = new Size(343, 44); + SaveBtn.TabIndex = 5; + SaveBtn.Text = "Save and give out"; + SaveBtn.UseVisualStyleBackColor = true; + SaveBtn.Click += SaveBtn_Click; + // + // BackBtn + // + BackBtn.Location = new Point(48, 328); + BackBtn.Margin = new Padding(4); + BackBtn.Name = "BackBtn"; + BackBtn.Size = new Size(343, 44); + BackBtn.TabIndex = 6; + BackBtn.Text = "Go back"; + BackBtn.UseVisualStyleBackColor = true; + BackBtn.Click += BackBtn_Click; + // + // ReaderLabel + // + ReaderLabel.AutoSize = true; + ReaderLabel.Location = new Point(39, 90); + ReaderLabel.Margin = new Padding(4, 0, 4, 0); + ReaderLabel.Name = "ReaderLabel"; + ReaderLabel.Size = new Size(87, 64); + ReaderLabel.TabIndex = 7; + ReaderLabel.Text = "Reader\r\ncard:"; + // + // CardCBox + // + CardCBox.FormattingEnabled = true; + CardCBox.Location = new Point(155, 105); + CardCBox.Margin = new Padding(4); + CardCBox.Name = "CardCBox"; + CardCBox.Size = new Size(235, 40); + CardCBox.TabIndex = 8; + // + // BookColumnCBox + // + BookColumnCBox.HeaderText = "Book's title"; + BookColumnCBox.MinimumWidth = 8; + BookColumnCBox.Name = "BookColumnCBox"; + BookColumnCBox.Width = 150; + // + // NoteColumn + // + NoteColumn.HeaderText = "Note"; + NoteColumn.MinimumWidth = 8; + NoteColumn.Name = "NoteColumn"; + NoteColumn.Width = 150; + // + // RegOrder + // + AutoScaleDimensions = new SizeF(13F, 32F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1020, 402); + Controls.Add(CardCBox); + Controls.Add(ReaderLabel); + Controls.Add(BackBtn); + Controls.Add(SaveBtn); + Controls.Add(groupBox1); + Controls.Add(BorrowDTPicker); + Controls.Add(DateLabel); + Controls.Add(LibLabel); + Controls.Add(LibrarianCBox); + Margin = new Padding(4); + Name = "RegOrder"; + Text = "RegOrder"; + groupBox1.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)DataGV).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private ComboBox LibrarianCBox; + private Label LibLabel; + private Label DateLabel; + private DateTimePicker BorrowDTPicker; + private GroupBox groupBox1; + private DataGridView DataGV; + private Button SaveBtn; + private Button BackBtn; + private Label ReaderLabel; + private ComboBox CardCBox; + private DataGridViewComboBoxColumn BookColumnCBox; + private DataGridViewTextBoxColumn NoteColumn; + } +} \ No newline at end of file diff --git a/LibraryDBproject/LDBproj/AdditionalForms/RegOrderF.cs b/LibraryDBproject/LDBproj/AdditionalForms/RegOrderF.cs new file mode 100644 index 0000000..a0bc9b5 --- /dev/null +++ b/LibraryDBproject/LDBproj/AdditionalForms/RegOrderF.cs @@ -0,0 +1,66 @@ +using LDBproject.Entities; +using LDBproject.Repositories; +using LDBproject.Repositories.Implementations; + +namespace LDBproject.AdditionalForms; + +public partial class RegOrderF : Form +{ + private readonly IOrderRep _orderRepository; + + public RegOrderF(IOrderRep orderRep, ICustomerCardsRep readerRep, + ILibrarianRep employeeRep, IBookRep bookRep) + { + InitializeComponent(); + _orderRepository = orderRep ?? throw new ArgumentNullException(nameof(orderRep)); + + LibrarianCBox.DataSource = employeeRep.GetCards(); + LibrarianCBox.DisplayMember = "FIO"; + LibrarianCBox.ValueMember = "CardID"; + + CardCBox.DataSource = readerRep.GetCards(); + CardCBox.DisplayMember = "FIO"; + CardCBox.ValueMember = "CardID"; + + BookColumnCBox.DataSource = bookRep.GetBookList(); + BookColumnCBox.DisplayMember = "Title"; + BookColumnCBox.ValueMember = "BookID"; + } + + private void SaveBtn_Click(object sender, EventArgs e) + { + try + { + if (DataGV.RowCount < 1 || LibrarianCBox.SelectedIndex < 0) + { + throw new Exception("[ Blanck space left ]"); + } + _orderRepository.CreateOrder(Order.NewOrder(0, (int)CardCBox.SelectedValue, (int)LibrarianCBox.SelectedValue, + CreateBookListFromDG(), BorrowDTPicker.Value)); + + Close(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Order Form [ Error while saving order ]", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private List CreateBookListFromDG() + { + List registrations = new List(); + foreach (DataGridViewRow row in DataGV.Rows) + { + if (row.Cells["BookColumnCBox"].Value != null) + { + var bookId = (int)row.Cells["BookColumnCBox"].Value; + var notes = row.Cells["NoteColumn"].Value?.ToString(); + registrations.Add(Registration.OrderReg(0, bookId, notes)); + } + } + return registrations; + } + + private void BackBtn_Click(object sender, EventArgs e) => Close(); +} diff --git a/LibraryDBproject/LDBproj/AdditionalForms/RegOrderF.resx b/LibraryDBproject/LDBproj/AdditionalForms/RegOrderF.resx new file mode 100644 index 0000000..ef527f1 --- /dev/null +++ b/LibraryDBproject/LDBproj/AdditionalForms/RegOrderF.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + True + + + True + + \ No newline at end of file diff --git a/LibraryDBproject/LDBproj/AdditionalForms/UpdateF.cs b/LibraryDBproject/LDBproj/AdditionalForms/UpdateF.cs index 42d289d..71fe140 100644 --- a/LibraryDBproject/LDBproj/AdditionalForms/UpdateF.cs +++ b/LibraryDBproject/LDBproj/AdditionalForms/UpdateF.cs @@ -1,14 +1,21 @@ -namespace LDBproject.AdditionalForms; +using LDBproject.Entities; +using LDBproject.Repositories; + +namespace LDBproject.AdditionalForms; public partial class UpdateF : Form { - public UpdateF() + private readonly IUpdateRep _updRep; + + public UpdateF(IUpdateRep updRep, ILibrarianRep libRep, ICustomerCardsRep customersRep) { InitializeComponent(); + _updRep = updRep ?? throw new ArgumentNullException(nameof(updRep)); + LibrarianCBox.DataSource = libRep.GetCards(); LibrarianCBox.DisplayMember = "FIO"; LibrarianCBox.ValueMember = "CardID"; - + CardCBox.DataSource = customersRep.GetCards(); CardCBox.DisplayMember = "FIO"; CardCBox.DisplayMember = "CardID"; CardCBox.ValueMember = "CardID"; @@ -22,7 +29,8 @@ public partial class UpdateF : Form { throw new Exception("[ Blanck space left ]"); } - + _updRep.AddUpdate(UpdateC.CustomerUpd(CardCBox.SelectedIndex, LibrarianCBox.SelectedIndex, + UpdDTPicker.Value, NextUpdDTPicker.Value, NoteTb.Text)); Close(); } catch (Exception ex) diff --git a/LibraryDBproject/LDBproj/AdditionalForms/UpdatesListF.cs b/LibraryDBproject/LDBproj/AdditionalForms/UpdatesListF.cs index 3ed4f3d..6659cde 100644 --- a/LibraryDBproject/LDBproj/AdditionalForms/UpdatesListF.cs +++ b/LibraryDBproject/LDBproj/AdditionalForms/UpdatesListF.cs @@ -1,14 +1,17 @@ -using Unity; +using LDBproject.Repositories; +using Unity; namespace LDBproject.AdditionalForms; public partial class UpdatesListF : Form { private readonly IUnityContainer _container; + private readonly IUpdateRep _updR; - public UpdatesListF(IUnityContainer container) + public UpdatesListF(IUnityContainer container, IUpdateRep updR) { InitializeComponent(); + _updR = updR ?? throw new ArgumentNullException(nameof(updR)); _container = container ?? throw new ArgumentNullException(nameof(container)); } @@ -51,6 +54,7 @@ public partial class UpdatesListF : Form try { + _updR.RemoveUpd(foundID); ReloadList(); } catch (Exception ex) @@ -59,7 +63,8 @@ public partial class UpdatesListF : Form } } - private void ReloadList() { } + private void ReloadList() => + DataGV.DataSource = _updR.GetUpdateList(); private bool GetIDFromRow(out int id) { diff --git a/LibraryDBproject/LDBproj/Entities/Order.cs b/LibraryDBproject/LDBproj/Entities/Order.cs index 6f67ebc..0c25bdd 100644 --- a/LibraryDBproject/LDBproj/Entities/Order.cs +++ b/LibraryDBproject/LDBproj/Entities/Order.cs @@ -6,11 +6,11 @@ public class Order public int CardID { get; private set; } - public DateTime BorrowDate { get; private set; } - public int LibrarianID { get; private set; } - public IEnumerable BookList { get; set; } = []; + public DateTime BorrowDate { get; private set; } + + public IEnumerable Registrations { get; set; } = []; public static Order NewOrder( int orderIndex, int ticketIndex, int librarian, IEnumerable list, DateTime borrow) @@ -20,7 +20,7 @@ public class Order OrderID = orderIndex, CardID = ticketIndex, LibrarianID = librarian, - BookList = list, + Registrations = list, BorrowDate = borrow }; } diff --git a/LibraryDBproject/LDBproj/Entities/Registration.cs b/LibraryDBproject/LDBproj/Entities/Registration.cs index 691d8e9..6890f15 100644 --- a/LibraryDBproject/LDBproj/Entities/Registration.cs +++ b/LibraryDBproject/LDBproj/Entities/Registration.cs @@ -11,7 +11,7 @@ public class Registration public string Note { get; private set; } public static Registration OrderReg( - int orderIndex, string notes, int bookIndex) + int orderIndex, int bookIndex, string notes) { return new Registration { diff --git a/LibraryDBproject/LDBproj/LDBproject.csproj b/LibraryDBproject/LDBproj/LDBproject.csproj index d013ccd..8508f1d 100644 --- a/LibraryDBproject/LDBproj/LDBproject.csproj +++ b/LibraryDBproject/LDBproj/LDBproject.csproj @@ -9,12 +9,20 @@ - - - - - + + + + + + + + + + + + + \ No newline at end of file diff --git a/LibraryDBproject/LDBproj/Program.cs b/LibraryDBproject/LDBproj/Program.cs index 87ae676..6cf5bda 100644 --- a/LibraryDBproject/LDBproj/Program.cs +++ b/LibraryDBproject/LDBproj/Program.cs @@ -1,6 +1,10 @@ using LDBproject.Repositories.Implementations; using LDBproject.Repositories; using Unity; +using Unity.Microsoft.Logging; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Serilog; namespace LDBproject @@ -20,13 +24,29 @@ namespace LDBproject { var container = new UnityContainer(); + container.AddExtension(new LoggingExtension(CreateLoggerFactory())); + container.RegisterType(); container.RegisterType(); container.RegisterType(); container.RegisterType(); container.RegisterType(); + container.RegisterType(); + return container; } + + private static LoggerFactory CreateLoggerFactory() + { + var loggerFactory = new LoggerFactory(); + loggerFactory.AddSerilog(new LoggerConfiguration() + .ReadFrom.Configuration(new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json") + .Build()) + .CreateLogger()); + return loggerFactory; + } } } \ No newline at end of file diff --git a/LibraryDBproject/LDBproj/Repositories/IConnectionString.cs b/LibraryDBproject/LDBproj/Repositories/IConnectionString.cs new file mode 100644 index 0000000..7df12aa --- /dev/null +++ b/LibraryDBproject/LDBproj/Repositories/IConnectionString.cs @@ -0,0 +1,6 @@ +namespace LDBproject.Repositories; + +public interface IConnectionString +{ + public string ConnectionString { get; } +} diff --git a/LibraryDBproject/LDBproj/Repositories/IOrderRep.cs b/LibraryDBproject/LDBproj/Repositories/IOrderRep.cs index ee981f2..cde2b59 100644 --- a/LibraryDBproject/LDBproj/Repositories/IOrderRep.cs +++ b/LibraryDBproject/LDBproj/Repositories/IOrderRep.cs @@ -9,5 +9,7 @@ public interface IOrderRep void CreateOrder(Order order); + void UpdateOrderInfo(Order order); + void DeleteOrderinfo(int orderID); } diff --git a/LibraryDBproject/LDBproj/Repositories/Implementations/BookR.cs b/LibraryDBproject/LDBproj/Repositories/Implementations/BookR.cs index 3b3d81a..b831172 100644 --- a/LibraryDBproject/LDBproj/Repositories/Implementations/BookR.cs +++ b/LibraryDBproject/LDBproj/Repositories/Implementations/BookR.cs @@ -1,33 +1,115 @@ -using LDBproject.Entities; +using Microsoft.Extensions.Logging; +using LDBproject.Entities; +using Newtonsoft.Json; +using Npgsql; +using Dapper; namespace LDBproject.Repositories.Implementations; public class BookR : IBookRep { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; - public BookR() + public BookR(IConnectionString connectionString, ILogger logger) { + _connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public Book GetBookByID(int id) { - return null; + _logger.LogInformation("< Getting BOOK by id >"); + _logger.LogDebug("Object ID: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT b.*, GenreMask FROM Books b WHERE b.BookID = @BookID"; + return connection.QueryFirstOrDefault(querySelect, new { BookID = id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while reading :') BOOK by ID >"); + throw; + } } public void AddBook(Book book) { + _logger.LogInformation("< New BOOK Added >"); + _logger.LogDebug("Object: {json}", JsonConvert.SerializeObject(book)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var queryInsert = @"INSERT INTO Books (Title, Author, PublishYear, Status, GenreMask) + VALUES (@Title, @Author, @PublishYear, @Status, @GenreMask)"; + connection.Execute(queryInsert, book); + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while adding BOOK >"); + throw; + } } public void UpdateBook(Book book) { + _logger.LogInformation("< BOOK Info Updated >"); + _logger.LogDebug("Object: {json}", JsonConvert.SerializeObject(book)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var queryUpdate = @"UPDATE Books SET Title = @Title, Author = @Author, + PublishYear = @PublishYear, Status = @Status, GenreMask = @GenreMask + WHERE BookID = @BookID"; + connection.Execute(queryUpdate, book); + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while updating BOOK >"); + throw; + } } public void DeleteBook(int id) { + _logger.LogInformation("< Removing BOOK >"); + _logger.LogDebug("Object ID: {id}", id); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @"DELETE FROM Books WHERE BookID = @BookID"; + connection.Execute(queryDelete, new { BookID = id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while deleting BOOK >"); + throw; + } } public IEnumerable GetBookList() { - return []; + _logger.LogInformation("< Getting all BOOKS >"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var querySelectAll = @"SELECT b.*, GenreMask FROM Books b"; + var books = connection.Query(querySelectAll); + + _logger.LogDebug("Aimed objects: {json}", JsonConvert.SerializeObject(books)); + return books; + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while getting BOOKS >"); + throw; + } } } diff --git a/LibraryDBproject/LDBproj/Repositories/Implementations/ConnectionStrR.cs b/LibraryDBproject/LDBproj/Repositories/Implementations/ConnectionStrR.cs new file mode 100644 index 0000000..936e8e2 --- /dev/null +++ b/LibraryDBproject/LDBproj/Repositories/Implementations/ConnectionStrR.cs @@ -0,0 +1,7 @@ +namespace LDBproject.Repositories.Implementations +{ + internal class ConnectionStrR : IConnectionString + { + public string ConnectionString => "Server=127.0.0.7;Port=5472;Database=libraryDB;Uid=Del8a;Pwd=del8almond;"; + } +} \ No newline at end of file diff --git a/LibraryDBproject/LDBproj/Repositories/Implementations/CustomerCardR.cs b/LibraryDBproject/LDBproj/Repositories/Implementations/CustomerCardR.cs index c2c17d5..9e27d0f 100644 --- a/LibraryDBproject/LDBproj/Repositories/Implementations/CustomerCardR.cs +++ b/LibraryDBproject/LDBproj/Repositories/Implementations/CustomerCardR.cs @@ -1,28 +1,109 @@ -using LDBproject.Entities; +using Microsoft.Extensions.Logging; +using LDBproject.Entities; +using Newtonsoft.Json; +using Npgsql; +using Dapper; + namespace LDBproject.Repositories.Implementations; public class CustomerCardR : ICustomerCardsRep { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public CustomerCardR(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + } + public CustomerCard GetCardByID(int id) { - return null; + _logger.LogInformation("< Getting CARD by id >"); + _logger.LogDebug("Object ID: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT * FROM CustomerCards WHERE CardID = @CardID"; // Assumes you have a CardID column + return connection.QueryFirstOrDefault(querySelect, new { CardID = id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while reading CARD by ID >"); + throw; + } } public void AddCard(CustomerCard card) { + _logger.LogInformation("< New (reader)CARD Added >"); + _logger.LogDebug("Object: {json}", JsonConvert.SerializeObject(card)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var queryInsert = @"INSERT INTO CustomerCards (FIO, AgeBirthday) VALUES (@FIO, @AgeBirthday)"; + connection.Execute(queryInsert, card); + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while adding CARD >"); + throw; + } } public void UpdateCard(CustomerCard card) { + _logger.LogInformation("< CARD Info Updated >"); + _logger.LogDebug("Object: {json}", JsonConvert.SerializeObject(card)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var queryUpdate = @"UPDATE CustomerCards SET FIO = @FIO, AgeBirthday = @AgeBirthday WHERE CardID = @CardID"; // Assumes you have a CardID column + connection.Execute(queryUpdate, card); + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while updating CARD >"); + throw; + } } public void DeleteCard(int id) { + _logger.LogInformation("< Removing CARD >"); + _logger.LogDebug("Object ID: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @"DELETE FROM CustomerCards WHERE CardID = @CardID"; // Assumes you have a CardID column + connection.Execute(queryDelete, new { CardID = id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while deleting CARD >"); + throw; + } } public IEnumerable GetCards() { - return []; + _logger.LogInformation("< Getting all CARDS >"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var querySelectAll = "SELECT * FROM CustomerCards"; + var cards = connection.Query(querySelectAll); + _logger.LogDebug("Aimed objects: {json}", JsonConvert.SerializeObject(cards)); + return cards; + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while getting CARDS >"); + throw; + } } } \ No newline at end of file diff --git a/LibraryDBproject/LDBproj/Repositories/Implementations/LibrarianR.cs b/LibraryDBproject/LDBproj/Repositories/Implementations/LibrarianR.cs index bb7c0de..f9ecb95 100644 --- a/LibraryDBproject/LDBproj/Repositories/Implementations/LibrarianR.cs +++ b/LibraryDBproject/LDBproj/Repositories/Implementations/LibrarianR.cs @@ -1,32 +1,108 @@ -using LDBproject.Entities; +using Microsoft.Extensions.Logging; +using LDBproject.Entities; +using Newtonsoft.Json; +using Npgsql; +using Dapper; namespace LDBproject.Repositories.Implementations; internal class LibrarianR : ILibrarianRep { - public LibrarianR() + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public LibrarianR(IConnectionString connectionString, ILogger logger) { + _connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public LibrarianCard GetCardByID(int id) { - return null; + _logger.LogInformation("< Getting EMPLOYEE by id >"); + _logger.LogDebug("Object ID: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT * FROM LibrarianCards WHERE CardID = @CardID"; // Assumes you have a CardID column + return connection.QueryFirstOrDefault(querySelect, new { CardID = id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while getting EMPLOYEE by ID >"); + throw; + } } public void AddCard(LibrarianCard card) { + _logger.LogInformation("< New EMPLOYEE Added >"); + _logger.LogDebug("Object: {json}", JsonConvert.SerializeObject(card)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var queryInsert = @"INSERT INTO LibrarianCards (FIO, GenreMask) VALUES (@FIO, @GenreMask)"; + connection.Execute(queryInsert, card); + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while adding EMPLOYEE >"); + throw; + } } public void ChangeCardInfo(LibrarianCard card) { + _logger.LogInformation("< EMPLOYEE Info Updated >"); + _logger.LogDebug("Object: {json}", JsonConvert.SerializeObject(card)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var queryUpdate = @"UPDATE LibrarianCards SET FIO = @FIO, GenreMask = @GenreMask WHERE CardID = @CardID"; // Assumes you have a CardID column + connection.Execute(queryUpdate, card); + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while updating EMPLOYEE info >"); + throw; + } } public void DeleteCard(int id) { + _logger.LogInformation("< Removing CARD >"); + _logger.LogDebug("Object ID: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @"DELETE FROM LibrarianCards WHERE CardID = @CardID"; // Assumes you have a CardID column + connection.Execute(queryDelete, new { CardID = id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while deleting EMPLOYEE >"); + throw; + } } public IEnumerable GetCards() { - return []; + _logger.LogInformation("< Getting all EMPLOYEE >"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var querySelectAll = "SELECT * FROM LibrarianCards"; + var cards = connection.Query(querySelectAll); + _logger.LogDebug("Aimed objects: {json}", JsonConvert.SerializeObject(cards)); + return cards; + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while getting EMPLOYEE >"); + throw; + } } } diff --git a/LibraryDBproject/LDBproj/Repositories/Implementations/OrderR.cs b/LibraryDBproject/LDBproj/Repositories/Implementations/OrderR.cs index 7a2dd0a..f963b72 100644 --- a/LibraryDBproject/LDBproj/Repositories/Implementations/OrderR.cs +++ b/LibraryDBproject/LDBproj/Repositories/Implementations/OrderR.cs @@ -1,15 +1,156 @@ -using LDBproject.Entities; +using Microsoft.Extensions.Logging; +using LDBproject.Entities; +using Newtonsoft.Json; +using Npgsql; +using Dapper; +using System.Data.SqlClient; namespace LDBproject.Repositories.Implementations; public class OrderR : IOrderRep { - public void CreateOrder(Order order) {} + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; - public void DeleteOrderinfo(int orderID) {} - - public IEnumerable GetOrdersInfo(int? librarianID = null, int? orderID = null, int? customerID = null) + public OrderR(IConnectionString connectionString, ILogger logger) { - return []; + _connectionString = connectionString; + _logger = logger; + } + + public void CreateOrder(Order order) + { + _logger.LogInformation("< Adding new ORDER > [!]"); + _logger.LogDebug("Object - {json}", JsonConvert.SerializeObject(order)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); + + // 1. Insert the Order without RETURNING + var queryInsert = @"INSERT INTO Orders (CardID, LibrarianID, BorrowDate) VALUES (@CardID, @LibrarianID, @BorrowDate)"; + + connection.Execute(queryInsert, new + { + order.CardID, + order.LibrarianID, + order.BorrowDate + }, transaction); + + // 2. Get the last inserted OrderID + var queryGetLastInsertedId = "SELECT MAX(OrderID) FROM Orders"; + + int orderID = connection.QuerySingle(queryGetLastInsertedId, transaction: transaction); + + + // 3. Insert the Registrations associated with the order + var querySubInsert = @"INSERT INTO Registrations (OrderID, BookID, Note) VALUES (@OrderID, @BookID, @Note)"; + foreach (var elem in order.Registrations) + { + connection.Execute(querySubInsert, new { OrderID = orderID, elem.BookID, elem.Note }, transaction); + } + transaction.Commit(); + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while adding ORDER >"); + throw; + } + } + + public void UpdateOrderInfo(Order order) + { + _logger.LogInformation("< Updating order info >"); + _logger.LogDebug("Object - {json}", JsonConvert.SerializeObject(order)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + using var transaction = connection.BeginTransaction(); + // 1. Update order + var queryUpdate = @" + UPDATE Orders SET + CardID = @CardID, + LibrarianID = @LibrarianID, + BorrowDate = @BorrowDate + WHERE OrderID = @OrderID"; + + connection.Execute(queryUpdate, new + { + order.CardID, + order.LibrarianID, + order.BorrowDate, + order.OrderID + }, transaction); + + //2. Update registrations: + var queryDeleteRegistrations = "DELETE FROM Registrations WHERE OrderID = @OrderID"; + connection.Execute(queryDeleteRegistrations, new { OrderID = order.OrderID }, transaction); + + var querySubInsert = @"INSERT INTO Registrations (OrderID, BookID, Note) VALUES (@OrderID, @BookID, @Note)"; + foreach (var elem in order.Registrations) + { + connection.Execute(querySubInsert, new { OrderID = order.OrderID, elem.BookID, elem.Note }, transaction); + } + + transaction.Commit(); + + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while updating order info >"); + throw; + } + } + + public void DeleteOrderinfo(int orderID) + { + _logger.LogInformation("< Deleting exact order >"); + _logger.LogDebug("Obj: {id}", orderID); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); + + // 1. Delete registrations for the order + var queryDeleteRegistrations = @"DELETE FROM Registrations WHERE OrderID=@OrderID"; + connection.Execute(queryDeleteRegistrations, new { OrderID = orderID }, transaction); + // 2. Delete the order itself + var queryDel = @"DELETE FROM Orders WHERE OrderID=@OrderID"; + connection.Execute(queryDel, new { orderID }, transaction); + + transaction.Commit(); + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while deleting ORDER >"); + throw; + } + } + + // ----------------------------- [ REVIEWED (waits for further editing) ] ------------------------------- + + public IEnumerable GetOrdersInfo(int? orderID = null, int ? librarianID = null, int? customerID = null) + { + _logger.LogInformation("< Getting all ORDERS >"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + var querySelectAll = @"SELECT * FROM Orders"; + var orders = connection.Query(querySelectAll); + + _logger.LogDebug("Aimed objects: {json}", JsonConvert.SerializeObject(orders)); + + return orders; + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while reading ORDERS >"); + throw; + } } } diff --git a/LibraryDBproject/LDBproj/Repositories/Implementations/UpdateR.cs b/LibraryDBproject/LDBproj/Repositories/Implementations/UpdateR.cs index 59b60db..e13f4ec 100644 --- a/LibraryDBproject/LDBproj/Repositories/Implementations/UpdateR.cs +++ b/LibraryDBproject/LDBproj/Repositories/Implementations/UpdateR.cs @@ -1,28 +1,90 @@ -using LDBproject.Entities; +using Microsoft.Extensions.Logging; +using LDBproject.Entities; +using Newtonsoft.Json; +using Npgsql; +using Dapper; namespace LDBproject.Repositories.Implementations; public class UpdateR : IUpdateRep { - public UpdateR() + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public UpdateR(IConnectionString connectionString, ILogger logger) { + _connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public UpdateC GetUpdateByID(int id) { - return null; + _logger.LogInformation("< Getting UPDATE by id >"); + _logger.LogDebug("Object ID: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT * FROM Updates WHERE ID = @ID"; + return connection.QueryFirstOrDefault(querySelect, new { ID = id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while getting UPDATE by id >"); + throw; + } } public void AddUpdate(UpdateC update) { + _logger.LogInformation("< New card UPDATE added >"); + _logger.LogDebug("Object: {json}", JsonConvert.SerializeObject(update)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @"INSERT INTO Updates (CardID, LibrarianID, LastUpdate, UpdBoundary, Note) + VALUES (@CardID, @LibrarianID, @LastUpdate, @UpdBoundary, @Note)"; + connection.Execute(queryInsert, update); + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while adding card UPDATE >"); + throw; + } } public void RemoveUpd(int Id) { + _logger.LogInformation("< Deleting card UPDATE by id >"); + _logger.LogDebug("Object ID: {id}", Id); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @"DELETE FROM Updates WHERE ID = @ID"; + connection.Execute(queryDelete, new { ID = Id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while deleting card UPDATE >"); + throw; + } } public IEnumerable GetUpdateList() { - return []; + _logger.LogInformation("< Getting all UPDATES >"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelectAll = @"SELECT * FROM Updates"; + var upds = connection.Query(querySelectAll); + _logger.LogDebug("Aimed objects: {json}", JsonConvert.SerializeObject(upds)); + return upds; + } + catch (Exception ex) + { + _logger.LogError(ex, "< Error while getting all UPDATES >"); + throw; + } } }