вторая почти готовая

This commit is contained in:
nezui1 2024-11-25 11:59:33 +04:00
parent ab83e06788
commit 09557899c2
17 changed files with 585 additions and 69 deletions

View File

@ -15,9 +15,9 @@ public class Accessories
public int Count { get; private set; }
public double Price { get; private set; }
public float Price { get; private set; }
public static Accessories CreateEntity(int id, AccessoriesType accessoriesType, int count, double price)
public static Accessories CreateEntity(int id, AccessoriesType accessoriesType, int count, float price)
{
return new Accessories
{

View File

@ -13,9 +13,9 @@ public class Service
public ServiceType ServiceType { get; private set; }
public double Price { get; private set; }
public float Price { get; private set; }
public static Service CreateEntity(int id, ServiceType serviceType, double price)
public static Service CreateEntity(int id, ServiceType serviceType, float price)
{
return new Service
{

View File

@ -74,15 +74,13 @@ namespace ProjectCompRepair.Forms
{
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Close();
}
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
private Accessories CreateAccessories(int id)
{
return Accessories.CreateEntity(id, (AccessoriesType)comboBoxType.SelectedItem! , (int)numericUpDownCount.Value, (double)numericUpDownPrice.Value);
private Accessories CreateAccessories(int id) => Accessories.CreateEntity(id, (AccessoriesType)comboBoxType.SelectedItem! , (int)numericUpDownCount.Value, (float)numericUpDownPrice.Value);
}
}
}

View File

@ -69,7 +69,7 @@ public partial class FormMaster : Form
{
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Close();
}
private void ButtonCancel_Click(object sender, EventArgs e) => Close();

View File

@ -77,7 +77,7 @@ public partial class FormOrder : Form
{
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Close();
}
private void ButtonCancel_Click(object sender, EventArgs e) => Close();

View File

@ -100,7 +100,7 @@ namespace ProjectCompRepair.Forms
{
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Close();
}
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
@ -112,7 +112,7 @@ namespace ProjectCompRepair.Forms
{
serviceType |= (ServiceType)elem;
}
return Service.CreateEntity(id, serviceType, (double)numericUpDownPrice.Value);
return Service.CreateEntity(id, serviceType, (float)numericUpDownPrice.Value);
}
}

View File

@ -59,7 +59,7 @@ namespace ProjectCompRepair.Forms
}
try
{
var form = _container.Resolve<FormMaster>();
var form = _container.Resolve<FormService>();
form.Id = findId;
form.ShowDialog();
LoadList();

View File

@ -1,7 +1,14 @@
using Microsoft.Extensions.Logging;
using ProjectCompRepair.Repositories;
using ProjectCompRepair.Repositories.Implemantations;
using Serilog;
using System;
using Unity;
using Microsoft.Extensions.Configuration;
using Unity.Microsoft.Logging;
using Unity.Lifetime;
namespace ProjectCompRepair
{
@ -23,13 +30,29 @@ namespace ProjectCompRepair
{
var container = new UnityContainer();
container.RegisterType<IAccessoriesRepository, AccessoriesRepository>();
container.RegisterType<IMasterRepository, MasterRepository>();
container.RegisterType<IOrderRepository, OrderRepository>();
container.RegisterType<IServiceRepository, ServiceRepository>();
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
container.RegisterType<IAccessoriesRepository, AccessoriesRepository>(new TransientLifetimeManager());
container.RegisterType<IMasterRepository, MasterRepository>(new TransientLifetimeManager());
container.RegisterType<IOrderRepository, OrderRepository>(new TransientLifetimeManager());
container.RegisterType<IServiceRepository, ServiceRepository>(new TransientLifetimeManager());
container.RegisterType<IConnectionString, ConnectionString >(new TransientLifetimeManager());
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;
}
}
}

View File

@ -9,8 +9,19 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Npgsql" Version="8.0.5" />
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="Microsoft.Extensions.Configuration" 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="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Npgsql" Version="9.0.1" />
<PackageReference Include="Serilog" Version="4.1.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.4" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
<PackageReference Include="Unity" Version="5.11.10" />
<PackageReference Include="Unity.Microsoft.Logging" Version="5.11.1" />
</ItemGroup>
<ItemGroup>

View File

@ -0,0 +1,19 @@
using ProjectCompRepair.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCompRepair.Repositories;
public interface IAccessoriesOrder
{
void CreateAccessoriesOrder(AccessoiresOrder accessoriesOrder);
void DeleteAccessoriesOrder(int id);
IEnumerable<AccessoiresOrder> ReadAccessoiresOrder(int? Id = 0, int? orderId = 0, int? count = 0);
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCompRepair.Repositories;
public interface IConnectionString
{
public string ConnectionString {get;}
}

View File

@ -0,0 +1,26 @@
using ProjectCompRepair.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCompRepair.Repositories.Implemantations;
public class AccessoriesOrderRepository : IAccessoriesOrder
{
public void CreateAccessoriesOrder(AccessoiresOrder accessoriesOrder)
{
throw new NotImplementedException();
}
public void DeleteAccessoriesOrder(int id)
{
throw new NotImplementedException();
}
public IEnumerable<AccessoiresOrder> ReadAccessoiresOrder(int? Id = 0, int? orderId = 0, int? count = 0)
{
throw new NotImplementedException();
}
}

View File

@ -4,33 +4,139 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Data.SqlClient;
using Dapper;
using Npgsql;
using System.Xml.Linq;
namespace ProjectCompRepair.Repositories.Implemantations;
public class AccessoriesRepository : IAccessoriesRepository
{
private readonly IConnectionString _connectionString;
private readonly ILogger<AccessoriesRepository> _logger;
public AccessoriesRepository(IConnectionString connectionString, ILogger<AccessoriesRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public void CreateAccessories(Accessories accessories)
{
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект : {json}", JsonConvert.SerializeObject(accessories));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var queryInsert = @"
INSERT INTO Accessories (AccessoriesType, Count, Price)
VALUES (@AccessoriesType, @Count, @Price)";
connection.Execute(queryInsert, accessories);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
}
public void UpdateAccessories(Accessories accessories)
{
_logger.LogInformation("Редактирование объекта");
_logger.LogDebug("Объект : {json}", JsonConvert.SerializeObject(accessories));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var queryUpdate = @"UPDATE Accessories
SET
AccessoriesType = @AccessoriesType,
Count = @Count,
Price = @Price
WHERE ID = @Id;";
connection.Execute(queryUpdate, accessories);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при редактировании объекта");
throw;
}
}
public void DeleteAccessories(int id)
{
}
public IEnumerable<Accessories> ReadAccessories()
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объект : {id}", id);
try
{
return [];
}
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var queryDelete = @"
DELETE FROM Accessories
WHERE ID = @id
"
;
connection.Execute(queryDelete, new { id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при удалении объекта");
throw;
}
}
public Accessories ReadAccessoriesById(int id)
{
return Accessories.CreateEntity(0, 0, 0, 0);
}
public void UpdateAccessories(Accessories accessories)
_logger.LogInformation("Получение объекта по идентификатору");
_logger.LogDebug("Объект : {id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var querySelect = @"
SELECT * FROM Accessories
WHERE ID = @id
";
var accessories = connection.QueryFirst<Accessories>(querySelect, new { id });
_logger.LogDebug("Найденный объект {json}", JsonConvert.SerializeObject(accessories));
return accessories;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при поиске объекта");
throw;
}
}
public IEnumerable<Accessories> ReadAccessories()
{
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var querySelect = @"SELECT * FROM Accessories";
var accessories = connection.Query<Accessories>(querySelect);
_logger.LogDebug("Полученные объекты : {json}", JsonConvert.SerializeObject(accessories));
return accessories;
}
catch(Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCompRepair.Repositories.Implemantations;
public class ConnectionString : IConnectionString
{
string IConnectionString.ConnectionString => "host=localhost;port=5432;username=postgres;password=123;Database=otp;";
}

View File

@ -1,6 +1,11 @@
using ProjectCompRepair.Entities;
using Dapper;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Npgsql;
using ProjectCompRepair.Entities;
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -9,28 +14,123 @@ namespace ProjectCompRepair.Repositories.Implemantations;
public class MasterRepository : IMasterRepository
{
private readonly IConnectionString _connectionString;
private readonly ILogger<AccessoriesRepository> _logger;
public MasterRepository(IConnectionString connectionString, ILogger<AccessoriesRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public void CreateMaster(Master master)
{
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект : {json}", JsonConvert.SerializeObject(master));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var queryInsert = @"
INSERT INTO Master (Name)
VALUES (@Name)";
connection.Execute(queryInsert, master);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
}
public void DeleteMaster(int id)
{
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объект : {id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var queryDelete = @"
DELETE FROM Master
WHERE ID = @id
"
;
connection.Execute(queryDelete, new { id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при удалении объекта");
throw;
}
}
public IEnumerable<Master> ReadMaster()
{
return [];
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var querySelect = @"SELECT * FROM Master";
var master = connection.Query<Master>(querySelect);
_logger.LogDebug("Полученные объекты : {json}", JsonConvert.SerializeObject(master));
return master;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
public Master ReadMasterById(int id)
{
return Master.CreateEntity(0, string.Empty);
_logger.LogInformation("Получение объекта по идентификатору");
_logger.LogDebug("Объект : {id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var querySelect = @"
SELECT * FROM Master
WHERE ID = @id
";
var master = connection.QueryFirst<Master>(querySelect, new { id });
_logger.LogDebug("Найденный объект {json}", JsonConvert.SerializeObject(master));
return master;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при поиске объекта");
throw;
}
}
public void UpdateMaster(Master master)
{
_logger.LogInformation("Редактирование объекта");
_logger.LogDebug("Объект : {json}", JsonConvert.SerializeObject(master));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var queryUpdate = @"UPDATE Master
SET
Name = @Name
WHERE ID = @Id;";
connection.Execute(queryUpdate, master);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при редактировании объекта");
throw;
}
}
}

View File

@ -1,4 +1,8 @@
using ProjectCompRepair.Entities;
using Dapper;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Npgsql;
using ProjectCompRepair.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
@ -9,29 +13,131 @@ namespace ProjectCompRepair.Repositories.Implemantations;
public class OrderRepository : IOrderRepository
{
private readonly IConnectionString _connectionString;
private readonly ILogger<AccessoriesRepository> _logger;
public OrderRepository(IConnectionString connectionString, ILogger<AccessoriesRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public void CreateOrder(Order order)
{
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект : {json}", JsonConvert.SerializeObject(order));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var queryInsert = @"
INSERT INTO Order (Name, Coment, Date, MasterId, AccessoiresOrders, ServicesOrders)
VALUES (@Name, @Coment, @Date, @MasterId, @AccessoiresOrders, @ServicesOrders)";
connection.Execute(queryInsert, order);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
}
public void UpdateOrder(Order order)
{
_logger.LogInformation("Редактирование объекта");
_logger.LogDebug("Объект : {json}", JsonConvert.SerializeObject(order));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var queryUpdate = @"UPDATE Order
SET
Name = @Name,
Coment = @Coment,
Price = @Price,
Date = @Date,
MasterId = @MasterId,
AccessoiresOrders = @AccessoiresOrders,
ServicesOrders = @ServicesOrders
WHERE ID = @Id;";
connection.Execute(queryUpdate, order);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при редактировании объекта");
throw;
}
}
public void DeleteOrder(int id)
{
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объект : {id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var queryDelete = @"
DELETE FROM Order
WHERE ID = @id
"
;
connection.Execute(queryDelete, new { id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при удалении объекта");
throw;
}
}
public Order ReadOrderById(int id)
{
_logger.LogInformation("Получение объекта по идентификатору");
_logger.LogDebug("Объект : {id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var querySelect = @"
SELECT * FROM Order
WHERE ID = @id
";
var order = connection.QueryFirst<Order>(querySelect, new { id });
_logger.LogDebug("Найденный объект {json}", JsonConvert.SerializeObject(order));
return order;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при поиске объекта");
throw;
}
}
public IEnumerable<Order> ReadOrder()
{
return [];
}
public Order ReadOrderById(int id)
_logger.LogInformation("Получение всех объектов");
try
{
return Order.CreateElement(0, string.Empty, string.Empty, 0);
}
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
public void UpdateOrder(Order order)
var querySelect = @"SELECT * FROM Order";
var order = connection.Query<Order>(querySelect);
_logger.LogDebug("Полученные объекты : {json}", JsonConvert.SerializeObject(order));
return order;
}
catch (Exception ex)
{
throw new NotImplementedException();
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
}

View File

@ -1,4 +1,8 @@
using ProjectCompRepair.Entities;
using Dapper;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Npgsql;
using ProjectCompRepair.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
@ -9,28 +13,124 @@ namespace ProjectCompRepair.Repositories.Implemantations;
public class ServiceRepository : IServiceRepository
{
private readonly IConnectionString _connectionString;
private readonly ILogger<AccessoriesRepository> _logger;
public ServiceRepository(IConnectionString connectionString, ILogger<AccessoriesRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public void CreateService(Service service)
{
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект : {json}", JsonConvert.SerializeObject(service));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var queryInsert = @"
INSERT INTO Service (ServiceType, Price)
VALUES (@ServiceType, @Price)";
connection.Execute(queryInsert, service);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
}
public void UpdateService(Service service)
{
_logger.LogInformation("Редактирование объекта");
_logger.LogDebug("Объект : {json}", JsonConvert.SerializeObject(service));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var queryUpdate = @"UPDATE Service
SET
ServiceType = @ServiceType,
Price = @Price
WHERE ID = @Id;";
connection.Execute(queryUpdate, service);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при редактировании объекта");
throw;
}
}
public void DeleteService(int id)
{
}
public IEnumerable<Service> ReadService()
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объект : {id}", id);
try
{
return [];
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var queryDelete = @"
DELETE FROM Service
WHERE ID = @id
"
;
connection.Execute(queryDelete, new { id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при удалении объекта");
throw;
}
}
public Service ReadServiceById(int id)
{
return Service.CreateEntity(0, Entities.Enums.ServiceType.None, 0);
}
public void UpdateService(Service service)
_logger.LogInformation("Получение объекта по идентификатору");
_logger.LogDebug("Объект : {id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var querySelect = @"
SELECT * FROM Service
WHERE ID = @id
";
var service = connection.QueryFirst<Service>(querySelect, new { id });
_logger.LogDebug("Найденный объект {json}", JsonConvert.SerializeObject(service));
return service;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при поиске объекта");
throw;
}
}
public IEnumerable<Service> ReadService()
{
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var querySelect = @"SELECT * FROM Service";
var service = connection.Query<Service>(querySelect);
_logger.LogDebug("Полученные объекты : {json}", JsonConvert.SerializeObject(service));
return service;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
}
}