From 0f718e3c87dd994d8c40a038bdf067f224f42902 Mon Sep 17 00:00:00 2001 From: Adelina888 Date: Thu, 28 Nov 2024 22:37:00 +0400 Subject: [PATCH] =?UTF-8?q?2=20=D0=BB=D0=B0=D0=B1=D0=B0=20=D0=B3=D0=BE?= =?UTF-8?q?=D1=82=D0=BE=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectPolyclinic/Forms/FormVisiting.cs | 2 +- .../Implementations/DoctorRepository.cs | 117 ++++++++++++++--- .../Implementations/MedicationRepository.cs | 118 +++++++++++++++--- .../ProfessionalDevelopmentRepository.cs | 52 +++++++- .../Implementations/VisitingRepository.cs | 86 ++++++++++++- 5 files changed, 336 insertions(+), 39 deletions(-) diff --git a/ProjectPolyclinic/ProjectPolyclinic/Forms/FormVisiting.cs b/ProjectPolyclinic/ProjectPolyclinic/Forms/FormVisiting.cs index d10f993..9290f50 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Forms/FormVisiting.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Forms/FormVisiting.cs @@ -76,7 +76,7 @@ namespace ProjectPolyclinic.Forms var list = new List(); foreach (DataGridViewRow row in dataGridViewMedicines.Rows) { - if (row.Cells["ColumnFeed"].Value == null || + if (row.Cells["ColumnMedicines"].Value == null || row.Cells["ColumnCount"].Value == null) { continue; diff --git a/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/DoctorRepository.cs b/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/DoctorRepository.cs index 8e33bd0..d99cd11 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/DoctorRepository.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/DoctorRepository.cs @@ -1,4 +1,8 @@ -using ProjectPolyclinic.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectPolyclinic.Entities; using ProjectPolyclinic.Entities.Enums; using System; using System.Collections.Generic; @@ -8,27 +12,108 @@ using System.Threading.Tasks; namespace ProjectPolyclinic.Repositories.Implementations { - internal class DoctorRepository:IDoctorRepository + public class DoctorRepository:IDoctorRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public DoctorRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateDoctor(Doctor doctor) { - - } - public void DeleteDoctor(int id) - { - - } - public Doctor ReadDoctorById(int id) - { - return Doctor.CreateEntity(0, string.Empty, string.Empty, Specialization.None); - } - public IEnumerable ReadDoctors() - { - return []; + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(doctor)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @"INSERT INTO Doctors (First_Name, Last_Name, Specialization) + VALUES (@First_Name, @Last_Name, @Specialization)"; + connection.Execute(queryInsert, doctor); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void UpdateDoctor(Doctor doctor) { - + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(doctor)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @"UPDATE Doctors SET + First_Name = @First_Name, + Last_Name = @Last_Name, + Specialization= @Specialization + WHERE Id=@id"; + connection.Execute(queryUpdate, doctor); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } + public void DeleteDoctor(int id) + { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @"DELETE FROM Doctors WHERE ID=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } + } + public Doctor ReadDoctorById(int id) + { + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT * FROM Doctors WHERE Id=@id"; + var doctor = connection.QueryFirst(querySelect, new + { + id + }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(doctor)); + return doctor; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + public IEnumerable ReadDoctors() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Doctors"; + var doctors = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(doctors)); + return doctors; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } + } + } } diff --git a/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/MedicationRepository.cs b/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/MedicationRepository.cs index 2ea4f41..f5687ac 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/MedicationRepository.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/MedicationRepository.cs @@ -1,34 +1,120 @@ -using ProjectPolyclinic.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectPolyclinic.Entities; using ProjectPolyclinic.Entities.Enums; using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; using System.Text; using System.Threading.Tasks; namespace ProjectPolyclinic.Repositories.Implementations { - internal class MedicationRepository:IMedicationRepository + public class MedicationRepository:IMedicationRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public MedicationRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateMedication(Medication medication) { - - } - public void DeleteMedication(int id) - { - - } - public Medication ReadMedicationById(int id) - { - return Medication.CreateEntity(0, MedicinesType.None, string.Empty, string.Empty); - } - public IEnumerable ReadMedication() - { - return []; + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(medication)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @"INSERT INTO Medicines (MedicinesType, Dosing, Description) + VALUES (@MedicinesType, @Dosing, @Description)"; + connection.Execute(queryInsert, medication); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void UpdateMedication(Medication medication) { - + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(medication)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @"UPDATE Medicines SET + MedicinesType = @MedicinesType, + Dosing = @Dosing, + Description= @Description + WHERE Id=@id"; + connection.Execute(queryUpdate, medication); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } + public void DeleteMedication(int id) + { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @"DELETE FROM Medicines WHERE ID=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } + } + public Medication ReadMedicationById(int id) + { + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT * FROM Medicines WHERE Id=@id"; + var medication = connection.QueryFirst(querySelect, new + { + id + }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(medication)); + return medication; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + public IEnumerable ReadMedication() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Medicines"; + var medicines = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(medicines)); + return medicines; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } + } + } } diff --git a/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/ProfessionalDevelopmentRepository.cs b/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/ProfessionalDevelopmentRepository.cs index a92b071..b3bb0de 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/ProfessionalDevelopmentRepository.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/ProfessionalDevelopmentRepository.cs @@ -1,4 +1,9 @@ -using ProjectPolyclinic.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectPolyclinic.Entities; +using Serilog.Core; using System; using System.Collections.Generic; using System.Linq; @@ -7,15 +12,56 @@ using System.Threading.Tasks; namespace ProjectPolyclinic.Repositories.Implementations { - internal class ProfessionalDevelopmentRepository:IProfessionalDevelopmentRepository + public class ProfessionalDevelopmentRepository:IProfessionalDevelopmentRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public ProfessionalDevelopmentRepository(IConnectionString connectionString,ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateProfessionalDevelopment(ProfessionalDevelopment professionalDevelopment) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(professionalDevelopment)); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO ProfessionalDevelopments (DoctorId, ProfessionalDevelopmentTime) +VALUES (@DoctorId, @ProfessionalDevelopmentTime)"; + connection.Execute(queryInsert, professionalDevelopment); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public IEnumerable ReadProfessionalDevelopment(DateTime? dateForm = null, DateTime? dateTo = null, int? doctorId = null, int? Id = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM ProfessionalDevelopments"; + var professionalDevelopments = + connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(professionalDevelopments)); + return professionalDevelopments; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } } diff --git a/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/VisitingRepository.cs b/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/VisitingRepository.cs index c538d1f..2a8bab1 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/VisitingRepository.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/VisitingRepository.cs @@ -1,4 +1,9 @@ -using ProjectPolyclinic.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectPolyclinic.Entities; +using Serilog.Core; using System; using System.Collections.Generic; using System.Linq; @@ -7,19 +12,94 @@ using System.Threading.Tasks; namespace ProjectPolyclinic.Repositories.Implementations { - internal class VisitingRepository:IVisitingRepository + public class VisitingRepository:IVisitingRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public VisitingRepository(IConnectionString connectionString,ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateVisiting(Visiting visiting) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(visiting)); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); + var queryInsert = @" +INSERT INTO Visitings (DoctorId, PatientId,DiagnosisName,VisitingTime) +VALUES (@DoctorId, @PatientId, @DiagnosisName,@VisitingTime ); +SELECT MAX(Id) FROM Visitings"; + var visitingId = connection.QueryFirst(queryInsert, visiting, transaction); + var querySubInsert = @" +INSERT INTO Medicines_Visiting (MedicinesId, VisitingId, Count) +VALUES (@MedicinesId,@VisitingId, @Count)"; + foreach (var elem in visiting.Medicines) + { + connection.Execute(querySubInsert, new + { + visitingId, + elem.MedicinesId, + elem.Count + }, transaction); + } + transaction.Commit(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } + } public void DeleteVisiting(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" +DELETE FROM Visitings +WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public IEnumerable ReadVisiting(DateTime? dateForm = null, DateTime? dateTo = null, int? patientId = null, int? doctorId = null,int ? visitingId = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT * FROM Visitings"; + var visitings = + connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(visitings)); + return visitings; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } }