2024-12-13 02:45:26 +04:00

87 lines
3.5 KiB
C#

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;
using System.Text;
using System.Threading.Tasks;
namespace ProjectPolyclinic.Repositories.Implementations
{
public class ProfessionalDevelopmentRepository : IProfessionalDevelopmentRepository
{
private readonly IConnectionString _connectionString;
private readonly ILogger<ProfessionalDevelopmentRepository> _logger;
public ProfessionalDevelopmentRepository(IConnectionString connectionString, ILogger<ProfessionalDevelopmentRepository> 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<ProfessionalDevelopment> ReadProfessionalDevelopment(DateTime? dateForm = null, DateTime? dateTo = null, int? doctorId = null, int? Id = null)
{
/*_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new
NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = "SELECT * FROM ProfessionalDevelopments";
var professionalDevelopments =
connection.Query<ProfessionalDevelopment>(querySelect);
_logger.LogDebug("Полученные объекты: {json}",
JsonConvert.SerializeObject(professionalDevelopments));
return professionalDevelopments;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}*/
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"
SELECT pd.*, CONCAT(d.First_Name, ' ', d.Last_Name) as DoctorName
FROM ProfessionalDevelopments pd
LEFT JOIN Doctors d ON d.Id = pd.DoctorId";
var professionalDevelopments =
connection.Query<ProfessionalDevelopment>(querySelect);
_logger.LogDebug("Полученные объекты: {json}",
JsonConvert.SerializeObject(professionalDevelopments));
return professionalDevelopments;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
}
}