сделали управление бд
This commit is contained in:
parent
a1c449901d
commit
bb20f0e17b
73
SUBD_Car_rent/Forms/FormMain.Designer.cs
generated
Normal file
73
SUBD_Car_rent/Forms/FormMain.Designer.cs
generated
Normal file
@ -0,0 +1,73 @@
|
||||
namespace Forms
|
||||
{
|
||||
partial class FormMain
|
||||
{
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// Clean up any resources being used.
|
||||
/// </summary>
|
||||
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Windows Form Designer generated code
|
||||
|
||||
/// <summary>
|
||||
/// Required method for Designer support - do not modify
|
||||
/// the contents of this method with the code editor.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
button1 = new Button();
|
||||
dataGridView1 = new DataGridView();
|
||||
((System.ComponentModel.ISupportInitialize)dataGridView1).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
// button1
|
||||
//
|
||||
button1.Location = new Point(12, 12);
|
||||
button1.Name = "button1";
|
||||
button1.Size = new Size(75, 23);
|
||||
button1.TabIndex = 0;
|
||||
button1.Text = "button1";
|
||||
button1.UseVisualStyleBackColor = true;
|
||||
button1.Click += button1_Click;
|
||||
//
|
||||
// dataGridView1
|
||||
//
|
||||
dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
|
||||
dataGridView1.Location = new Point(12, 41);
|
||||
dataGridView1.Name = "dataGridView1";
|
||||
dataGridView1.RowTemplate.Height = 25;
|
||||
dataGridView1.Size = new Size(776, 397);
|
||||
dataGridView1.TabIndex = 1;
|
||||
//
|
||||
// FormMain
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(800, 450);
|
||||
Controls.Add(dataGridView1);
|
||||
Controls.Add(button1);
|
||||
Name = "FormMain";
|
||||
Text = "FormMain";
|
||||
((System.ComponentModel.ISupportInitialize)dataGridView1).EndInit();
|
||||
ResumeLayout(false);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private Button button1;
|
||||
private DataGridView dataGridView1;
|
||||
}
|
||||
}
|
40
SUBD_Car_rent/Forms/FormMain.cs
Normal file
40
SUBD_Car_rent/Forms/FormMain.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using database;
|
||||
|
||||
namespace Forms
|
||||
{
|
||||
public partial class FormMain : Form
|
||||
{
|
||||
|
||||
public FormMain()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
/*
|
||||
private Abstractions bd = new Implementation();
|
||||
private void button1_Click(object sender, EventArgs e)
|
||||
{
|
||||
List<Branch> branches = bd.GetBranches();
|
||||
|
||||
// Очищаем dataGridView1 перед заполнением новыми данными
|
||||
dataGridView1.Rows.Clear();
|
||||
|
||||
// Предварительно определяем столбцы, если это не было сделано ранее
|
||||
if (dataGridView1.ColumnCount == 0)
|
||||
{
|
||||
dataGridView1.Columns.Add("Id", "ID");
|
||||
dataGridView1.Columns.Add("Name", "Name");
|
||||
dataGridView1.Columns.Add("Address", "Address");
|
||||
dataGridView1.Columns.Add("Phone", "Phone");
|
||||
dataGridView1.Columns.Add("WorkingHours", "Working Hours");
|
||||
}
|
||||
|
||||
// Заполняем dataGridView1 данными из списка branches
|
||||
foreach (Branch branch in branches)
|
||||
{
|
||||
dataGridView1.Rows.Add(branch.Id, branch.Name, branch.Address, branch.Phone, branch.WorkingHours);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
120
SUBD_Car_rent/Forms/FormMain.resx
Normal file
120
SUBD_Car_rent/Forms/FormMain.resx
Normal file
@ -0,0 +1,120 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<root>
|
||||
<!--
|
||||
Microsoft ResX Schema
|
||||
|
||||
Version 2.0
|
||||
|
||||
The primary goals of this format is to allow a simple XML format
|
||||
that is mostly human readable. The generation and parsing of the
|
||||
various data types are done through the TypeConverter classes
|
||||
associated with the data types.
|
||||
|
||||
Example:
|
||||
|
||||
... ado.net/XML headers & schema ...
|
||||
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
||||
<resheader name="version">2.0</resheader>
|
||||
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
||||
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
||||
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
|
||||
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
||||
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
||||
<value>[base64 mime encoded serialized .NET Framework object]</value>
|
||||
</data>
|
||||
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
|
||||
<comment>This is a comment</comment>
|
||||
</data>
|
||||
|
||||
There are any number of "resheader" rows that contain simple
|
||||
name/value pairs.
|
||||
|
||||
Each data row contains a name, and value. The row also contains a
|
||||
type or mimetype. Type corresponds to a .NET class that support
|
||||
text/value conversion through the TypeConverter architecture.
|
||||
Classes that don't support this are serialized and stored with the
|
||||
mimetype set.
|
||||
|
||||
The mimetype is used for serialized objects, and tells the
|
||||
ResXResourceReader how to depersist the object. This is currently not
|
||||
extensible. For a given mimetype the value must be set accordingly:
|
||||
|
||||
Note - application/x-microsoft.net.object.binary.base64 is the format
|
||||
that the ResXResourceWriter will generate, however the reader can
|
||||
read any of the formats listed below.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.binary.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.soap.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.bytearray.base64
|
||||
value : The object must be serialized into a byte array
|
||||
: using a System.ComponentModel.TypeConverter
|
||||
: and then encoded with base64 encoding.
|
||||
-->
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
</root>
|
15
SUBD_Car_rent/Forms/Forms.csproj
Normal file
15
SUBD_Car_rent/Forms/Forms.csproj
Normal file
@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net6.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<UseWindowsForms>true</UseWindowsForms>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\databaseAbstractions\database.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
17
SUBD_Car_rent/Forms/Program.cs
Normal file
17
SUBD_Car_rent/Forms/Program.cs
Normal file
@ -0,0 +1,17 @@
|
||||
namespace Forms
|
||||
{
|
||||
internal static class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// The main entry point for the application.
|
||||
/// </summary>
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
// To customize application configuration such as set high DPI settings or default font,
|
||||
// see https://aka.ms/applicationconfiguration.
|
||||
ApplicationConfiguration.Initialize();
|
||||
Application.Run(new FormMain());
|
||||
}
|
||||
}
|
||||
}
|
31
SUBD_Car_rent/SUBD_Car_rent.sln
Normal file
31
SUBD_Car_rent/SUBD_Car_rent.sln
Normal file
@ -0,0 +1,31 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.8.34309.116
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "database", "databaseAbstractions\database.csproj", "{874A3D9B-6051-4823-9A3B-725C71FDC245}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Forms", "Forms\Forms.csproj", "{318E26AF-A4DC-4863-B829-DBD4B63DC0D7}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{874A3D9B-6051-4823-9A3B-725C71FDC245}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{874A3D9B-6051-4823-9A3B-725C71FDC245}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{874A3D9B-6051-4823-9A3B-725C71FDC245}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{874A3D9B-6051-4823-9A3B-725C71FDC245}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{318E26AF-A4DC-4863-B829-DBD4B63DC0D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{318E26AF-A4DC-4863-B829-DBD4B63DC0D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{318E26AF-A4DC-4863-B829-DBD4B63DC0D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{318E26AF-A4DC-4863-B829-DBD4B63DC0D7}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {C9647B13-C74A-4FAE-A110-8C99AF4E3DEC}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
43
SUBD_Car_rent/databaseAbstractions/Abstractions.cs
Normal file
43
SUBD_Car_rent/databaseAbstractions/Abstractions.cs
Normal file
@ -0,0 +1,43 @@
|
||||
namespace database
|
||||
{
|
||||
public abstract class Abstractions
|
||||
{
|
||||
|
||||
// CDUD операции для каждой сущности
|
||||
|
||||
// Car
|
||||
public abstract void AddCar(Car car);
|
||||
public abstract List<Car> GetCars();
|
||||
public abstract Car GetCarById(int id);
|
||||
public abstract void UpdateCar(Car car);
|
||||
public abstract void DeleteCar(int id);
|
||||
|
||||
// Client
|
||||
public abstract void AddClient(Client client);
|
||||
public abstract Client GetClientById(int id);
|
||||
public abstract List<Client> GetClients();
|
||||
public abstract void UpdateClient(Client client);
|
||||
public abstract void DeleteClient(int id);
|
||||
|
||||
// Rental
|
||||
public abstract void AddRental(Rental rental);
|
||||
public abstract Rental GetRentalById(int id);
|
||||
public abstract List<Rental> GetRentals();
|
||||
public abstract void UpdateRental(Rental rental);
|
||||
public abstract void DeleteRental(int id);
|
||||
|
||||
// CarModel
|
||||
public abstract void AddCarModel(CarModel carModel);
|
||||
public abstract CarModel GetCarModelById(int id);
|
||||
public abstract List<CarModel> GetCarModels();
|
||||
public abstract void UpdateCarModel(CarModel carModel);
|
||||
public abstract void DeleteCarModel(int id);
|
||||
|
||||
// Branch
|
||||
public abstract void AddBranch(Branch branch);
|
||||
public abstract Branch GetBranchById(int id);
|
||||
public abstract List<Branch> GetBranches();
|
||||
public abstract void UpdateBranch(Branch branch);
|
||||
public abstract void DeleteBranch(int id);
|
||||
}
|
||||
}
|
351
SUBD_Car_rent/databaseAbstractions/Implementation.cs
Normal file
351
SUBD_Car_rent/databaseAbstractions/Implementation.cs
Normal file
@ -0,0 +1,351 @@
|
||||
using Npgsql;
|
||||
|
||||
namespace database
|
||||
{
|
||||
public class Implementation : Abstractions
|
||||
{
|
||||
private NpgsqlConnection GetConnection()
|
||||
{
|
||||
return new NpgsqlConnection("Host=172.18.241.131;Username=tim;Database=carrent;");
|
||||
}
|
||||
|
||||
// CDUD операции для каждой сущности
|
||||
|
||||
// Car
|
||||
public override void AddCar(Car car)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"INSERT INTO car (id, model_id, branch_id, year, mileage, status_id) VALUES ({car.Id}, {car.ModelId}, {car.BranchId}, {car.Year}, {car.Mileage}, {car.StatusId})", conn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public override List<Car> GetCars()
|
||||
{
|
||||
var cars = new List<Car>();
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand("SELECT * FROM car", conn);
|
||||
using var reader = cmd.ExecuteReader();
|
||||
while (reader.Read())
|
||||
{
|
||||
cars.Add(new Car
|
||||
{
|
||||
Id = reader.GetInt32(0),
|
||||
ModelId = reader.GetInt32(1),
|
||||
BranchId = reader.GetInt32(2),
|
||||
Year = reader.GetInt32(3),
|
||||
Mileage = reader.GetInt32(4),
|
||||
StatusId = reader.GetInt32(5)
|
||||
});
|
||||
}
|
||||
return cars;
|
||||
}
|
||||
|
||||
public override Car GetCarById(int id)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"SELECT * FROM car WHERE id = {id}", conn);
|
||||
using var reader = cmd.ExecuteReader();
|
||||
if (reader.Read())
|
||||
{
|
||||
return new Car
|
||||
{
|
||||
Id = reader.GetInt32(0),
|
||||
ModelId = reader.GetInt32(1),
|
||||
BranchId = reader.GetInt32(2),
|
||||
Year = reader.GetInt32(3),
|
||||
Mileage = reader.GetInt32(4),
|
||||
StatusId = reader.GetInt32(5)
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public override void UpdateCar(Car car)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"UPDATE car SET model_id = {car.ModelId}, branch_id = {car.BranchId}, year = {car.Year}, mileage = {car.Mileage}, status_id = {car.StatusId} WHERE id = {car.Id}", conn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public override void DeleteCar(int id)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"DELETE FROM car WHERE id = {id}", conn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
// Client
|
||||
public override void AddClient(Client client)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"INSERT INTO client (id, name, surname, address, phone, email) VALUES ({client.Id}, '{client.Name}', '{client.Surname}', '{client.Address}', '{client.Phone}', '{client.Email}')", conn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public override List<Client> GetClients()
|
||||
{
|
||||
var clients = new List<Client>();
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand("SELECT * FROM client", conn);
|
||||
using var reader = cmd.ExecuteReader();
|
||||
while (reader.Read())
|
||||
{
|
||||
clients.Add(new Client
|
||||
{
|
||||
Id = reader.GetInt32(0),
|
||||
Name = reader.GetString(1),
|
||||
Surname = reader.GetString(2),
|
||||
Address = reader.GetString(3),
|
||||
Phone = reader.GetString(4),
|
||||
Email = reader.GetString(5)
|
||||
});
|
||||
}
|
||||
return clients;
|
||||
}
|
||||
|
||||
public override Client GetClientById(int id)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"SELECT * FROM client WHERE id = {id}", conn);
|
||||
using var reader = cmd.ExecuteReader();
|
||||
if (reader.Read())
|
||||
{
|
||||
return new Client
|
||||
{
|
||||
Id = reader.GetInt32(0),
|
||||
Name = reader.GetString(1),
|
||||
Surname = reader.GetString(2),
|
||||
Address = reader.GetString(3),
|
||||
Phone = reader.GetString(4),
|
||||
Email = reader.GetString(5)
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public override void UpdateClient(Client client)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"UPDATE client SET name = '{client.Name}', surname = '{client.Surname}', address = '{client.Address}', phone = '{client.Phone}', email = '{client.Email}' WHERE id = {client.Id}", conn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public override void DeleteClient(int id)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"DELETE FROM client WHERE id = {id}", conn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
// Rental
|
||||
public override void AddRental(Rental rental)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"INSERT INTO rental (id, car_id, client_id, start_date, end_date, cost) VALUES ({rental.Id}, {rental.CarId}, {rental.ClientId}, '{rental.StartDate}', '{rental.EndDate}', {rental.Cost})", conn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public override List<Rental> GetRentals()
|
||||
{
|
||||
var rentals = new List<Rental>();
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand("SELECT * FROM rental", conn);
|
||||
using var reader = cmd.ExecuteReader();
|
||||
while (reader.Read())
|
||||
{
|
||||
rentals.Add(new Rental
|
||||
{
|
||||
Id = reader.GetInt32(0),
|
||||
CarId = reader.GetInt32(1),
|
||||
ClientId = reader.GetInt32(2),
|
||||
StartDate = reader.GetDateTime(3),
|
||||
EndDate = reader.GetDateTime(4),
|
||||
Cost = reader.GetDecimal(5)
|
||||
});
|
||||
}
|
||||
return rentals;
|
||||
}
|
||||
|
||||
public override Rental GetRentalById(int id)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"SELECT * FROM rental WHERE id = {id}", conn);
|
||||
using var reader = cmd.ExecuteReader();
|
||||
if (reader.Read())
|
||||
{
|
||||
return new Rental
|
||||
{
|
||||
Id = reader.GetInt32(0),
|
||||
CarId = reader.GetInt32(1),
|
||||
ClientId = reader.GetInt32(2),
|
||||
StartDate = reader.GetDateTime(3),
|
||||
EndDate = reader.GetDateTime(4),
|
||||
Cost = reader.GetDecimal(5)
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public override void UpdateRental(Rental rental)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"UPDATE rental SET car_id = {rental.CarId}, client_id = {rental.ClientId}, start_date = '{rental.StartDate}', end_date = '{rental.EndDate}', cost = {rental.Cost} WHERE id = {rental.Id}", conn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public override void DeleteRental(int id)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"DELETE FROM rental WHERE id = {id}", conn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
// CarModel
|
||||
public override void AddCarModel(CarModel carModel)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"INSERT INTO car_model (id, brand, model, year, body_type_id, seats) VALUES ({carModel.Id}, '{carModel.Brand}', '{carModel.Model}', {carModel.Year}, {carModel.BodyTypeId}, {carModel.Seats})", conn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public override List<CarModel> GetCarModels()
|
||||
{
|
||||
var carModels = new List<CarModel>();
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand("SELECT * FROM car_model", conn);
|
||||
using var reader = cmd.ExecuteReader();
|
||||
while (reader.Read())
|
||||
{
|
||||
carModels.Add(new CarModel
|
||||
{
|
||||
Id = reader.GetInt32(0),
|
||||
Brand = reader.GetString(1),
|
||||
Model = reader.GetString(2),
|
||||
Year = reader.GetInt32(3),
|
||||
BodyTypeId = reader.GetInt32(4),
|
||||
Seats = reader.GetInt32(5)
|
||||
});
|
||||
}
|
||||
return carModels;
|
||||
}
|
||||
|
||||
public override CarModel GetCarModelById(int id)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"SELECT * FROM car_model WHERE id = {id}", conn);
|
||||
using var reader = cmd.ExecuteReader();
|
||||
if (reader.Read())
|
||||
{
|
||||
return new CarModel
|
||||
{
|
||||
Id = reader.GetInt32(0),
|
||||
Brand = reader.GetString(1),
|
||||
Model = reader.GetString(2),
|
||||
Year = reader.GetInt32(3),
|
||||
BodyTypeId = reader.GetInt32(4),
|
||||
Seats = reader.GetInt32(5)
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public override void UpdateCarModel(CarModel carModel)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"UPDATE car_model SET brand = '{carModel.Brand}', model = '{carModel.Model}', year = {carModel.Year}, body_type_id = {carModel.BodyTypeId}, seats = {carModel.Seats} WHERE id = {carModel.Id}", conn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public override void DeleteCarModel(int id)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"DELETE FROM car_model WHERE id = {id}", conn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
// Branch
|
||||
public override void AddBranch(Branch branch)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"INSERT INTO branch (id, name, address, phone, working_hours) VALUES ({branch.Id}, '{branch.Name}', '{branch.Address}', '{branch.Phone}', '{branch.WorkingHours}')", conn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public override List<Branch> GetBranches()
|
||||
{
|
||||
var branches = new List<Branch>();
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand("SELECT * FROM branch", conn);
|
||||
using var reader = cmd.ExecuteReader();
|
||||
while (reader.Read())
|
||||
{
|
||||
branches.Add(new Branch
|
||||
{
|
||||
Id = reader.GetInt32(0),
|
||||
Name = reader.GetString(1),
|
||||
Address = reader.GetString(2),
|
||||
Phone = reader.GetString(3),
|
||||
WorkingHours = reader.GetString(4)
|
||||
});
|
||||
}
|
||||
return branches;
|
||||
}
|
||||
|
||||
public override Branch GetBranchById(int id)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"SELECT * FROM branch WHERE id = {id}", conn);
|
||||
using var reader = cmd.ExecuteReader();
|
||||
if (reader.Read())
|
||||
{
|
||||
return new Branch
|
||||
{
|
||||
Id = reader.GetInt32(0),
|
||||
Name = reader.GetString(1),
|
||||
Address = reader.GetString(2),
|
||||
Phone = reader.GetString(3),
|
||||
WorkingHours = reader.GetString(4)
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public override void UpdateBranch(Branch branch)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"UPDATE branch SET name = '{branch.Name}', address = '{branch.Address}', phone = '{branch.Phone}', working_hours = '{branch.WorkingHours}' WHERE id = {branch.Id}", conn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public override void DeleteBranch(int id)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand($"DELETE FROM branch WHERE id = {id}", conn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
13
SUBD_Car_rent/databaseAbstractions/database.csproj
Normal file
13
SUBD_Car_rent/databaseAbstractions/database.csproj
Normal file
@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Npgsql" Version="8.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
59
SUBD_Car_rent/databaseAbstractions/models.cs
Normal file
59
SUBD_Car_rent/databaseAbstractions/models.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace database
|
||||
{
|
||||
// Определение моделей данных
|
||||
|
||||
public class Car
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int ModelId { get; set; }
|
||||
public int BranchId { get; set; }
|
||||
public int Year { get; set; }
|
||||
public int Mileage { get; set; }
|
||||
public int StatusId { get; set; }
|
||||
}
|
||||
|
||||
public class Client
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Surname { get; set; }
|
||||
public string Address { get; set; }
|
||||
public string Phone { get; set; }
|
||||
public string Email { get; set; }
|
||||
}
|
||||
|
||||
public class Rental
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int CarId { get; set; }
|
||||
public int ClientId { get; set; }
|
||||
public DateTime StartDate { get; set; }
|
||||
public DateTime EndDate { get; set; }
|
||||
public decimal Cost { get; set; }
|
||||
}
|
||||
|
||||
public class CarModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Brand { get; set; }
|
||||
public string Model { get; set; }
|
||||
public int Year { get; set; }
|
||||
public int BodyTypeId { get; set; }
|
||||
public int Seats { get; set; }
|
||||
}
|
||||
|
||||
public class Branch
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Address { get; set; }
|
||||
public string Phone { get; set; }
|
||||
public string WorkingHours { get; set; }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user