ПИбд-23. Иштуганов А.М. Вторая лабораторная #2
@ -1,7 +1,11 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ProjectSchedule.Repositories;
|
||||
using ProjectSchedule.Repositories.Implementations;
|
||||
using Serilog;
|
||||
using Unity;
|
||||
using Unity.Lifetime;
|
||||
using Unity.Microsoft.Logging;
|
||||
|
||||
namespace ProjectSchedule
|
||||
{
|
||||
@ -22,13 +26,27 @@ namespace ProjectSchedule
|
||||
private static IUnityContainer CreateUnityContainer()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||
container.RegisterType<IAudienceRepository, AudienceRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<ITeacherRepository, TeacherRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IGroupRepository, GroupRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IStudingPlanSupplementRepository, StudingPlanSupplementRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IDisciplineRepository, DisciplineRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IScheduleRepository, ScheduleRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IConnectionString, ConnectionString>(new SingletonLifetimeManager());
|
||||
return container;
|
||||
}
|
||||
|
||||
private static LoggerFactory CreateLoggerFactory()
|
||||
{
|
||||
var loggerFactory = new LoggerFactory();
|
||||
loggerFactory.AddSerilog(new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json")
|
||||
.Build())
|
||||
.CreateLogger());
|
||||
return loggerFactory;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
namespace ProjectSchedule.Repositories;
|
||||
|
||||
public interface IConnectionString
|
||||
{
|
||||
public string ConnectionString { get; }
|
||||
}
|
@ -1,29 +1,120 @@
|
||||
using ProjectSchedule.Entities;
|
||||
using ProjectSchedule.Entities.Enums;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectSchedule.Entities;
|
||||
|
||||
namespace ProjectSchedule.Repositories.Implementations;
|
||||
|
||||
public class AudienceRepository : IAudienceRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<AudienceRepository> _logger;
|
||||
|
||||
public AudienceRepository(IConnectionString connectionString, ILogger<AudienceRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateAudience(Audience audience)
|
||||
{
|
||||
}
|
||||
|
||||
public void DeleteAudience(int id)
|
||||
{
|
||||
}
|
||||
|
||||
public Audience ReadAudienceById(int id)
|
||||
{
|
||||
return Audience.CreateEntity(0, string.Empty, TypeAudience.None, 0);
|
||||
}
|
||||
|
||||
public IEnumerable<Audience> ReadAudiences()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(audience));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Audience (Number, TypeAudience, SeatsCount)
|
||||
VALUES (@Number, @TypeAudience, @SeatsCount)";
|
||||
connection.Execute(queryInsert, audience);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateAudience(Audience audience)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(audience));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Audience
|
||||
SET
|
||||
Number=@Number,
|
||||
TypeAudience=@TypeAudience,
|
||||
SeatsCount=@SeatsCount
|
||||
WHERE Id=@Id";
|
||||
connection.Execute(queryUpdate, audience);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteAudience(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Audience
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Audience ReadAudienceById(int id)
|
||||
{
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Audience
|
||||
WHERE Id=@id";
|
||||
var audience = connection.QueryFirst<Audience>(querySelect, new { id });
|
||||
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(audience));
|
||||
return audience;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Audience> ReadAudiences()
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Audience";
|
||||
var audiences = connection.Query<Audience>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(audiences));
|
||||
return audiences;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
namespace ProjectSchedule.Repositories.Implementations;
|
||||
|
||||
internal class ConnectionString : IConnectionString
|
||||
{
|
||||
string IConnectionString.ConnectionString => "Host=127.0.0.1;Database=postgres;Username=postgres;Password=ishtuganov";
|
||||
}
|
@ -1,28 +1,118 @@
|
||||
using ProjectSchedule.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectSchedule.Entities;
|
||||
|
||||
namespace ProjectSchedule.Repositories.Implementations;
|
||||
|
||||
public class DisciplineRepository : IDisciplineRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<DisciplineRepository> _logger;
|
||||
|
||||
public DisciplineRepository(IConnectionString connectionString, ILogger<DisciplineRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateDiscipline(Discipline discipline)
|
||||
{
|
||||
}
|
||||
|
||||
public void DeleteDiscipline(int id)
|
||||
{
|
||||
}
|
||||
|
||||
public Discipline ReadDisciplineById(int id)
|
||||
{
|
||||
return Discipline.CreateEntity(0, string.Empty);
|
||||
}
|
||||
|
||||
public IEnumerable<Discipline> ReadDisciplines()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(discipline));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Discipline (Name)
|
||||
VALUES (@Name)";
|
||||
connection.Execute(queryInsert, discipline);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateDiscipline(Discipline discipline)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(discipline));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Discipline
|
||||
SET
|
||||
Name=@Name
|
||||
WHERE Id=@Id";
|
||||
connection.Execute(queryUpdate, discipline);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteDiscipline(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Discipline
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Discipline ReadDisciplineById(int id)
|
||||
{
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Discipline
|
||||
WHERE Id=@id";
|
||||
var discipline = connection.QueryFirst<Discipline>(querySelect, new { id });
|
||||
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(discipline));
|
||||
return discipline;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Discipline> ReadDisciplines()
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Discipline";
|
||||
var disciplines = connection.Query<Discipline>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(disciplines));
|
||||
return disciplines;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,24 +1,120 @@
|
||||
using ProjectSchedule.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectSchedule.Entities;
|
||||
|
||||
namespace ProjectSchedule.Repositories.Implementations;
|
||||
|
||||
public class GroupRepository : IGroupRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<GroupRepository> _logger;
|
||||
|
||||
public GroupRepository(IConnectionString connectionString, ILogger<GroupRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateGroup(Group group)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(group));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Groups (Name, GroupNumber, StudentsCount)
|
||||
VALUES (@Name, @GroupNumber, @StudentsCount)";
|
||||
connection.Execute(queryInsert, group);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public void DeleteGroup(int id)
|
||||
{
|
||||
}
|
||||
public Group ReadGroupById(int id)
|
||||
{
|
||||
return Group.CreateEntity(0, string.Empty, string.Empty, 0);
|
||||
}
|
||||
public IEnumerable<Group> ReadGroups()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public void UpdateGroup(Group group)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(group));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Groups
|
||||
SET
|
||||
Name=@Name,
|
||||
GroupNumber=@GroupNumber,
|
||||
StudentsCount=@StudentsCount
|
||||
WHERE Id=@Id";
|
||||
connection.Execute(queryUpdate, group);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteGroup(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Groups
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Group ReadGroupById(int id)
|
||||
{
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Groups
|
||||
WHERE Id=@id";
|
||||
var group = connection.QueryFirst<Group>(querySelect, new { id });
|
||||
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(group));
|
||||
return group;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Group> ReadGroups()
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Groups";
|
||||
var groups = connection.Query<Group>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(groups));
|
||||
return groups;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,16 +1,60 @@
|
||||
using ProjectSchedule.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectSchedule.Entities;
|
||||
|
||||
namespace ProjectSchedule.Repositories.Implementations;
|
||||
|
||||
public class ScheduleRepository : IScheduleRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<ScheduleRepository> _logger;
|
||||
|
||||
public ScheduleRepository(IConnectionString connectionString, ILogger<ScheduleRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateSchedule(Schedule schedule)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(schedule));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Schedule (TeacherId, DisciplineId, GroupStudentsId, AudienceId, DateDay,
|
||||
TypeWeek, NumberDay, NumberPair, TypePair)
|
||||
VALUES (@TeacherId, @DisciplineId, @GroupStudentsId, @AudienceId, @DateDay,
|
||||
@TypeWeek, @NumberDay, @NumberPair, @TypePair)";
|
||||
connection.Execute(queryInsert, schedule);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Schedule> ReadSchedules(DateTime? dateForm = null, DateTime? dateTo = null,
|
||||
int? teacherId = null, int? disciplineId = null, int? groupStudentsId = null, int? audienceId = null)
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Schedule";
|
||||
var schedules = connection.Query<Schedule>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(schedules));
|
||||
return schedules;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,18 +1,90 @@
|
||||
using ProjectSchedule.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectSchedule.Entities;
|
||||
|
||||
namespace ProjectSchedule.Repositories.Implementations;
|
||||
|
||||
public class StudingPlanSupplementRepository : IStudingPlanSupplementRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<StudingPlanSupplementRepository> _logger;
|
||||
|
||||
public StudingPlanSupplementRepository(IConnectionString connectionString, ILogger<StudingPlanSupplementRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateStudingPlanSupplement(StudingPlanSupplement studingPlanSupplement)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(studingPlanSupplement));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
using var transaction = connection.BeginTransaction();
|
||||
var queryInsert = @"
|
||||
INSERT INTO StudingPlanSupplement (GroupId, NameStudingPlan, Semester, DateApprovePlan)
|
||||
VALUES (@GroupId, @NameStudingPlan, @Semester, @DateApprovePlan);
|
||||
SELECT MAX(Id) FROM StudingPlanSupplement";
|
||||
var studingPlanSupplementId = connection.QueryFirst<int>(queryInsert, studingPlanSupplement, transaction);
|
||||
|
||||
var querySubInsert = @"
|
||||
INSERT INTO Discipline_StudingPlanSupplement (StudingPlanSupplementId, DisciplineId, LecturesCount, PracticesCount)
|
||||
VALUES (@StudingPlanSupplementId, @DisciplineId, @LecturesCount, @PracticesCount)";
|
||||
foreach (var elem in studingPlanSupplement.DisciplineStudingPlanSupplements)
|
||||
{
|
||||
connection.Execute(querySubInsert, new { studingPlanSupplementId, elem.DisciplineId, elem.LecturesCount, elem.PracticesCount }, transaction);
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteStudingPlanSupplement(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM StudingPlanSupplement
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<StudingPlanSupplement> ReadStudingPlanSupplements(DateTime? dateForm = null, DateTime? dateTo = null,
|
||||
int? disciplineId = null, int? groupStudentsId = null)
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM StudingPlanSupplement";
|
||||
var studingPlanSupplements = connection.Query<StudingPlanSupplement>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(studingPlanSupplements));
|
||||
return studingPlanSupplements;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,24 +1,120 @@
|
||||
using ProjectSchedule.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectSchedule.Entities;
|
||||
|
||||
namespace ProjectSchedule.Repositories.Implementations;
|
||||
|
||||
public class TeacherRepository : ITeacherRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<TeacherRepository> _logger;
|
||||
|
||||
public TeacherRepository(IConnectionString connectionString, ILogger<TeacherRepository> 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.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Teacher (Surname, Name, Patronymic)
|
||||
VALUES (@Surname, @Name, @Patronymic)";
|
||||
connection.Execute(queryInsert, teacher);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public void DeleteTeacher(int id)
|
||||
{
|
||||
}
|
||||
public Teacher ReadTeacherById(int id)
|
||||
{
|
||||
return Teacher.CreateEntity(0, string.Empty, string.Empty, string.Empty);
|
||||
}
|
||||
public IEnumerable<Teacher> ReadTeachers()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public void UpdateTeacher(Teacher teacher)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(teacher));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Teacher
|
||||
SET
|
||||
Surname=@Surname,
|
||||
Name=@Name,
|
||||
Patronymic=@Patronymic
|
||||
WHERE Id=@Id";
|
||||
connection.Execute(queryUpdate, teacher);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteTeacher(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Teacher
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Teacher ReadTeacherById(int id)
|
||||
{
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Teacher
|
||||
WHERE Id=@id";
|
||||
var teacher = connection.QueryFirst<Teacher>(querySelect, new { id });
|
||||
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(teacher));
|
||||
return teacher;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Teacher> ReadTeachers()
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Teacher";
|
||||
var teachers = connection.Query<Teacher>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(teachers));
|
||||
return teachers;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
15
ProjectSchedule/ProjectSchedule/appsettings.json
Normal file
15
ProjectSchedule/ProjectSchedule/appsettings.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.File" ],
|
||||
"MinimumLevel": "Debug",
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "Logs/schedule_log.txt",
|
||||
"rollingInterval": "Day"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user