diff --git a/Cop.Borovkov.Var3/Lab3.Database/Repository/Implementations/EducationFormRepository.cs b/Cop.Borovkov.Var3/Lab3.Database/Repository/Implementations/EducationFormRepository.cs index 5bcb5d9..19b88c1 100644 --- a/Cop.Borovkov.Var3/Lab3.Database/Repository/Implementations/EducationFormRepository.cs +++ b/Cop.Borovkov.Var3/Lab3.Database/Repository/Implementations/EducationFormRepository.cs @@ -29,6 +29,7 @@ namespace Lab3.Database.Repository.Implementations Id = Guid.NewGuid(), Name = f, })); + await _context.SaveChangesAsync(); } } } diff --git a/Cop.Borovkov.Var3/Lab3/Extensions/DIExtension.cs b/Cop.Borovkov.Var3/Lab3/Extensions/DIExtension.cs index 11ef788..9199bff 100644 --- a/Cop.Borovkov.Var3/Lab3/Extensions/DIExtension.cs +++ b/Cop.Borovkov.Var3/Lab3/Extensions/DIExtension.cs @@ -25,6 +25,7 @@ namespace Lab3.Extensions this IServiceCollection services) { services.AddScoped(); + services.AddScoped(); services.AddScoped>(sp => (id => new CreateForm( diff --git a/Cop.Borovkov.Var3/Lab3/Forms/CatalogForm.Designer.cs b/Cop.Borovkov.Var3/Lab3/Forms/CatalogForm.Designer.cs new file mode 100644 index 0000000..671312e --- /dev/null +++ b/Cop.Borovkov.Var3/Lab3/Forms/CatalogForm.Designer.cs @@ -0,0 +1,76 @@ +namespace Lab3.Forms +{ + partial class CatalogForm + { + /// + /// 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() + { + Catalog = new DataGridView(); + EducationForm = new DataGridViewTextBoxColumn(); + ((System.ComponentModel.ISupportInitialize)Catalog).BeginInit(); + SuspendLayout(); + // + // Catalog + // + Catalog.AllowUserToAddRows = false; + Catalog.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + Catalog.Columns.AddRange(new DataGridViewColumn[] { EducationForm }); + Catalog.Dock = DockStyle.Fill; + Catalog.Location = new Point(0, 0); + Catalog.Name = "Catalog"; + Catalog.RowHeadersWidth = 51; + Catalog.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + Catalog.Size = new Size(800, 450); + Catalog.TabIndex = 0; + Catalog.CellEndEdit += Catalog_CellEndEditAsync; + Catalog.KeyDown += Catalog_KeyDownAsync; + // + // EducationForm + // + EducationForm.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + EducationForm.HeaderText = "Форма обучения"; + EducationForm.MinimumWidth = 6; + EducationForm.Name = "EducationForm"; + // + // CatalogForm + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(Catalog); + Name = "CatalogForm"; + Text = "CatalogForm"; + Load += CatalogForm_LoadAsync; + ((System.ComponentModel.ISupportInitialize)Catalog).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView Catalog; + private DataGridViewTextBoxColumn EducationForm; + } +} \ No newline at end of file diff --git a/Cop.Borovkov.Var3/Lab3/Forms/CatalogForm.cs b/Cop.Borovkov.Var3/Lab3/Forms/CatalogForm.cs new file mode 100644 index 0000000..03cbaa6 --- /dev/null +++ b/Cop.Borovkov.Var3/Lab3/Forms/CatalogForm.cs @@ -0,0 +1,97 @@ +using Lab3.Database.Repository.Interfaces; + +namespace Lab3.Forms +{ + public partial class CatalogForm : Form + { + private readonly IEducationFormRepository _repository; + + public CatalogForm(IEducationFormRepository educationFormRepository) + { + _repository = educationFormRepository; + InitializeComponent(); + } + + private async void CatalogForm_LoadAsync(object sender, EventArgs e) + { + Catalog.Rows.Clear(); + + var values = (await _repository.Get()).ToList(); + for (int i = 0; i < values.Count; i++) + { + Catalog.Rows.Add(); + Catalog.Rows[i].Cells[0].Value = values[i]; + } + } + + private async void Catalog_CellEndEditAsync(object sender, DataGridViewCellEventArgs e) + { + await LoadAsync(); + } + + private async Task LoadAsync() + { + try + { + List values = new List(); + + for (int i = 0; i < Catalog.Rows.Count; ++i) + { + string? val = (string?)Catalog.Rows[i].Cells[0].Value; + if (string.IsNullOrEmpty(val)) + { + MessageBox.Show( + "Неверные данные", + "Ошибка", + MessageBoxButtons.OK, + MessageBoxIcon.Error); + return; + } + values.Add(val); + } + + await _repository.Update(values); + } + catch (Exception ex) + { + MessageBox.Show( + ex.Message, + "Ошибка", + MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + + private async void Catalog_KeyDownAsync(object sender, KeyEventArgs e) + { + if (e.KeyCode == Keys.Insert) + { + Catalog.Rows.Add(); + } + + if (e.KeyCode == Keys.Delete && Catalog.SelectedRows.Count == 1) + { + if (MessageBox.Show( + "Удалить?", + "Удаление", + MessageBoxButtons.YesNo, + MessageBoxIcon.Question) == DialogResult.Yes) + { + try + { + Catalog.Rows.RemoveAt(Catalog.SelectedRows[0].Index); + await LoadAsync(); + } + catch (Exception ex) + { + MessageBox.Show( + ex.Message, + "Ошибка", + MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + } + } +} diff --git a/Cop.Borovkov.Var3/Lab3/Forms/CatalogForm.resx b/Cop.Borovkov.Var3/Lab3/Forms/CatalogForm.resx new file mode 100644 index 0000000..f789fea --- /dev/null +++ b/Cop.Borovkov.Var3/Lab3/Forms/CatalogForm.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + \ No newline at end of file diff --git a/Cop.Borovkov.Var3/Lab3/Forms/MainForm.Designer.cs b/Cop.Borovkov.Var3/Lab3/Forms/MainForm.Designer.cs index 0e30590..383fc4f 100644 --- a/Cop.Borovkov.Var3/Lab3/Forms/MainForm.Designer.cs +++ b/Cop.Borovkov.Var3/Lab3/Forms/MainForm.Designer.cs @@ -28,16 +28,15 @@ /// private void InitializeComponent() { - StudentsListBox = new ComponentsLibrary.ListBoxValues(); + StudentsListBox = new Cop.Borovkov.Var3.Components.CustomListBox(); SuspendLayout(); // // StudentsListBox // StudentsListBox.Dock = DockStyle.Fill; StudentsListBox.Location = new Point(0, 0); - StudentsListBox.Margin = new Padding(3, 4, 3, 4); StudentsListBox.Name = "StudentsListBox"; - StudentsListBox.SelectedIndex = -1; + StudentsListBox.Selected = ""; StudentsListBox.Size = new Size(800, 450); StudentsListBox.TabIndex = 0; // @@ -50,11 +49,12 @@ Name = "MainForm"; Text = "MainForm"; Load += MainForm_LoadAsync; + KeyDown += MainForm_KeyDown; ResumeLayout(false); } #endregion - private ComponentsLibrary.ListBoxValues StudentsListBox; + private Cop.Borovkov.Var3.Components.CustomListBox StudentsListBox; } } \ No newline at end of file diff --git a/Cop.Borovkov.Var3/Lab3/Forms/MainForm.cs b/Cop.Borovkov.Var3/Lab3/Forms/MainForm.cs index 5a84939..d6754c4 100644 --- a/Cop.Borovkov.Var3/Lab3/Forms/MainForm.cs +++ b/Cop.Borovkov.Var3/Lab3/Forms/MainForm.cs @@ -8,20 +8,57 @@ namespace Lab3.Forms { private readonly IStudentRepository _studentRepository; private readonly IMapper _mapper; + private readonly Func _getCreateOrUpdateForm; public MainForm( IStudentRepository repository, - IMapper mapper) + IMapper mapper, + Func getCreateOrUpdateForm) { - _studentRepository = repository; + _studentRepository = repository; _mapper = mapper; + + _getCreateOrUpdateForm = getCreateOrUpdateForm; + InitializeComponent(); } private async void MainForm_LoadAsync(object sender, EventArgs e) { var students = _mapper.Map>(await _studentRepository.GetAsync()); - StudentsListBox.FillListBox(students); + StudentsListBox.FillValues( + students.Select(s => string.Join(" ", + [ + s.Id, + s.Name, + s.EducationForm, + s.StartEducation.ToLongDateString(), + s.SessionMarks, + ] + )) + ); + } + + private void MainForm_KeyDown(object sender, KeyEventArgs e) + { + if (e.Modifiers != Keys.Control) + { + return; + } + + switch (e.KeyCode) + { + case Keys.A: + _getCreateOrUpdateForm(null).Show(this); + break; + case Keys.U: + _getCreateOrUpdateForm( + Guid.Parse(StudentsListBox.Selected.Split()[0]) + ).Show(this); + break; + default: + return; + } } } } diff --git a/Cop.Borovkov.Var3/Lab3/Models/StudentViewModel.cs b/Cop.Borovkov.Var3/Lab3/Models/StudentViewModel.cs index 54e9145..9e9ca4a 100644 --- a/Cop.Borovkov.Var3/Lab3/Models/StudentViewModel.cs +++ b/Cop.Borovkov.Var3/Lab3/Models/StudentViewModel.cs @@ -6,10 +6,7 @@ namespace Lab3.Models public record StudentViewModel : StudentDTO { public string SessionMarks => string.Join("; ", StudentSessions - .Select(s => string.Format( - CultureInfo.InvariantCulture, - "сессия{}: {0:f2}", - s.Number, - s.Score))); + .OrderBy(s => s.Number) + .Select(s => $"сессия{s.Number:d}: {s.Score:n2}")); } } diff --git a/Cop.Borovkov.Var3/Lab3/Program.cs b/Cop.Borovkov.Var3/Lab3/Program.cs index 2ac9d73..ff96b66 100644 --- a/Cop.Borovkov.Var3/Lab3/Program.cs +++ b/Cop.Borovkov.Var3/Lab3/Program.cs @@ -1,4 +1,3 @@ -using Lab3.Database.Extensions; using Lab3.Extensions; using Lab3.Forms; using Microsoft.Extensions.Configuration; @@ -21,7 +20,7 @@ namespace Lab3 var app = CreateHostBuilder().Build(); - Application.Run(app.Services.GetRequiredService()); + Application.Run(app.Services.GetRequiredService()); } static IHostBuilder CreateHostBuilder()