From bb20f0e17b10d299a126358fb39808a76c759111 Mon Sep 17 00:00:00 2001 From: Timourka Date: Tue, 26 Mar 2024 20:51:53 +0400 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=D0=B8=20?= =?UTF-8?q?=D1=83=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=B1=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SUBD_Car_rent/Forms/FormMain.Designer.cs | 73 ++++ SUBD_Car_rent/Forms/FormMain.cs | 40 ++ SUBD_Car_rent/Forms/FormMain.resx | 120 ++++++ SUBD_Car_rent/Forms/Forms.csproj | 15 + SUBD_Car_rent/Forms/Program.cs | 17 + SUBD_Car_rent/SUBD_Car_rent.sln | 31 ++ .../databaseAbstractions/Abstractions.cs | 43 +++ .../databaseAbstractions/Implementation.cs | 351 ++++++++++++++++++ .../databaseAbstractions/database.csproj | 13 + SUBD_Car_rent/databaseAbstractions/models.cs | 59 +++ 10 files changed, 762 insertions(+) create mode 100644 SUBD_Car_rent/Forms/FormMain.Designer.cs create mode 100644 SUBD_Car_rent/Forms/FormMain.cs create mode 100644 SUBD_Car_rent/Forms/FormMain.resx create mode 100644 SUBD_Car_rent/Forms/Forms.csproj create mode 100644 SUBD_Car_rent/Forms/Program.cs create mode 100644 SUBD_Car_rent/SUBD_Car_rent.sln create mode 100644 SUBD_Car_rent/databaseAbstractions/Abstractions.cs create mode 100644 SUBD_Car_rent/databaseAbstractions/Implementation.cs create mode 100644 SUBD_Car_rent/databaseAbstractions/database.csproj create mode 100644 SUBD_Car_rent/databaseAbstractions/models.cs diff --git a/SUBD_Car_rent/Forms/FormMain.Designer.cs b/SUBD_Car_rent/Forms/FormMain.Designer.cs new file mode 100644 index 0000000..27eae93 --- /dev/null +++ b/SUBD_Car_rent/Forms/FormMain.Designer.cs @@ -0,0 +1,73 @@ +namespace Forms +{ + partial class FormMain + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + 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; + } +} \ No newline at end of file diff --git a/SUBD_Car_rent/Forms/FormMain.cs b/SUBD_Car_rent/Forms/FormMain.cs new file mode 100644 index 0000000..bc81e5a --- /dev/null +++ b/SUBD_Car_rent/Forms/FormMain.cs @@ -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 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); + } + } + */ + } +} diff --git a/SUBD_Car_rent/Forms/FormMain.resx b/SUBD_Car_rent/Forms/FormMain.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/SUBD_Car_rent/Forms/FormMain.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/SUBD_Car_rent/Forms/Forms.csproj b/SUBD_Car_rent/Forms/Forms.csproj new file mode 100644 index 0000000..f2ca8eb --- /dev/null +++ b/SUBD_Car_rent/Forms/Forms.csproj @@ -0,0 +1,15 @@ + + + + WinExe + net6.0-windows + enable + true + enable + + + + + + + \ No newline at end of file diff --git a/SUBD_Car_rent/Forms/Program.cs b/SUBD_Car_rent/Forms/Program.cs new file mode 100644 index 0000000..db0074d --- /dev/null +++ b/SUBD_Car_rent/Forms/Program.cs @@ -0,0 +1,17 @@ +namespace Forms +{ + internal static class Program + { + /// + /// The main entry point for the application. + /// + [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()); + } + } +} \ No newline at end of file diff --git a/SUBD_Car_rent/SUBD_Car_rent.sln b/SUBD_Car_rent/SUBD_Car_rent.sln new file mode 100644 index 0000000..fcf9603 --- /dev/null +++ b/SUBD_Car_rent/SUBD_Car_rent.sln @@ -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 diff --git a/SUBD_Car_rent/databaseAbstractions/Abstractions.cs b/SUBD_Car_rent/databaseAbstractions/Abstractions.cs new file mode 100644 index 0000000..8b961df --- /dev/null +++ b/SUBD_Car_rent/databaseAbstractions/Abstractions.cs @@ -0,0 +1,43 @@ +namespace database +{ + public abstract class Abstractions + { + + // CDUD операции для каждой сущности + + // Car + public abstract void AddCar(Car car); + public abstract List 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 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 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 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 GetBranches(); + public abstract void UpdateBranch(Branch branch); + public abstract void DeleteBranch(int id); + } +} diff --git a/SUBD_Car_rent/databaseAbstractions/Implementation.cs b/SUBD_Car_rent/databaseAbstractions/Implementation.cs new file mode 100644 index 0000000..bb03394 --- /dev/null +++ b/SUBD_Car_rent/databaseAbstractions/Implementation.cs @@ -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 GetCars() + { + var cars = new List(); + 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 GetClients() + { + var clients = new List(); + 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 GetRentals() + { + var rentals = new List(); + 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 GetCarModels() + { + var carModels = new List(); + 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 GetBranches() + { + var branches = new List(); + 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(); + } + } +} diff --git a/SUBD_Car_rent/databaseAbstractions/database.csproj b/SUBD_Car_rent/databaseAbstractions/database.csproj new file mode 100644 index 0000000..715d83b --- /dev/null +++ b/SUBD_Car_rent/databaseAbstractions/database.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/SUBD_Car_rent/databaseAbstractions/models.cs b/SUBD_Car_rent/databaseAbstractions/models.cs new file mode 100644 index 0000000..959eb24 --- /dev/null +++ b/SUBD_Car_rent/databaseAbstractions/models.cs @@ -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; } + } +}