Добавление записи работает

This commit is contained in:
bekodeg 2024-11-07 13:41:24 +04:00
parent 8563387642
commit 2c0e6b13f5
10 changed files with 263 additions and 150 deletions

View File

@ -2,8 +2,6 @@
{
public record StudentSessionDTO
{
public Guid Id { get; init; }
public decimal Score { get; init; }
public int Number { get; init; }

View File

@ -9,7 +9,8 @@ namespace Lab3.Database.MappingProfiles
public SessionMappingProfile()
{
_ = CreateMap<StudentSession, StudentSessionDTO>();
_ = CreateMap<StudentSessionDTO, StudentSession>();
_ = CreateMap<StudentSessionDTO, StudentSession>()
.ForMember(s => s.Id, opt => opt.MapFrom(s => Guid.NewGuid()));
}
}
}

View File

@ -1,6 +1,7 @@
using AutoMapper;
using Lab3.Database.Context;
using Lab3.Database.DTO;
using Lab3.Database.Models;
using Lab3.Database.Repository.Interfaces;
using Microsoft.EntityFrameworkCore;
@ -18,10 +19,19 @@ namespace Lab3.Database.Repository.Implementations
_context = context;
}
public Task<List<StudentDTO>> Create(StudentDTO studentDTO) => throw new NotImplementedException();
public Task<StudentDTO> Delete(Guid id) => throw new NotImplementedException();
public async Task<StudentDTO> CreateAsync(StudentDTO studentDTO)
{
var student = _mapper.Map<Student>(studentDTO);
public async Task<List<StudentDTO>> Get(int limit = 10000, int offset = 0)
var result = await _context.Students.AddAsync(student);
await _context.SaveChangesAsync();
return _mapper.Map<StudentDTO>(result.Entity);
}
public Task<StudentDTO> DeleteAsync(Guid id) => throw new NotImplementedException();
public async Task<List<StudentDTO>> GetAsync(int limit = 10000, int offset = 0)
=> _mapper.Map<List<StudentDTO>>(
await _context.Students
.Include(s => s.StudentSessions)
@ -29,7 +39,32 @@ namespace Lab3.Database.Repository.Implementations
.Take(limit)
.ToListAsync());
public Task<StudentDTO> Get(Guid id) => throw new NotImplementedException();
public Task<StudentDTO> Update(StudentDTO studentDTO) => throw new NotImplementedException();
public Task<StudentDTO> GetAsync(Guid id) => throw new NotImplementedException();
public async Task<StudentDTO?> UpdateAsync(StudentDTO studentDTO)
{
var student = _mapper.Map<Student>(studentDTO);
var currStudent = await _context.Students.FindAsync(student.Id);
if (currStudent == null)
{
return null;
}
currStudent.Name = student.Name;
currStudent.StartEducation = student.StartEducation;
currStudent.EducationForm = student.EducationForm;
foreach (var session in currStudent.StudentSessions)
{
session.Score = student.StudentSessions
.FirstOrDefault(s => s.Number == session.Number)?.Score ?? session.Score;
}
await _context.SaveChangesAsync();
return _mapper.Map<StudentDTO>(currStudent);
}
}
}

View File

@ -4,14 +4,14 @@ namespace Lab3.Database.Repository.Interfaces
{
public interface IStudentRepository
{
public Task<List<StudentDTO>> Get(int limit = 10000, int offset = 0);
public Task<List<StudentDTO>> GetAsync(int limit = 10000, int offset = 0);
public Task<StudentDTO> Get(Guid id);
public Task<StudentDTO> GetAsync(Guid id);
public Task<StudentDTO> Update(StudentDTO studentDTO);
public Task<StudentDTO?> UpdateAsync(StudentDTO studentDTO);
public Task<StudentDTO> Delete(Guid id);
public Task<StudentDTO> DeleteAsync(Guid id);
public Task<List<StudentDTO>> Create(StudentDTO studentDTO);
public Task<StudentDTO> CreateAsync(StudentDTO studentDTO);
}
}

View File

@ -1,4 +1,6 @@
using Lab3.Database.Extensions;
using DocumentFormat.OpenXml.Office2010.Excel;
using Lab3.Database.Extensions;
using Lab3.Database.Repository.Interfaces;
using Lab3.Forms;
using Lab3.MappingProfiles;
using Microsoft.Extensions.Configuration;
@ -23,7 +25,13 @@ namespace Lab3.Extensions
this IServiceCollection services)
{
services.AddScoped<MainForm>();
services.AddScoped<CreateForm>();
services.AddScoped<Func<Guid?, CreateForm>>(sp => (id
=> new CreateForm(
sp.GetRequiredService<IStudentRepository>(),
sp.GetRequiredService<IEducationFormRepository>(),
id
)));
return services;
}

View File

@ -28,52 +28,52 @@
/// </summary>
private void InitializeComponent()
{
customDateTimePicker1 = new CustomsComponentsVar2.CustomDateTimePicker();
visualSelectionComponent1 = new ComponentsLab.VisualSelectionComponent();
StartEducationDataPicker = new CustomsComponentsVar2.CustomDateTimePicker();
FormSelector = new ComponentsLab.VisualSelectionComponent();
label1 = new Label();
label2 = new Label();
textBox1 = new TextBox();
NameTextBox = new TextBox();
label3 = new Label();
numericUpDown1 = new NumericUpDown();
session1Score = new NumericUpDown();
label4 = new Label();
label5 = new Label();
label6 = new Label();
numericUpDown2 = new NumericUpDown();
session2Score = new NumericUpDown();
label7 = new Label();
numericUpDown3 = new NumericUpDown();
session3Score = new NumericUpDown();
label8 = new Label();
numericUpDown4 = new NumericUpDown();
session6Score = new NumericUpDown();
label9 = new Label();
numericUpDown5 = new NumericUpDown();
session5Score = new NumericUpDown();
label10 = new Label();
numericUpDown6 = new NumericUpDown();
button1 = new Button();
button2 = new Button();
((System.ComponentModel.ISupportInitialize)numericUpDown1).BeginInit();
((System.ComponentModel.ISupportInitialize)numericUpDown2).BeginInit();
((System.ComponentModel.ISupportInitialize)numericUpDown3).BeginInit();
((System.ComponentModel.ISupportInitialize)numericUpDown4).BeginInit();
((System.ComponentModel.ISupportInitialize)numericUpDown5).BeginInit();
((System.ComponentModel.ISupportInitialize)numericUpDown6).BeginInit();
session4Score = new NumericUpDown();
ButtonSave = new Button();
buttonCancel = new Button();
((System.ComponentModel.ISupportInitialize)session1Score).BeginInit();
((System.ComponentModel.ISupportInitialize)session2Score).BeginInit();
((System.ComponentModel.ISupportInitialize)session3Score).BeginInit();
((System.ComponentModel.ISupportInitialize)session6Score).BeginInit();
((System.ComponentModel.ISupportInitialize)session5Score).BeginInit();
((System.ComponentModel.ISupportInitialize)session4Score).BeginInit();
SuspendLayout();
//
// customDateTimePicker1
// StartEducationDataPicker
//
customDateTimePicker1.Location = new Point(430, 32);
customDateTimePicker1.Name = "customDateTimePicker1";
customDateTimePicker1.Size = new Size(456, 55);
customDateTimePicker1.TabIndex = 0;
customDateTimePicker1.Value = new DateTime(2024, 11, 6, 22, 54, 54, 353);
StartEducationDataPicker.Location = new Point(430, 32);
StartEducationDataPicker.Name = "StartEducationDataPicker";
StartEducationDataPicker.Size = new Size(456, 55);
StartEducationDataPicker.TabIndex = 0;
StartEducationDataPicker.Value = new DateTime(2024, 11, 6, 22, 54, 54, 353);
//
// visualSelectionComponent1
// FormSelector
//
visualSelectionComponent1.BorderStyle = BorderStyle.FixedSingle;
visualSelectionComponent1.comboBoxSelectedValue = "";
visualSelectionComponent1.Location = new Point(430, 114);
visualSelectionComponent1.Margin = new Padding(3, 4, 3, 4);
visualSelectionComponent1.Name = "visualSelectionComponent1";
visualSelectionComponent1.Size = new Size(211, 109);
visualSelectionComponent1.TabIndex = 1;
FormSelector.BorderStyle = BorderStyle.FixedSingle;
FormSelector.ComboBoxSelectedValue = "";
FormSelector.Location = new Point(436, 133);
FormSelector.Margin = new Padding(3, 4, 3, 4);
FormSelector.Name = "FormSelector";
FormSelector.Size = new Size(352, 31);
FormSelector.TabIndex = 1;
//
// label1
//
@ -87,18 +87,18 @@
// label2
//
label2.AutoSize = true;
label2.Location = new Point(436, 90);
label2.Location = new Point(436, 101);
label2.Name = "label2";
label2.Size = new Size(128, 20);
label2.TabIndex = 3;
label2.Text = "Форма обучения";
//
// textBox1
// NameTextBox
//
textBox1.Location = new Point(12, 60);
textBox1.Name = "textBox1";
textBox1.Size = new Size(315, 27);
textBox1.TabIndex = 4;
NameTextBox.Location = new Point(12, 60);
NameTextBox.Name = "NameTextBox";
NameTextBox.Size = new Size(315, 27);
NameTextBox.TabIndex = 4;
//
// label3
//
@ -109,15 +109,15 @@
label3.TabIndex = 5;
label3.Text = "ФИО";
//
// numericUpDown1
// session1Score
//
numericUpDown1.DecimalPlaces = 2;
numericUpDown1.Increment = new decimal(new int[] { 1, 0, 0, 131072 });
numericUpDown1.Location = new Point(12, 156);
numericUpDown1.Maximum = new decimal(new int[] { 5, 0, 0, 0 });
numericUpDown1.Name = "numericUpDown1";
numericUpDown1.Size = new Size(150, 27);
numericUpDown1.TabIndex = 6;
session1Score.DecimalPlaces = 2;
session1Score.Increment = new decimal(new int[] { 1, 0, 0, 131072 });
session1Score.Location = new Point(12, 156);
session1Score.Maximum = new decimal(new int[] { 5, 0, 0, 0 });
session1Score.Name = "session1Score";
session1Score.Size = new Size(150, 27);
session1Score.TabIndex = 6;
//
// label4
//
@ -146,15 +146,15 @@
label6.TabIndex = 10;
label6.Text = "Сессия2";
//
// numericUpDown2
// session2Score
//
numericUpDown2.DecimalPlaces = 2;
numericUpDown2.Increment = new decimal(new int[] { 1, 0, 0, 131072 });
numericUpDown2.Location = new Point(12, 211);
numericUpDown2.Maximum = new decimal(new int[] { 5, 0, 0, 0 });
numericUpDown2.Name = "numericUpDown2";
numericUpDown2.Size = new Size(150, 27);
numericUpDown2.TabIndex = 9;
session2Score.DecimalPlaces = 2;
session2Score.Increment = new decimal(new int[] { 1, 0, 0, 131072 });
session2Score.Location = new Point(12, 211);
session2Score.Maximum = new decimal(new int[] { 5, 0, 0, 0 });
session2Score.Name = "session2Score";
session2Score.Size = new Size(150, 27);
session2Score.TabIndex = 9;
//
// label7
//
@ -165,15 +165,15 @@
label7.TabIndex = 12;
label7.Text = "Сессия3";
//
// numericUpDown3
// session3Score
//
numericUpDown3.DecimalPlaces = 2;
numericUpDown3.Increment = new decimal(new int[] { 1, 0, 0, 131072 });
numericUpDown3.Location = new Point(12, 270);
numericUpDown3.Maximum = new decimal(new int[] { 5, 0, 0, 0 });
numericUpDown3.Name = "numericUpDown3";
numericUpDown3.Size = new Size(150, 27);
numericUpDown3.TabIndex = 11;
session3Score.DecimalPlaces = 2;
session3Score.Increment = new decimal(new int[] { 1, 0, 0, 131072 });
session3Score.Location = new Point(12, 270);
session3Score.Maximum = new decimal(new int[] { 5, 0, 0, 0 });
session3Score.Name = "session3Score";
session3Score.Size = new Size(150, 27);
session3Score.TabIndex = 11;
//
// label8
//
@ -184,15 +184,15 @@
label8.TabIndex = 18;
label8.Text = "Сессия6";
//
// numericUpDown4
// session6Score
//
numericUpDown4.DecimalPlaces = 2;
numericUpDown4.Increment = new decimal(new int[] { 1, 0, 0, 131072 });
numericUpDown4.Location = new Point(177, 270);
numericUpDown4.Maximum = new decimal(new int[] { 5, 0, 0, 0 });
numericUpDown4.Name = "numericUpDown4";
numericUpDown4.Size = new Size(150, 27);
numericUpDown4.TabIndex = 17;
session6Score.DecimalPlaces = 2;
session6Score.Increment = new decimal(new int[] { 1, 0, 0, 131072 });
session6Score.Location = new Point(177, 270);
session6Score.Maximum = new decimal(new int[] { 5, 0, 0, 0 });
session6Score.Name = "session6Score";
session6Score.Size = new Size(150, 27);
session6Score.TabIndex = 17;
//
// label9
//
@ -203,15 +203,15 @@
label9.TabIndex = 16;
label9.Text = "Сессия5";
//
// numericUpDown5
// session5Score
//
numericUpDown5.DecimalPlaces = 2;
numericUpDown5.Increment = new decimal(new int[] { 1, 0, 0, 131072 });
numericUpDown5.Location = new Point(177, 211);
numericUpDown5.Maximum = new decimal(new int[] { 5, 0, 0, 0 });
numericUpDown5.Name = "numericUpDown5";
numericUpDown5.Size = new Size(150, 27);
numericUpDown5.TabIndex = 15;
session5Score.DecimalPlaces = 2;
session5Score.Increment = new decimal(new int[] { 1, 0, 0, 131072 });
session5Score.Location = new Point(177, 211);
session5Score.Maximum = new decimal(new int[] { 5, 0, 0, 0 });
session5Score.Name = "session5Score";
session5Score.Size = new Size(150, 27);
session5Score.TabIndex = 15;
//
// label10
//
@ -222,95 +222,96 @@
label10.TabIndex = 14;
label10.Text = "Сессия4";
//
// numericUpDown6
// session4Score
//
numericUpDown6.DecimalPlaces = 2;
numericUpDown6.Increment = new decimal(new int[] { 1, 0, 0, 131072 });
numericUpDown6.Location = new Point(177, 156);
numericUpDown6.Maximum = new decimal(new int[] { 5, 0, 0, 0 });
numericUpDown6.Name = "numericUpDown6";
numericUpDown6.Size = new Size(150, 27);
numericUpDown6.TabIndex = 13;
session4Score.DecimalPlaces = 2;
session4Score.Increment = new decimal(new int[] { 1, 0, 0, 131072 });
session4Score.Location = new Point(177, 156);
session4Score.Maximum = new decimal(new int[] { 5, 0, 0, 0 });
session4Score.Name = "session4Score";
session4Score.Size = new Size(150, 27);
session4Score.TabIndex = 13;
//
// button1
// ButtonSave
//
button1.Location = new Point(430, 258);
button1.Name = "button1";
button1.Size = new Size(164, 39);
button1.TabIndex = 19;
button1.Text = "Сохранить";
button1.UseVisualStyleBackColor = true;
ButtonSave.Location = new Point(430, 258);
ButtonSave.Name = "ButtonSave";
ButtonSave.Size = new Size(164, 39);
ButtonSave.TabIndex = 19;
ButtonSave.Text = "Сохранить";
ButtonSave.UseVisualStyleBackColor = true;
ButtonSave.Click += ButtonSave_ClickAsync;
//
// button2
// buttonCancel
//
button2.Location = new Point(624, 258);
button2.Name = "button2";
button2.Size = new Size(164, 39);
button2.TabIndex = 20;
button2.Text = "Отмена";
button2.UseVisualStyleBackColor = true;
buttonCancel.Location = new Point(624, 258);
buttonCancel.Name = "buttonCancel";
buttonCancel.Size = new Size(164, 39);
buttonCancel.TabIndex = 20;
buttonCancel.Text = "Отмена";
buttonCancel.UseVisualStyleBackColor = true;
//
// CreateForm
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 309);
Controls.Add(button2);
Controls.Add(button1);
Controls.Add(buttonCancel);
Controls.Add(ButtonSave);
Controls.Add(label8);
Controls.Add(numericUpDown4);
Controls.Add(session6Score);
Controls.Add(label9);
Controls.Add(numericUpDown5);
Controls.Add(session5Score);
Controls.Add(label10);
Controls.Add(numericUpDown6);
Controls.Add(session4Score);
Controls.Add(label7);
Controls.Add(numericUpDown3);
Controls.Add(session3Score);
Controls.Add(label6);
Controls.Add(numericUpDown2);
Controls.Add(session2Score);
Controls.Add(label5);
Controls.Add(label4);
Controls.Add(numericUpDown1);
Controls.Add(session1Score);
Controls.Add(label3);
Controls.Add(textBox1);
Controls.Add(NameTextBox);
Controls.Add(label2);
Controls.Add(label1);
Controls.Add(customDateTimePicker1);
Controls.Add(visualSelectionComponent1);
Controls.Add(StartEducationDataPicker);
Controls.Add(FormSelector);
Name = "CreateForm";
Text = "CreateForm";
Load += CreateForm_Load;
((System.ComponentModel.ISupportInitialize)numericUpDown1).EndInit();
((System.ComponentModel.ISupportInitialize)numericUpDown2).EndInit();
((System.ComponentModel.ISupportInitialize)numericUpDown3).EndInit();
((System.ComponentModel.ISupportInitialize)numericUpDown4).EndInit();
((System.ComponentModel.ISupportInitialize)numericUpDown5).EndInit();
((System.ComponentModel.ISupportInitialize)numericUpDown6).EndInit();
Load += CreateForm_LoadAsync;
((System.ComponentModel.ISupportInitialize)session1Score).EndInit();
((System.ComponentModel.ISupportInitialize)session2Score).EndInit();
((System.ComponentModel.ISupportInitialize)session3Score).EndInit();
((System.ComponentModel.ISupportInitialize)session6Score).EndInit();
((System.ComponentModel.ISupportInitialize)session5Score).EndInit();
((System.ComponentModel.ISupportInitialize)session4Score).EndInit();
ResumeLayout(false);
PerformLayout();
}
#endregion
private CustomsComponentsVar2.CustomDateTimePicker customDateTimePicker1;
private ComponentsLab.VisualSelectionComponent visualSelectionComponent1;
private CustomsComponentsVar2.CustomDateTimePicker StartEducationDataPicker;
private ComponentsLab.VisualSelectionComponent FormSelector;
private Label label1;
private Label label2;
private TextBox textBox1;
private TextBox NameTextBox;
private Label label3;
private NumericUpDown numericUpDown1;
private NumericUpDown session1Score;
private Label label4;
private Label label5;
private Label label6;
private NumericUpDown numericUpDown2;
private NumericUpDown session2Score;
private Label label7;
private NumericUpDown numericUpDown3;
private NumericUpDown session3Score;
private Label label8;
private NumericUpDown numericUpDown4;
private NumericUpDown session6Score;
private Label label9;
private NumericUpDown numericUpDown5;
private NumericUpDown session5Score;
private Label label10;
private NumericUpDown numericUpDown6;
private Button button1;
private Button button2;
private NumericUpDown session4Score;
private Button ButtonSave;
private Button buttonCancel;
}
}

View File

@ -1,17 +1,87 @@
using Lab3.Database.Repository.Interfaces;
using Lab3.Database.DTO;
using Lab3.Database.Models;
using Lab3.Database.Repository.Interfaces;
namespace Lab3.Forms
{
public partial class CreateForm : Form
{
public CreateForm(IStudentRepository student)
private readonly IStudentRepository _studentRepository;
private readonly IEducationFormRepository _educationFormRepository;
private readonly Guid? _updatedStudentGuid = null;
public CreateForm(
IStudentRepository studentRepository,
IEducationFormRepository educationFormRepository,
Guid? updatedStudentGuid)
{
_studentRepository = studentRepository;
_educationFormRepository = educationFormRepository;
_updatedStudentGuid = updatedStudentGuid;
InitializeComponent();
}
private void CreateForm_Load(object sender, EventArgs e)
private async void CreateForm_LoadAsync(object sender, EventArgs e)
{
FormSelector.Fill(await _educationFormRepository.Get());
StartEducationDataPicker.DateStart = DateTime.Now.AddYears(-6);
StartEducationDataPicker.DateEnd = DateTime.Now;
}
private async void ButtonSave_ClickAsync(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(NameTextBox.Text)
|| string.IsNullOrEmpty(FormSelector.ComboBoxSelectedValue))
{
throw new Exception();
}
StudentDTO student = new()
{
Id = _updatedStudentGuid ?? Guid.NewGuid(),
Name = NameTextBox.Text,
StartEducation = StartEducationDataPicker.Value,
EducationForm = FormSelector.ComboBoxSelectedValue,
StudentSessions = [
new(){
Score = session1Score.Value,
Number = 1,
},
new(){
Score = session2Score.Value,
Number = 2,
},
new(){
Score = session3Score.Value,
Number = 3,
},
new(){
Score = session4Score.Value,
Number = 4,
},
new(){
Score = session5Score.Value,
Number = 5,
},
new(){
Score = session6Score.Value,
Number = 6,
},
],
};
if (_updatedStudentGuid != null)
{
await _studentRepository.UpdateAsync(student);
}
else
{
await _studentRepository.CreateAsync(student);
}
}
}
}

View File

@ -20,7 +20,7 @@ namespace Lab3.Forms
private async void MainForm_LoadAsync(object sender, EventArgs e)
{
var students = _mapper.Map<List<StudentViewModel>>(await _studentRepository.Get());
var students = _mapper.Map<List<StudentViewModel>>(await _studentRepository.GetAsync());
StudentsListBox.FillListBox(students);
}
}

View File

@ -13,8 +13,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="ComponentsLibraryLab1" Version="1.0.0" />
<PackageReference Include="ComponentsLibraryLab2" Version="1.0.0" />
<PackageReference Include="ComponentsLibraryLab1" Version="8.0.2" />
<PackageReference Include="ComponentsLibraryLab2" Version="8.0.1" />
<PackageReference Include="Cop.Borovkov.Var3" Version="8.0.1" />
<PackageReference Include="CustomComponentsVar2" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />

View File

@ -21,7 +21,7 @@ namespace Lab3
var app = CreateHostBuilder().Build();
Application.Run(app.Services.GetRequiredService<CreateForm>());
Application.Run(app.Services.GetRequiredService<Func<Guid?, CreateForm>>()(null));
}
static IHostBuilder CreateHostBuilder()