2
This commit is contained in:
parent
3cc8a5afcf
commit
fe3802d178
@ -6,16 +6,16 @@ public class Student
|
|||||||
public string FirstName { get; private set; } = string.Empty;
|
public string FirstName { get; private set; } = string.Empty;
|
||||||
public string LastName { get; private set; } = string.Empty;
|
public string LastName { get; private set; } = string.Empty;
|
||||||
public float Score { get; private set; }
|
public float Score { get; private set; }
|
||||||
//public Hostel HostelType { get; private set; }
|
|
||||||
public string DisplayName => $"{FirstName} {LastName}";
|
public string DisplayName => $"{FirstName} {LastName}";
|
||||||
|
|
||||||
public static Student CreateOperation(int id, string firstName, string lastName, float Score)
|
public static Student CreateOperation(int id, string firstName, string lastName, float score)
|
||||||
{
|
{
|
||||||
return new Student
|
return new Student
|
||||||
{
|
{
|
||||||
Id = id,
|
Id = id,
|
||||||
FirstName = firstName,
|
FirstName = firstName,
|
||||||
LastName = lastName
|
LastName = lastName,
|
||||||
|
Score = score
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<root>
|
<root>
|
||||||
<!--
|
<!--
|
||||||
Microsoft ResX Schema
|
Microsoft ResX Schema
|
||||||
|
|
||||||
Version 2.0
|
Version 2.0
|
||||||
|
|
||||||
@ -48,7 +48,7 @@
|
|||||||
value : The object must be serialized with
|
value : The object must be serialized with
|
||||||
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
|
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
|
||||||
: and then encoded with base64 encoding.
|
: and then encoded with base64 encoding.
|
||||||
|
|
||||||
mimetype: application/x-microsoft.net.object.soap.base64
|
mimetype: application/x-microsoft.net.object.soap.base64
|
||||||
value : The object must be serialized with
|
value : The object must be serialized with
|
||||||
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||||
|
11
Project/Project/Forms/StatementForm.Designer.cs
generated
11
Project/Project/Forms/StatementForm.Designer.cs
generated
@ -133,8 +133,15 @@ namespace Project.Forms
|
|||||||
//
|
//
|
||||||
// ColumnMark
|
// ColumnMark
|
||||||
//
|
//
|
||||||
ColumnMark.DataPropertyName = "Enum.GetValues(typeof(Mark));";
|
ColumnMark.DataPropertyName = "Enum.GetNames(typeof(Mark));";
|
||||||
ColumnMark.DataSource = Enum.GetValues(typeof(Mark));
|
ColumnMark.DataSource = new Mark[]
|
||||||
|
{
|
||||||
|
Mark.One,
|
||||||
|
Mark.Two,
|
||||||
|
Mark.Three,
|
||||||
|
Mark.Four,
|
||||||
|
Mark.Five
|
||||||
|
};
|
||||||
ColumnMark.HeaderText = "Оценки";
|
ColumnMark.HeaderText = "Оценки";
|
||||||
ColumnMark.Name = "ColumnMark";
|
ColumnMark.Name = "ColumnMark";
|
||||||
ColumnMark.Resizable = DataGridViewTriState.True;
|
ColumnMark.Resizable = DataGridViewTriState.True;
|
||||||
|
@ -29,6 +29,8 @@ public partial class StatementForm : Form
|
|||||||
ColumnStudent.DataSource = studentRepository.GetAllStudents();
|
ColumnStudent.DataSource = studentRepository.GetAllStudents();
|
||||||
ColumnStudent.DisplayMember = "DisplayName";
|
ColumnStudent.DisplayMember = "DisplayName";
|
||||||
ColumnStudent.ValueMember = "Id";
|
ColumnStudent.ValueMember = "Id";
|
||||||
|
|
||||||
|
ColumnMark.DataSource = Enum.GetNames(typeof(Mark));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ButtonSave_Click(object sender, EventArgs e)
|
private void ButtonSave_Click(object sender, EventArgs e)
|
||||||
@ -65,9 +67,20 @@ public partial class StatementForm : Form
|
|||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
list.Add(StatementStudent.CreateOperation(0,
|
if (Enum.TryParse<Mark>(row.Cells["ColumnMark"].Value.ToString(), out var mark))
|
||||||
Convert.ToInt32(row.Cells["ColumnStudent"].Value),
|
{
|
||||||
(Mark)Convert.ToInt32(row.Cells["ColumnMark"].Value)));
|
list.Add(StatementStudent.CreateOperation(
|
||||||
|
0,
|
||||||
|
Convert.ToInt32(row.Cells["ColumnStudent"].Value),
|
||||||
|
mark));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox.Show($"Недопустимое значение оценки: {row.Cells["ColumnMark"].Value}",
|
||||||
|
"Ошибка",
|
||||||
|
MessageBoxButtons.OK,
|
||||||
|
MessageBoxIcon.Warning);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
@ -120,7 +120,7 @@
|
|||||||
<metadata name="ColumnStudent.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
<metadata name="ColumnStudent.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
||||||
<value>True</value>
|
<value>True</value>
|
||||||
</metadata>
|
</metadata>
|
||||||
<metadata name="Оценки.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
<metadata name="ColumnMark.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
||||||
<value>True</value>
|
<value>True</value>
|
||||||
</metadata>
|
</metadata>
|
||||||
</root>
|
</root>
|
@ -94,4 +94,4 @@
|
|||||||
private Button ButtonAdd;
|
private Button ButtonAdd;
|
||||||
private DataGridView dataGridViewData;
|
private DataGridView dataGridViewData;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -70,7 +70,7 @@ public partial class StudentForm : Form
|
|||||||
|
|
||||||
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
|
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
|
||||||
|
|
||||||
private Student CreateStudent(int id) => Student.CreateOperation(
|
private Student CreateStudent(int id) => Student.CreateOperation (
|
||||||
id,
|
id,
|
||||||
textBoxName.Text,
|
textBoxName.Text,
|
||||||
textBoxSurname.Text,(float)numericUpDownScore.Value!);
|
textBoxSurname.Text,(float)numericUpDownScore.Value!);
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
using Unity.Lifetime;
|
|
||||||
using Unity.Microsoft.Logging;
|
using Unity.Microsoft.Logging;
|
||||||
using Unity;
|
using Unity;
|
||||||
using Project.Repositories.Implementations;
|
using Project.Repositories.Implementations;
|
||||||
using Project.Repositories;
|
using Project.Repositories;
|
||||||
using Project.Entities;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
namespace Project
|
namespace Project
|
||||||
{
|
{
|
||||||
@ -24,14 +25,26 @@ namespace Project
|
|||||||
private static IUnityContainer CreateContainer()
|
private static IUnityContainer CreateContainer()
|
||||||
{
|
{
|
||||||
var container = new UnityContainer();
|
var container = new UnityContainer();
|
||||||
|
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||||
container.RegisterType<IStudentRepository, StudentRepository>();
|
container.RegisterType<IStudentRepository, StudentRepository>();
|
||||||
container.RegisterType<IDecreeRepository, DecreeRepository>();
|
container.RegisterType<IDecreeRepository, DecreeRepository>();
|
||||||
container.RegisterType<IStatementRepository, StatementRepository>();
|
container.RegisterType<IStatementRepository, StatementRepository>();
|
||||||
container.RegisterType<ISubjectRepository, SubjectRepository>();
|
container.RegisterType<ISubjectRepository, SubjectRepository>();
|
||||||
container.RegisterType<ITeacherRepository, TeacherRepository>();
|
container.RegisterType<ITeacherRepository, TeacherRepository>();
|
||||||
|
container.RegisterType<IConnectionString, ConnectionString>();
|
||||||
return container;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
12
Project/Project/Repositories/IConnectionString.cs
Normal file
12
Project/Project/Repositories/IConnectionString.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Project.Repositories;
|
||||||
|
|
||||||
|
public interface IConnectionString
|
||||||
|
{
|
||||||
|
public string ConnectionString { get; }
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Project.Repositories.Implementations;
|
||||||
|
|
||||||
|
public class ConnectionString : IConnectionString
|
||||||
|
{
|
||||||
|
string IConnectionString.ConnectionString => "Host=localhost;Port=5432;Username=postgres;Password=admin123;Database=BD";
|
||||||
|
}
|
@ -1,16 +1,58 @@
|
|||||||
using Project.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using Project.Entities;
|
||||||
|
|
||||||
namespace Project.Repositories.Implementations;
|
namespace Project.Repositories.Implementations;
|
||||||
|
|
||||||
|
|
||||||
public class DecreeRepository : IDecreeRepository
|
public class DecreeRepository : IDecreeRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<DecreeRepository> _logger;
|
||||||
|
|
||||||
|
public DecreeRepository(IConnectionString connectionString, ILogger<DecreeRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void AddDecree(Decree decree)
|
public void AddDecree(Decree decree)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(decree));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Decree (StudentId, Name, Date, Info)
|
||||||
|
VALUES (@StudentId, @Name, @Date, @Info)";
|
||||||
|
connection.Execute(queryInsert, decree);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Decree> GetAllDecrees()
|
public IEnumerable<Decree> GetAllDecrees()
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Decree";
|
||||||
|
var decrees = connection.Query<Decree>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(decrees));
|
||||||
|
return decrees;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,76 @@
|
|||||||
using Project.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using Project.Entities;
|
||||||
|
|
||||||
namespace Project.Repositories.Implementations;
|
namespace Project.Repositories.Implementations;
|
||||||
|
|
||||||
public class StatementRepository : IStatementRepository
|
public class StatementRepository : IStatementRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<StatementRepository> _logger;
|
||||||
|
|
||||||
|
public StatementRepository(IConnectionString connectionString, ILogger<StatementRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateStatement(Statement statement)
|
public void CreateStatement(Statement statement)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(statement));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
connection.Open();
|
||||||
|
using var transaction = connection.BeginTransaction();
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Statement (TeacherId, SubjectId, Date)
|
||||||
|
VALUES (@TeacherId, @SubjectId, @Date);
|
||||||
|
SELECT MAX(Id) FROM Statement";
|
||||||
|
var statementId =
|
||||||
|
connection.QueryFirst<int>(queryInsert, statement, transaction);
|
||||||
|
var querySubInsert = @"
|
||||||
|
INSERT INTO StatementStudent (StatementId, StudentId, Mark)
|
||||||
|
VALUES (@StatementId, @StudentId, @Mark)";
|
||||||
|
foreach (var elem in statement.StatementStudents)
|
||||||
|
{
|
||||||
|
connection.Execute(querySubInsert, new
|
||||||
|
{
|
||||||
|
statementId,
|
||||||
|
elem.StudentId,
|
||||||
|
elem.Mark
|
||||||
|
}, transaction);
|
||||||
|
}
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Statement> GetAllStatements()
|
public IEnumerable<Statement> GetAllStatements()
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Statement";
|
||||||
|
var statements = connection.Query<Statement>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(statements));
|
||||||
|
return statements;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,28 +1,124 @@
|
|||||||
using Project.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using Project.Entities;
|
||||||
|
|
||||||
namespace Project.Repositories.Implementations;
|
namespace Project.Repositories.Implementations;
|
||||||
|
|
||||||
public class StudentRepository : IStudentRepository
|
public class StudentRepository : IStudentRepository
|
||||||
{
|
{
|
||||||
|
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<StudentRepository> _logger;
|
||||||
|
|
||||||
|
public StudentRepository(IConnectionString connectionString, ILogger<StudentRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void AddStudent(Student student)
|
public void AddStudent(Student student)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(student));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Student (FirstName, LastName, Score)
|
||||||
|
VALUES (@FirstName, @LastName, @Score)";
|
||||||
|
connection.Execute(queryInsert, student);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteStudent(int id)
|
public void DeleteStudent(int id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Student
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Student> GetAllStudents()
|
public IEnumerable<Student> GetAllStudents()
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Student";
|
||||||
|
var students = connection.Query<Student>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(students));
|
||||||
|
return students;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Student GetStudentById(int id)
|
public Student GetStudentById(int id)
|
||||||
{
|
{
|
||||||
return Student.CreateOperation(0, "", "", 0);
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Student
|
||||||
|
WHERE Id=@id";
|
||||||
|
var student = connection.QueryFirst<Student>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(student));
|
||||||
|
return student;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateStudent(Student student)
|
public void UpdateStudent(Student student)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(student));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Student
|
||||||
|
SET
|
||||||
|
FirstName=@FirstName,
|
||||||
|
LastName=@LastName,
|
||||||
|
Score=@Score
|
||||||
|
WHERE Id=@Id";
|
||||||
|
connection.Execute(queryUpdate, student);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,29 +1,122 @@
|
|||||||
using Project.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using Project.Entities;
|
||||||
|
|
||||||
namespace Project.Repositories.Implementations;
|
namespace Project.Repositories.Implementations;
|
||||||
|
|
||||||
|
|
||||||
public class SubjectRepository : ISubjectRepository
|
public class SubjectRepository : ISubjectRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<SubjectRepository> _logger;
|
||||||
|
|
||||||
|
public SubjectRepository(IConnectionString connectionString, ILogger<SubjectRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void AddSubject(Subject subject)
|
public void AddSubject(Subject subject)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(subject));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Subject (Name, Courses)
|
||||||
|
VALUES (@Name, @Courses)";
|
||||||
|
connection.Execute(queryInsert, subject);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteSubject(int id)
|
public void DeleteSubject(int id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Subject
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Subject> GetAllSubjects()
|
public IEnumerable<Subject> GetAllSubjects()
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Subject";
|
||||||
|
var subjects = connection.Query<Subject>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(subjects));
|
||||||
|
return subjects;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Subject GetSubjectById(int id)
|
public Subject GetSubjectById(int id)
|
||||||
{
|
{
|
||||||
return Subject.CreateOperation(0, "", Entities.Enums.Course.Third);
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Subject
|
||||||
|
WHERE Id=@id";
|
||||||
|
var subject = connection.QueryFirst<Subject>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(subject));
|
||||||
|
return subject;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateSubject(Subject subject)
|
public void UpdateSubject(Subject subject)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(subject));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Subject
|
||||||
|
SET
|
||||||
|
Name=@Name,
|
||||||
|
Courses=@Courses
|
||||||
|
WHERE Id=@Id";
|
||||||
|
connection.Execute(queryUpdate, subject);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,28 +1,122 @@
|
|||||||
using Project.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using Project.Entities;
|
||||||
|
|
||||||
namespace Project.Repositories.Implementations;
|
namespace Project.Repositories.Implementations;
|
||||||
|
|
||||||
public class TeacherRepository : ITeacherRepository
|
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 AddTeacher(Teacher teacher)
|
public void AddTeacher(Teacher teacher)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(teacher));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Teacher (FirstName, LastName)
|
||||||
|
VALUES (@FirstName, @LastName)";
|
||||||
|
connection.Execute(queryInsert, teacher);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteTeacher(int id)
|
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 IEnumerable<Teacher> GetAllTeachers()
|
public IEnumerable<Teacher> GetAllTeachers()
|
||||||
{
|
{
|
||||||
return [];
|
_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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Teacher GetTeacherById(int id)
|
public Teacher GetTeacherById(int id)
|
||||||
{
|
{
|
||||||
return Teacher.CreateOperation(0, "", "");
|
_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 void UpdateTeacher(Teacher teacher)
|
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
|
||||||
|
FirstName=@FirstName,
|
||||||
|
LastName=@LastName
|
||||||
|
WHERE Id=@Id";
|
||||||
|
connection.Execute(queryUpdate, teacher);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
15
Project/Project/appsettings.json
Normal file
15
Project/Project/appsettings.json
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"Serilog": {
|
||||||
|
"Using": [ "Serilog.Sinks.File" ],
|
||||||
|
"MinimumLevel": "Debug",
|
||||||
|
"WriteTo": [
|
||||||
|
{
|
||||||
|
"Name": "File",
|
||||||
|
"Args": {
|
||||||
|
"path": "Logs/gas_log.txt",
|
||||||
|
"rollingInterval": "Day"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user