2025-04-22 07:25:24 +00:00
using Demka_Snova_1.Hardik.Conect.Dao ;
using Npgsql ;
using System ;
using System.Collections.Generic ;
namespace Demka_Snova_1.Hardik.Conect
{
public class DatabaseConnection : IDisposable
{
private readonly NpgsqlConnection _connection ;
public DatabaseConnection ( )
{
string connectionString = "Server=45.67.56.214;Port=5421;Database=user16;User Id=user16;Password=dZ28IVE5;" ;
_connection = new NpgsqlConnection ( connectionString ) ;
_connection . Open ( ) ;
}
public List < SotrudnikDao > GetAllSotrudniki ( )
{
var sotrudniki = new List < SotrudnikDao > ( ) ;
using ( var cmd = new NpgsqlCommand ( "SELECT * FROM \"Demo_Blago\".\"Sotrudnik\"" , _connection ) )
using ( var reader = cmd . ExecuteReader ( ) )
{
while ( reader . Read ( ) )
{
sotrudniki . Add ( new SotrudnikDao
{
ID = reader . GetInt32 ( reader . GetOrdinal ( "ID" ) ) ,
Pozition = reader . GetString ( reader . GetOrdinal ( "Pozition" ) ) ,
Fio = reader . GetString ( reader . GetOrdinal ( "Fio" ) ) ,
Login = reader . GetString ( reader . GetOrdinal ( "Login" ) ) ,
Pass = reader . GetString ( reader . GetOrdinal ( "Pass" ) ) ,
LaspEnt = reader . GetString ( reader . GetOrdinal ( "LaspEnt" ) ) ,
TipeEnt = reader . GetString ( reader . GetOrdinal ( "TipeEnt" ) )
} ) ;
}
}
return sotrudniki ;
}
public List < KlientDao > GetAllKlienti ( )
{
var klienti = new List < KlientDao > ( ) ;
using ( var cmd = new NpgsqlCommand ( "SELECT * FROM \"Demo_Blago\".Klient" , _connection ) )
using ( var reader = cmd . ExecuteReader ( ) )
{
while ( reader . Read ( ) )
{
klienti . Add ( new KlientDao
{
Fio = reader . GetString ( reader . GetOrdinal ( "FIO" ) ) ,
Code = reader . GetInt32 ( reader . GetOrdinal ( "Code" ) ) ,
Passport = reader . GetString ( reader . GetOrdinal ( "Pasport" ) ) ,
Birth_Day = reader . GetDateTime ( reader . GetOrdinal ( "Birth_Day" ) ) ,
Adres = reader . GetString ( reader . GetOrdinal ( "Adres" ) ) ,
Email = reader . GetString ( reader . GetOrdinal ( "Email" ) ) ,
password = reader . GetString ( reader . GetOrdinal ( "password" ) )
} ) ;
}
}
return klienti ;
}
public List < ordersDao > GetAllOrders ( )
{
var orders = new List < ordersDao > ( ) ;
using ( var cmd = new NpgsqlCommand ( "SELECT o.*, k.\"FIO\" as klient_fio FROM \"Demo_Blago\".orders o LEFT JOIN \"Demo_Blago\".\"Klient\" k ON o.klient = k.\"Code\"" , _connection ) )
using ( var reader = cmd . ExecuteReader ( ) )
{
while ( reader . Read ( ) )
{
orders . Add ( new ordersDao
{
ID = reader . GetInt32 ( reader . GetOrdinal ( "ID" ) ) ,
CodeZakaz = reader . GetString ( reader . GetOrdinal ( "code" ) ) ,
Date = DateOnly . FromDateTime ( reader . GetDateTime ( reader . GetOrdinal ( "sozdanie" ) ) ) ,
2025-04-23 07:04:16 +00:00
Time = TimeOnly . FromTimeSpan ( reader . GetTimeSpan ( reader . GetOrdinal ( "time" ) ) ) ,
2025-04-22 07:25:24 +00:00
CodeClient = reader . IsDBNull ( reader . GetOrdinal ( "klient" ) ) ? null : reader . GetInt32 ( reader . GetOrdinal ( "klient" ) ) . ToString ( ) ,
Usluga = reader . GetString ( reader . GetOrdinal ( "Usluga" ) ) ,
Status = reader . GetString ( reader . GetOrdinal ( "Status" ) ) ,
DateClose = reader . IsDBNull ( reader . GetOrdinal ( "Zakritie" ) ) ? null : DateOnly . FromDateTime ( reader . GetDateTime ( reader . GetOrdinal ( "Zakritie" ) ) ) ,
Prokat = reader . GetDecimal ( reader . GetOrdinal ( "prokat" ) ) ,
klient = new KlientDao
{
Fio = reader . IsDBNull ( reader . GetOrdinal ( "klient_fio" ) ) ? null : reader . GetString ( reader . GetOrdinal ( "klient_fio" ) )
}
} ) ;
}
}
return orders ;
}
public List < uslugiDao > GetAllUslugi ( )
{
var uslugi = new List < uslugiDao > ( ) ;
using ( var cmd = new NpgsqlCommand ( "SELECT * FROM \"Demo_Blago\".uslugi" , _connection ) )
using ( var reader = cmd . ExecuteReader ( ) )
{
while ( reader . Read ( ) )
{
uslugi . Add ( new uslugiDao
{
id = int . Parse ( reader . GetString ( reader . GetOrdinal ( "id" ) ) ) ,
Name = reader . GetString ( reader . GetOrdinal ( "name" ) ) ,
Code = reader . GetString ( reader . GetOrdinal ( "code" ) ) ,
PrisePerH = reader . GetInt32 ( reader . GetOrdinal ( "prisePerH" ) )
} ) ;
}
}
return uslugi ;
}
public List < historyDao > GetAllHistory ( )
{
var history = new List < historyDao > ( ) ;
using ( var cmd = new NpgsqlCommand ( "SELECT * FROM \"Demo_Blago\".history" , _connection ) )
using ( var reader = cmd . ExecuteReader ( ) )
{
while ( reader . Read ( ) )
{
history . Add ( new historyDao
{
id = reader . GetInt32 ( reader . GetOrdinal ( "id" ) ) ,
user = reader . GetString ( reader . GetOrdinal ( "User" ) ) ,
Vhod = reader . GetDateTime ( reader . GetOrdinal ( "vrema" ) ) ,
status = reader . GetString ( reader . GetOrdinal ( "status" ) )
} ) ;
}
}
return history ;
}
public void AddSotrudnik ( SotrudnikDao sotrudnik )
{
using ( var cmd = new NpgsqlCommand (
"INSERT INTO \"Demo_Blago\".\"Sotrudnik\" " +
"(\"ID\", \"Pozition\", \"Fio\", \"Login\", \"Pass\", \"LaspEnt\", \"TipeEnt\") " +
"VALUES (@id, @pozition, @fio, @login, @pass, @laspEnt, @tipeEnt)" , _connection ) )
{
cmd . Parameters . AddWithValue ( "id" , sotrudnik . ID ) ;
cmd . Parameters . AddWithValue ( "pozition" , sotrudnik . Pozition ) ;
cmd . Parameters . AddWithValue ( "fio" , sotrudnik . Fio ) ;
cmd . Parameters . AddWithValue ( "login" , sotrudnik . Login ) ;
cmd . Parameters . AddWithValue ( "pass" , sotrudnik . Pass ) ;
cmd . Parameters . AddWithValue ( "laspEnt" , sotrudnik . LaspEnt ) ;
cmd . Parameters . AddWithValue ( "tipeEnt" , sotrudnik . TipeEnt ) ;
cmd . ExecuteNonQuery ( ) ;
}
}
public void UpdateSotrudnik ( SotrudnikDao sotrudnik )
{
using ( var cmd = new NpgsqlCommand (
"UPDATE \"Demo_Blago\".\"Sotrudnik\" SET " +
"\"Pozition\" = @pozition, \"Fio\" = @fio, \"Login\" = @login, " +
"\"Pass\" = @pass, \"LaspEnt\" = @laspEnt, \"TipeEnt\" = @tipeEnt " +
"WHERE \"ID\" = @id" , _connection ) )
{
cmd . Parameters . AddWithValue ( "id" , sotrudnik . ID ) ;
cmd . Parameters . AddWithValue ( "pozition" , sotrudnik . Pozition ) ;
cmd . Parameters . AddWithValue ( "fio" , sotrudnik . Fio ) ;
cmd . Parameters . AddWithValue ( "login" , sotrudnik . Login ) ;
cmd . Parameters . AddWithValue ( "pass" , sotrudnik . Pass ) ;
cmd . Parameters . AddWithValue ( "laspEnt" , sotrudnik . LaspEnt ) ;
cmd . Parameters . AddWithValue ( "tipeEnt" , sotrudnik . TipeEnt ) ;
cmd . ExecuteNonQuery ( ) ;
}
}
public void DeleteSotrudnik ( int id )
{
using ( var cmd = new NpgsqlCommand (
"DELETE FROM \"Demo_Blago\".\"Sotrudnik\" WHERE \"ID\" = @id" , _connection ) )
{
cmd . Parameters . AddWithValue ( "id" , id ) ;
cmd . ExecuteNonQuery ( ) ;
}
}
public void AddOrder ( ordersDao order )
{
using ( var cmd = new NpgsqlCommand (
"INSERT INTO \"Demo_Blago\".orders " +
"(\"ID\", \"code\", \"sozdanie\", \"time\", \"klient\", \"Usluga\", \"Status\", \"Zakritie\", \"prokat\") " +
"VALUES (@id, @code, @sozdanie, @time, @klient, @usluga, @status, @zakritie, @prokat)" , _connection ) )
{
cmd . Parameters . AddWithValue ( "id" , order . ID ) ;
cmd . Parameters . AddWithValue ( "code" , order . CodeZakaz ) ;
cmd . Parameters . AddWithValue ( "sozdanie" , order . Date . ToDateTime ( TimeOnly . MinValue ) ) ;
cmd . Parameters . AddWithValue ( "time" , order . Time . ToTimeSpan ( ) ) ;
cmd . Parameters . AddWithValue ( "klient" , int . Parse ( order . CodeClient ) ) ;
cmd . Parameters . AddWithValue ( "usluga" , order . Usluga ) ;
cmd . Parameters . AddWithValue ( "status" , order . Status ) ;
cmd . Parameters . AddWithValue ( "zakritie" , order . DateClose ? . ToDateTime ( TimeOnly . MinValue ) ? ? ( object ) DBNull . Value ) ;
cmd . Parameters . AddWithValue ( "prokat" , order . Prokat ) ;
cmd . ExecuteNonQuery ( ) ;
}
}
public void Dispose ( )
{
_connection ? . Close ( ) ;
_connection ? . Dispose ( ) ;
}
}
}