принцип понятен, скоро будет бум

This commit is contained in:
Baryshev Dmitry 2024-12-02 23:02:30 +04:00
parent 0942dd8be3
commit 6030309361
5 changed files with 112 additions and 13 deletions

View File

@ -19,14 +19,13 @@ public class Driver
public int TruckId { get;private set; } public int TruckId { get;private set; }
public static Driver CreateDriver(int id, string fname, string lname ,string phone_num, int tryckid) public static Driver CreateDriver(int id, string fname, string lname, int tryckid)
{ {
return new Driver return new Driver
{ {
Id = id, Id = id,
First_name = fname, First_name = fname,
Last_name = lname, Last_name = lname,
Phone_Number = phone_num ?? string.Empty,
TruckId = tryckid TruckId = tryckid
}; };
} }

View File

@ -13,6 +13,8 @@
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Npgsql" Version="9.0.1" />
<PackageReference Include="Serilog" Version="4.0.2" /> <PackageReference Include="Serilog" Version="4.0.2" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" /> <PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.4" /> <PackageReference Include="Serilog.Settings.Configuration" Version="8.0.4" />

View File

@ -1,6 +1,11 @@
using ProjectGarage.Entities; using Dapper;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Npgsql;
using ProjectGarage.Entities;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -11,24 +16,117 @@ public class DriverRepository : IDriverRepository
{ {
public readonly IConnectionString _connectionString; public readonly IConnectionString _connectionString;
public DriverRepository(IConnectionString connectionString) private readonly ILogger<DriverRepository> _logger;
public DriverRepository(IConnectionString connectionString, ILogger<DriverRepository> logger)
{ {
_connectionString = connectionString; _connectionString = connectionString;
_logger = logger;
} }
public void CreateDriver(Driver driver) public void CreateDriver(Driver driver)
{ {
_logger.LogInformation("Добавление объект");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(driver));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var queryInsert = @"
INSERT INTO driver (driver_fname, driver_lname, id_truck) VALUES
(@First_name, @Last_name, @TruckID);";
connection.Execute(queryInsert, driver);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
} }
public void DeleteDriver(int id)
{
}
public Driver ReadDriverByID(int id) => Driver.CreateDriver(0, string.Empty, string.Empty, string.Empty, 0);
public IEnumerable<Driver> ReadDrivers() => [];
public void UpdateDriver(Driver driver) public void UpdateDriver(Driver driver)
{ {
_logger.LogInformation("Редактирование объекта");
_logger.LogDebug("Объект: {json}",
JsonConvert.SerializeObject(driver));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var queryUpdate = @"
UPDATE driver
SET
driver_fname=@First_name,
driver_lname=@Last_name,
id_truck=@TruckID
WHERE id_driver=@Id";
connection.Execute(queryUpdate, driver);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при редактировании объекта");
throw;
}
}
public void DeleteDriver(int driverId)
{
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объект: {id}", driverId);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var queryDelete = @"
DELETE FROM driver
WHERE id_driver=@id";
connection.Execute(queryDelete, new { driverId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при удалении объекта");
throw;
}
}
public Driver ReadDriverByID(int driverId)
{
_logger.LogInformation("Получение объекта по идентификатору");
_logger.LogDebug("Объект: {id}", driverId);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"
SELECT * FROM route_list
WHERE id=@routeListId";
var driver = connection.QueryFirst<Driver>(querySelect, new { driverId });
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(driver));
return driver;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при поиске объекта");
throw;
}
}
public IEnumerable<Driver> ReadDrivers()
{
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT * FROM driver";
var driver = connection.Query<Driver>(querySelect);
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(driver));
return driver;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
} }
} }

View File

@ -6,7 +6,7 @@
{ {
"Name": "File", "Name": "File",
"Args": { "Args": {
"path": "C\\Users\\dimoo\\OneDrive\\Рабочий стол\\Учеба\\2 курс\\Лабы 2 курс\\ОТП\\garage.txt", "path": "Logs/garage.txt",
"rollingInterval": "Day" "rollingInterval": "Day"
} }
} }

View File