From ba40b2c9e155321b49f852ba2e5e8320a7b55e8e Mon Sep 17 00:00:00 2001 From: funa4i Date: Mon, 18 Nov 2024 14:54:58 +0400 Subject: [PATCH 1/5] fullab --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index ca1c7a3..269ad74 100644 --- a/.gitignore +++ b/.gitignore @@ -398,3 +398,5 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml +/StudentProgressRecord/docker-compose.yml +/StudentProgressRecord/V1_ddl.sql -- 2.25.1 From d5889f7b3e3c1d7961a003167e9f85342ee78280 Mon Sep 17 00:00:00 2001 From: funa4i Date: Mon, 18 Nov 2024 14:55:50 +0400 Subject: [PATCH 2/5] labfull2 --- StudentProgressRecord/Entity/Marks.cs | 1 + StudentProgressRecord/Entity/Statement.cs | 2 +- .../Entity/StudentTransition.cs | 4 +- .../Forms/FormsEntity/FormStatement.cs | 5 +- .../IRepositories/IConnectionString.cs | 13 +++ StudentProgressRecord/Program.cs | 18 +++ .../RepositoryImp/ConnectionString.cs | 18 +++ .../RepositoryImp/StatementRepository.cs | 95 ++++++++++++++-- .../RepositoryImp/StudentRepository.cs | 106 +++++++++++++++++- .../StudentTransitionRepository.cs | 81 ++++++++++++- .../RepositoryImp/SubjectRepository.cs | 101 ++++++++++++++++- .../RepositoryImp/TeacherRepository.cs | 106 +++++++++++++++++- .../StudentProgressRecord.csproj | 24 ++++ StudentProgressRecord/appsetting.json | 15 +++ 14 files changed, 555 insertions(+), 34 deletions(-) create mode 100644 StudentProgressRecord/IRepositories/IConnectionString.cs create mode 100644 StudentProgressRecord/RepositoryImp/ConnectionString.cs create mode 100644 StudentProgressRecord/appsetting.json diff --git a/StudentProgressRecord/Entity/Marks.cs b/StudentProgressRecord/Entity/Marks.cs index b54ec69..641d923 100644 --- a/StudentProgressRecord/Entity/Marks.cs +++ b/StudentProgressRecord/Entity/Marks.cs @@ -23,5 +23,6 @@ namespace StudentProgressRecord.Entity Mark = mark }; } + } } diff --git a/StudentProgressRecord/Entity/Statement.cs b/StudentProgressRecord/Entity/Statement.cs index a6ec764..6c7c009 100644 --- a/StudentProgressRecord/Entity/Statement.cs +++ b/StudentProgressRecord/Entity/Statement.cs @@ -17,7 +17,7 @@ namespace StudentProgressRecord.Entity public DateTime Date { get; set; } - public IEnumerable Marks { get; private set; } = []; + public IEnumerable Marks { get; set; } = []; public static Statement CreateOperation(long id, long subjectId, long teacherId, DateTime timeStamp, IEnumerable marks) diff --git a/StudentProgressRecord/Entity/StudentTransition.cs b/StudentProgressRecord/Entity/StudentTransition.cs index ed4f112..5e69af6 100644 --- a/StudentProgressRecord/Entity/StudentTransition.cs +++ b/StudentProgressRecord/Entity/StudentTransition.cs @@ -16,7 +16,7 @@ namespace StudentProgressRecord.Entity public Operations Operation { get; set; } - public DateTime TimeStamp { get; set; } + public DateTime Date { get; set; } public static StudentTransition CreateOperation(long id, long studentId, Operations operation, DateTime time) { @@ -25,7 +25,7 @@ namespace StudentProgressRecord.Entity Id = id, StudentId = studentId, Operation = operation, - TimeStamp = time + Date = time }; } } diff --git a/StudentProgressRecord/Forms/FormsEntity/FormStatement.cs b/StudentProgressRecord/Forms/FormsEntity/FormStatement.cs index 9c9956f..e52dd98 100644 --- a/StudentProgressRecord/Forms/FormsEntity/FormStatement.cs +++ b/StudentProgressRecord/Forms/FormsEntity/FormStatement.cs @@ -35,9 +35,9 @@ namespace StudentProgressRecord.Forms var list = new List() {1,2,3,4,5}; + columnMark.ValueType = typeof(int); columnMark.DataSource = list; - - + columnStudent.DataSource = studentRepository.ReadStudents(); columnStudent.DisplayMember = "Name"; columnStudent.ValueMember = "Id"; @@ -58,6 +58,7 @@ namespace StudentProgressRecord.Forms dateTimePicker.Value, CreateMarkListFromDataGrid()) ); + Close(); } catch (Exception ex) { diff --git a/StudentProgressRecord/IRepositories/IConnectionString.cs b/StudentProgressRecord/IRepositories/IConnectionString.cs new file mode 100644 index 0000000..4dac8fe --- /dev/null +++ b/StudentProgressRecord/IRepositories/IConnectionString.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StudentProgressRecord.IRepositories +{ + public interface IConnectionString + { + string GetConnectionString(); + } +} diff --git a/StudentProgressRecord/Program.cs b/StudentProgressRecord/Program.cs index e88baeb..05ab2e7 100644 --- a/StudentProgressRecord/Program.cs +++ b/StudentProgressRecord/Program.cs @@ -1,3 +1,6 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Serilog; using StudentProgressRecord.Forms; using StudentProgressRecord.IRepositories; @@ -5,6 +8,7 @@ using StudentProgressRecord.Repositories; using StudentProgressRecord.RepositoryImp; using Unity; using Unity.Lifetime; +using Unity.Microsoft.Logging; namespace StudentProgressRecord { @@ -24,6 +28,9 @@ namespace StudentProgressRecord { var container = new UnityContainer(); + container.AddExtension(new LoggingExtension(CreateLoggerFactory())); + + container.RegisterType(new SingletonLifetimeManager()); container.RegisterType (new TransientLifetimeManager()); @@ -43,5 +50,16 @@ namespace StudentProgressRecord return container; } + private static LoggerFactory CreateLoggerFactory() + { + var lf = new LoggerFactory(); + lf.AddSerilog(new LoggerConfiguration() + .ReadFrom.Configuration(new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsetting.json") + .Build()) + .CreateLogger()); + return lf; + } } } \ No newline at end of file diff --git a/StudentProgressRecord/RepositoryImp/ConnectionString.cs b/StudentProgressRecord/RepositoryImp/ConnectionString.cs new file mode 100644 index 0000000..7f8264e --- /dev/null +++ b/StudentProgressRecord/RepositoryImp/ConnectionString.cs @@ -0,0 +1,18 @@ +using Microsoft.VisualBasic.ApplicationServices; +using StudentProgressRecord.IRepositories; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StudentProgressRecord.RepositoryImp +{ + public class ConnectionString : IConnectionString + { + public string GetConnectionString() + { + return "Server=localhost;Database=University;User Id=root;Password=password;"; + } + } +} diff --git a/StudentProgressRecord/RepositoryImp/StatementRepository.cs b/StudentProgressRecord/RepositoryImp/StatementRepository.cs index ec4736d..422c1f6 100644 --- a/StudentProgressRecord/RepositoryImp/StatementRepository.cs +++ b/StudentProgressRecord/RepositoryImp/StatementRepository.cs @@ -1,29 +1,106 @@ -using StudentProgressRecord.Entity; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using StudentProgressRecord.Entity; +using StudentProgressRecord.IRepositories; using StudentProgressRecord.Repositories; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Data.SqlClient; + namespace StudentProgressRecord.RepositoryImp { public class StatementRepository : IStatementRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + + public StatementRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateStatement(Statement statement) { - + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(statement)); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + connection.Open(); + + using var transaction = connection.BeginTransaction(); + + var queryInsert = @" + INSERT INTO Statement (SubjectId, TeacherId, Date) + VALUES (@SubjectId, @TeacherId, @Date); + SELECT MAX(Id) FROM Statement"; + + var statementID = connection.QueryFirst(queryInsert, statement, transaction); + + var querySubInsert = @" + INSERT INTO Marks (StatementId, StudentId, Mark) + VALUES (@StatementId, @StudentId,@Mark)"; + + foreach (var elem in statement.Marks) + { + connection.Execute(querySubInsert, new + { + statementID, + elem.StudentId, + elem.Mark + }, transaction); + } + transaction.Commit(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } + } public void DeleteStatement(long id) { - + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var queryDelete = @" + DELETE FROM Statement + WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public IEnumerable ReadStatements(long? subjectId = null, long? teacherId = null, DateTime? dateFrom = null, DateTime? dateTo = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var querySelect = "SELECT * FROM Statement"; + var objs = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(objs)); + return objs; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } } diff --git a/StudentProgressRecord/RepositoryImp/StudentRepository.cs b/StudentProgressRecord/RepositoryImp/StudentRepository.cs index a4ce0a1..a2e266a 100644 --- a/StudentProgressRecord/RepositoryImp/StudentRepository.cs +++ b/StudentProgressRecord/RepositoryImp/StudentRepository.cs @@ -1,4 +1,10 @@ -using StudentProgressRecord.Entity; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using Serilog.Core; +using StudentProgressRecord.Entity; +using StudentProgressRecord.IRepositories; using StudentProgressRecord.Repositories; using System; using System.Collections.Generic; @@ -10,29 +16,117 @@ namespace StudentProgressRecord.RepositoryImp { public class StudentRepository : IStudentRepository { + + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public StudentRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateStudent(Student student) { - + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект, {json}", JsonConvert.SerializeObject(student)); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var query = @" + INSERT INTO Student (Name, FamilyPos, Domitory) + VALUES (@Name, @FamilyPos, @Domitory)"; + connection.Execute(query, student); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении обЪекта"); + throw; + } } public void DeleteStudent(long id) { - + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var queryDelete = @" + DELETE FROM Student + WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public Student ReadStudentById(long id) { - return Student.CreateEntity(0, string.Empty, false , false); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var querySelect = @" + SELECT * FROM Student + WHERE Id=@id"; + var obj = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(obj)); + return obj; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable ReadStudents(bool? familyPos = null, bool? domitory = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var querySelect = "SELECT * FROM Student"; + var objs = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(objs)); + return objs; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public void UpdateStudent(Student student) { - + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(student)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var queryUpdate = @" + UPDATE Student + SET + Name=@Name, + FamilyPos=@FamilyPos, + Domitory=@Domitory + WHERE Id=@Id"; + connection.Execute(queryUpdate, student); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } } diff --git a/StudentProgressRecord/RepositoryImp/StudentTransitionRepository.cs b/StudentProgressRecord/RepositoryImp/StudentTransitionRepository.cs index ddec0cb..d54c6f6 100644 --- a/StudentProgressRecord/RepositoryImp/StudentTransitionRepository.cs +++ b/StudentProgressRecord/RepositoryImp/StudentTransitionRepository.cs @@ -1,28 +1,101 @@ using StudentProgressRecord.Entity; using StudentProgressRecord.IRepositories; using StudentProgressRecord.Entity.Enums; +using Newtonsoft.Json; +using Npgsql; +using Microsoft.Extensions.Logging; +using Dapper; namespace StudentProgressRecord.RepositoryImp { public class StudentTransitionRepository : IStudentTransitionRepository { + + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public StudentTransitionRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateStudentTransition(StudentTransition studentTransition) { - + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект, {json}", JsonConvert.SerializeObject(studentTransition)); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var query = @" + INSERT INTO StudentTransition (StudentId, Operation, Date) + VALUES (@StudentId, @Operation, @Date)"; + connection.Execute(query, studentTransition); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении обЪекта"); + throw; + } } public void DeleteStudentTransition(long id) { - + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var queryDelete = @" + DELETE FROM StudentTransition + WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public StudentTransition ReadStudentTransitionById(long id) { - return StudentTransition.CreateOperation(0, 0, Operations.Transfer, DateTime.Now); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var querySelect = @" + SELECT * FROM StudentTransition + WHERE Id=@id"; + var obj = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(obj)); + return obj; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable ReadStudentTransitions(long? groupId = null, bool? familyPos = null, bool? domitory = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var querySelect = "SELECT * FROM StudentTransition"; + var objs = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(objs)); + return objs; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } } diff --git a/StudentProgressRecord/RepositoryImp/SubjectRepository.cs b/StudentProgressRecord/RepositoryImp/SubjectRepository.cs index 6ecf5f8..8d935fd 100644 --- a/StudentProgressRecord/RepositoryImp/SubjectRepository.cs +++ b/StudentProgressRecord/RepositoryImp/SubjectRepository.cs @@ -1,4 +1,9 @@ -using StudentProgressRecord.Entity; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using StudentProgressRecord.Entity; +using StudentProgressRecord.IRepositories; using StudentProgressRecord.Repositories; using System; using System.Collections.Generic; @@ -10,29 +15,115 @@ namespace StudentProgressRecord.RepositoryImp { public class SubjectRepository : ISubjectRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public SubjectRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateSubject(Subject subject) { - + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект, {json}", JsonConvert.SerializeObject(subject)); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var query = @" + INSERT INTO Subject (Name) + VALUES (@Name)"; + connection.Execute(query, subject); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении обЪекта"); + throw; + } } public void DeleteSubject(long id) { - + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var queryDelete = @" + DELETE FROM Subject + WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public Subject ReadSubjectById(long id) { - return Subject.CreateEntity(0, string.Empty); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var querySelect = @" + SELECT * FROM Subject + WHERE Id=@id"; + var obj = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(obj)); + return obj; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable ReadSubjects() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var querySelect = "SELECT * FROM Subject"; + var objs = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(objs)); + return objs; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public void UpdateSubject(Subject subject) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(subject)); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var queryUpdate = @" + UPDATE Subject + SET + Name=@Name + WHERE Id=@Id"; + connection.Execute(queryUpdate, subject); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } } diff --git a/StudentProgressRecord/RepositoryImp/TeacherRepository.cs b/StudentProgressRecord/RepositoryImp/TeacherRepository.cs index 9ac1980..8fdc506 100644 --- a/StudentProgressRecord/RepositoryImp/TeacherRepository.cs +++ b/StudentProgressRecord/RepositoryImp/TeacherRepository.cs @@ -1,8 +1,14 @@ -using StudentProgressRecord.Entity; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using StudentProgressRecord.Entity; +using StudentProgressRecord.IRepositories; using StudentProgressRecord.Repositories; using System; using System.Collections.Generic; using System.Linq; +using System.Net.NetworkInformation; using System.Text; using System.Threading.Tasks; @@ -10,28 +16,118 @@ namespace StudentProgressRecord.RepositoryImp { public class TeacherRepository : ITeacherRepository { + + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public TeacherRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + + public void CreateTeacher(Teacher teacher) { - + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект, {json}", JsonConvert.SerializeObject(teacher)); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var query = @" + INSERT INTO Teacher (Name) + VALUES (@Name)"; + connection.Execute(query, teacher); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении обЪекта"); + throw; + } } public void DeleteTeacher(long id) { - + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var queryDelete = @" + DELETE FROM Teacher + WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public Teacher ReadTeacherById(long id) { - return Teacher.CreateEntity(0, string.Empty); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var querySelect = @" + SELECT * FROM Teacher + WHERE Id=@id"; + var obj = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(obj)); + return obj; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable ReadTeachers(long? departmentID = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var querySelect = "SELECT * FROM Teacher"; + var objs = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(objs)); + return objs; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public void UpdateTeacher(Teacher teacher) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(teacher)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var queryUpdate = @" + UPDATE Teacher + SET + Name=@Name + WHERE Id=@Id"; + connection.Execute(queryUpdate, teacher); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } + } } diff --git a/StudentProgressRecord/StudentProgressRecord.csproj b/StudentProgressRecord/StudentProgressRecord.csproj index 94297cd..126a7d2 100644 --- a/StudentProgressRecord/StudentProgressRecord.csproj +++ b/StudentProgressRecord/StudentProgressRecord.csproj @@ -9,9 +9,29 @@ + + + + + + Always + + + + + + + + + + + + + + @@ -29,4 +49,8 @@ + + + + \ No newline at end of file diff --git a/StudentProgressRecord/appsetting.json b/StudentProgressRecord/appsetting.json new file mode 100644 index 0000000..a13deda --- /dev/null +++ b/StudentProgressRecord/appsetting.json @@ -0,0 +1,15 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "Logs/university_log.txt", + "rollingInterval": "Day" + } + } + ] + } +} \ No newline at end of file -- 2.25.1 From d87c72750bbabb42ccf3a912488732627699edec Mon Sep 17 00:00:00 2001 From: funa4i Date: Mon, 18 Nov 2024 15:48:14 +0400 Subject: [PATCH 3/5] =?UTF-8?q?=D1=80=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- StudentProgressRecord/RepositoryImp/SubjectRepository.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/StudentProgressRecord/RepositoryImp/SubjectRepository.cs b/StudentProgressRecord/RepositoryImp/SubjectRepository.cs index 9126d3f..47efba3 100644 --- a/StudentProgressRecord/RepositoryImp/SubjectRepository.cs +++ b/StudentProgressRecord/RepositoryImp/SubjectRepository.cs @@ -3,7 +3,7 @@ using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Npgsql; using StudentProgressRecord.Entity; -using StudentProgressRecord.Entity.Enums; +using StudentProgressRecord.IRepositories; using StudentProgressRecord.Repositories; using System; using System.Collections.Generic; -- 2.25.1 From aa5c773d5a7bab4d8c255622c4e457ccc47d6876 Mon Sep 17 00:00:00 2001 From: funa4i Date: Mon, 18 Nov 2024 16:07:41 +0400 Subject: [PATCH 4/5] =?UTF-8?q?=D0=B2=D1=80=D0=BE=D0=B4=D0=B5=20=D0=B2?= =?UTF-8?q?=D1=81=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- StudentProgressRecord/Entity/Subject.cs | 3 ++- .../Forms/FormsEntity/FormSubject.cs | 4 ++-- .../RepositoryImp/SubjectRepository.cs | 13 +++++-------- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/StudentProgressRecord/Entity/Subject.cs b/StudentProgressRecord/Entity/Subject.cs index 7b863ad..16598d6 100644 --- a/StudentProgressRecord/Entity/Subject.cs +++ b/StudentProgressRecord/Entity/Subject.cs @@ -20,7 +20,8 @@ namespace StudentProgressRecord.Entity return new Subject { Id = id, - Name = name + Name = name, + direction = direction }; } } diff --git a/StudentProgressRecord/Forms/FormsEntity/FormSubject.cs b/StudentProgressRecord/Forms/FormsEntity/FormSubject.cs index 455c77e..5ceb3ac 100644 --- a/StudentProgressRecord/Forms/FormsEntity/FormSubject.cs +++ b/StudentProgressRecord/Forms/FormsEntity/FormSubject.cs @@ -62,8 +62,8 @@ namespace StudentProgressRecord { try { - if (string.IsNullOrWhiteSpace(textBoxSubject.Text) || - string.IsNullOrWhiteSpace(textBoxSubject.Text)) + if (string.IsNullOrWhiteSpace(textBoxSubject.Text) || + checkedListBoxDir.CheckedItems.Count == 0) { throw new Exception("Имя не может быть пустым"); } diff --git a/StudentProgressRecord/RepositoryImp/SubjectRepository.cs b/StudentProgressRecord/RepositoryImp/SubjectRepository.cs index 47efba3..399e127 100644 --- a/StudentProgressRecord/RepositoryImp/SubjectRepository.cs +++ b/StudentProgressRecord/RepositoryImp/SubjectRepository.cs @@ -3,13 +3,10 @@ using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Npgsql; using StudentProgressRecord.Entity; +using StudentProgressRecord.Entity.Enums; using StudentProgressRecord.IRepositories; using StudentProgressRecord.Repositories; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; + namespace StudentProgressRecord.RepositoryImp { @@ -32,8 +29,8 @@ namespace StudentProgressRecord.RepositoryImp { using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); var query = @" - INSERT INTO Subject (Name) - VALUES (@Name)"; + INSERT INTO Subject (direction, Name) + VALUES (@direction ,@Name)"; connection.Execute(query, subject); } catch (Exception ex) @@ -64,7 +61,7 @@ namespace StudentProgressRecord.RepositoryImp public Subject ReadSubjectById(long id) { - return Subject.CreateEntity(0, string.Empty); + return Subject.CreateEntity(0, string.Empty, Direction.None); } public IEnumerable ReadSubjects() -- 2.25.1 From a8247cf8f78d635f112ae84e39b1d1ecc4d6a00c Mon Sep 17 00:00:00 2001 From: funa4i Date: Tue, 19 Nov 2024 16:02:24 +0400 Subject: [PATCH 5/5] =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D0=B5=D1=87=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormsEntity/FormStatement.Designer.cs | 1 + .../Forms/FormsEntity/FormStatement.cs | 2 +- .../RepositoryImp/SubjectRepository.cs | 19 ++++++++++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/StudentProgressRecord/Forms/FormsEntity/FormStatement.Designer.cs b/StudentProgressRecord/Forms/FormsEntity/FormStatement.Designer.cs index 305fd83..3cdd1f2 100644 --- a/StudentProgressRecord/Forms/FormsEntity/FormStatement.Designer.cs +++ b/StudentProgressRecord/Forms/FormsEntity/FormStatement.Designer.cs @@ -61,6 +61,7 @@ // // dateTimePicker // + dateTimePicker.Enabled = false; dateTimePicker.Location = new Point(13, 163); dateTimePicker.Name = "dateTimePicker"; dateTimePicker.Size = new Size(250, 27); diff --git a/StudentProgressRecord/Forms/FormsEntity/FormStatement.cs b/StudentProgressRecord/Forms/FormsEntity/FormStatement.cs index e52dd98..9ed164a 100644 --- a/StudentProgressRecord/Forms/FormsEntity/FormStatement.cs +++ b/StudentProgressRecord/Forms/FormsEntity/FormStatement.cs @@ -47,7 +47,7 @@ namespace StudentProgressRecord.Forms { try { - if (dataGridView.RowCount < 1 || comboBoxTeacher.SelectedIndex < 0 || comboBoxSubject.SelectedIndex < 0) + if (dataGridView.RowCount < 2 || comboBoxTeacher.SelectedIndex < 0 || comboBoxSubject.SelectedIndex < 0) { throw new Exception("Имеются незаполненые поля"); } diff --git a/StudentProgressRecord/RepositoryImp/SubjectRepository.cs b/StudentProgressRecord/RepositoryImp/SubjectRepository.cs index 399e127..b7487a9 100644 --- a/StudentProgressRecord/RepositoryImp/SubjectRepository.cs +++ b/StudentProgressRecord/RepositoryImp/SubjectRepository.cs @@ -61,7 +61,24 @@ namespace StudentProgressRecord.RepositoryImp public Subject ReadSubjectById(long id) { - return Subject.CreateEntity(0, string.Empty, Direction.None); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.GetConnectionString()); + var querySelect = @" + SELECT * FROM Subject + WHERE Id=@id"; + var obj = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(obj)); + return obj; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable ReadSubjects() -- 2.25.1