From 742434cc5d7a6bc717870be7556d08638e1216ea Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Mon, 8 May 2023 15:25:26 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=B5=D1=81=20?= =?UTF-8?q?=D0=BD=D0=B0=20MongoDB=20Branches(=D1=84=D0=B8=D0=BB=D0=B8?= =?UTF-8?q?=D0=B0=D0=BB=D1=8B)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CarRent.sln | 8 +- CarRent/CarRent.csproj | 1 + CarRent/FormBranches.cs | 14 +- CarRent/FormCars.cs | 2 + CarRent/FormClients.cs | 1 + CarRent/FormCreateBranch.cs | 41 +++++- CarRent/FormCreateCar.cs | 2 +- CarRent/FormRentals.cs | 11 +- CarRent/Program.cs | 3 +- .../BindingModels/BranchBindingModel.cs | 4 +- .../BindingModels/CarBindingModel.cs | 6 +- .../BindingModels/ClientBindingModel.cs | 3 +- .../BindingModels/RentalBindingModel.cs | 10 +- .../SearchModels/BranchSearchModel.cs | 2 + .../SearchModels/CarSearchModel.cs | 1 + .../SearchModels/ClientSearchModel.cs | 1 + .../SearchModels/RentalSearchModel.cs | 1 + .../ViewModels/BranchViewModel.cs | 3 +- CarRentContracts/ViewModels/CarViewModel.cs | 9 +- .../ViewModels/ClientViewModel.cs | 4 +- .../ViewModels/RentalViewModel.cs | 10 +- CarRentDataModels/IId.cs | 3 +- CarRentDatabase/Models/Branch.cs | 5 +- CarRentDatabase/Models/Car.cs | 6 +- CarRentDatabase/Models/Client.cs | 4 +- CarRentDatabase/Models/Rental.cs | 8 +- CarRentMongoDB/CarRentMongoDB.cs | 34 +++++ CarRentMongoDB/CarRentMongoDB.csproj | 21 +++ CarRentMongoDB/Implements/BranchStorage.cs | 129 ++++++++++++++++++ CarRentMongoDB/Implements/CarStorage.cs | 92 +++++++++++++ CarRentMongoDB/Implements/ClientStorage.cs | 93 +++++++++++++ CarRentMongoDB/Implements/RentalStorage.cs | 114 ++++++++++++++++ CarRentMongoDB/Models/Branch.cs | 70 ++++++++++ CarRentMongoDB/Models/Car.cs | 105 ++++++++++++++ CarRentMongoDB/Models/Client.cs | 82 +++++++++++ CarRentMongoDB/Models/Rental.cs | 98 +++++++++++++ 36 files changed, 952 insertions(+), 49 deletions(-) create mode 100644 CarRentMongoDB/CarRentMongoDB.cs create mode 100644 CarRentMongoDB/CarRentMongoDB.csproj create mode 100644 CarRentMongoDB/Implements/BranchStorage.cs create mode 100644 CarRentMongoDB/Implements/CarStorage.cs create mode 100644 CarRentMongoDB/Implements/ClientStorage.cs create mode 100644 CarRentMongoDB/Implements/RentalStorage.cs create mode 100644 CarRentMongoDB/Models/Branch.cs create mode 100644 CarRentMongoDB/Models/Car.cs create mode 100644 CarRentMongoDB/Models/Client.cs create mode 100644 CarRentMongoDB/Models/Rental.cs diff --git a/CarRent.sln b/CarRent.sln index 87bcb07..b7b268b 100644 --- a/CarRent.sln +++ b/CarRent.sln @@ -11,7 +11,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRentBusinessLogic", "Car EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRentDatabase", "CarRentDatabase\CarRentDatabase.csproj", "{01667A54-719D-414B-B05F-1B3693971136}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarRent", "CarRent\CarRent.csproj", "{19934FD6-266C-4824-8FC8-4895095C3EDF}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRent", "CarRent\CarRent.csproj", "{19934FD6-266C-4824-8FC8-4895095C3EDF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarRentMongoDB", "CarRentMongoDB\CarRentMongoDB.csproj", "{698D9643-EA17-44DD-9D83-D623DBB6B70F}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {19934FD6-266C-4824-8FC8-4895095C3EDF}.Debug|Any CPU.Build.0 = Debug|Any CPU {19934FD6-266C-4824-8FC8-4895095C3EDF}.Release|Any CPU.ActiveCfg = Release|Any CPU {19934FD6-266C-4824-8FC8-4895095C3EDF}.Release|Any CPU.Build.0 = Release|Any CPU + {698D9643-EA17-44DD-9D83-D623DBB6B70F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {698D9643-EA17-44DD-9D83-D623DBB6B70F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {698D9643-EA17-44DD-9D83-D623DBB6B70F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {698D9643-EA17-44DD-9D83-D623DBB6B70F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/CarRent/CarRent.csproj b/CarRent/CarRent.csproj index 1032114..6da73e2 100644 --- a/CarRent/CarRent.csproj +++ b/CarRent/CarRent.csproj @@ -20,6 +20,7 @@ + \ No newline at end of file diff --git a/CarRent/FormBranches.cs b/CarRent/FormBranches.cs index 2c5dfe8..20717bc 100644 --- a/CarRent/FormBranches.cs +++ b/CarRent/FormBranches.cs @@ -34,10 +34,10 @@ namespace CarRent { dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["ObjectId"].Visible = false; } } - catch (Exception ex) - { } + catch (Exception){ } } private void FormBranches_Load(object sender, EventArgs e) { @@ -81,14 +81,14 @@ namespace CarRent { if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { - int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + int? id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + string? objectId = dataGridView.SelectedRows[0].Cells["Id"].Value.ToString(); try { - if (!_branchLogic.Delete(new BranchBindingModel - { - Id = id - })) + if (Program.CURRENT_DATABASE.Equals("PostgreSQL") + && !_branchLogic.Delete(new BranchBindingModel{ Id = id ?? 0}) + || !_branchLogic.Delete(new BranchBindingModel { ObjectId = objectId ?? string.Empty })) { throw new Exception("Ошибка при удалении."); } diff --git a/CarRent/FormCars.cs b/CarRent/FormCars.cs index c6d7254..0896cb5 100644 --- a/CarRent/FormCars.cs +++ b/CarRent/FormCars.cs @@ -33,7 +33,9 @@ namespace CarRent { dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["ObjectId"].Visible = false; dataGridView.Columns["BranchId"].Visible = false; + dataGridView.Columns["BranchStringId"].Visible = false; } } catch (Exception ex) diff --git a/CarRent/FormClients.cs b/CarRent/FormClients.cs index 2bff648..29a8f05 100644 --- a/CarRent/FormClients.cs +++ b/CarRent/FormClients.cs @@ -32,6 +32,7 @@ namespace CarRent { dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["ObjectId"].Visible = false; } } catch (Exception ex) diff --git a/CarRent/FormCreateBranch.cs b/CarRent/FormCreateBranch.cs index a8fca6a..57cd622 100644 --- a/CarRent/FormCreateBranch.cs +++ b/CarRent/FormCreateBranch.cs @@ -2,6 +2,7 @@ using CarRentContracts.BindingModels; using CarRentContracts.BusinessLogicContracts; using CarRentContracts.SearchModels; +using CarRentContracts.ViewModels; using ClientRentBusinessLogic.BusinessLogics; using System; using System.Collections.Generic; @@ -22,6 +23,9 @@ namespace CarRent private int? _id; public int Id { set { _id = value; } } + private string? _objectId; + public string ObjectId { set { _objectId = value; } } + public FormCreateBranch(IBranchLogic branchLogic) { InitializeComponent(); @@ -47,13 +51,28 @@ namespace CarRent } try { - var model = new BranchBindingModel + BranchBindingModel model; + if (Program.CURRENT_DATABASE.Equals("PostgreSQL")) { - Id = _id ?? 0, - Name = textBoxName.Text, - PhoneNumber = textBoxPhoneNumber.Text, - Address = textBoxAddress.Text, - }; + model = new BranchBindingModel + { + Id = _id ?? 0, + Name = textBoxName.Text, + PhoneNumber = textBoxPhoneNumber.Text, + Address = textBoxAddress.Text, + }; + } + else + { + model = new BranchBindingModel + { + ObjectId = _objectId ?? string.Empty, + Name = textBoxName.Text, + PhoneNumber = textBoxPhoneNumber.Text, + Address = textBoxAddress.Text, + }; + } + var operationResult = _id.HasValue ? _branchLogic.Update(model) : _branchLogic.Create(model); @@ -85,7 +104,15 @@ namespace CarRent { try { - var view = _branchLogic.ReadElement(new BranchSearchModel { Id = _id.Value }); + BranchViewModel? view; + if (Program.CURRENT_DATABASE.Equals("PostgreSQL")) + { + view = _branchLogic.ReadElement(new BranchSearchModel { Id = _id }); + } + else + { + view = _branchLogic.ReadElement(new BranchSearchModel { ObjectId = _objectId }); + } if (view != null) { diff --git a/CarRent/FormCreateCar.cs b/CarRent/FormCreateCar.cs index 6dd9d94..72ecd76 100644 --- a/CarRent/FormCreateCar.cs +++ b/CarRent/FormCreateCar.cs @@ -133,7 +133,7 @@ namespace CarRent textBoxModel.Text = view.Model; textBoxRentalCost.Text = view.RentalCostPerHour.ToString(); dateTimePicker.Value = view.YearOfManuf; - comboBoxBranch.SelectedIndex = view.BranchId; + comboBoxBranch.SelectedIndex = view.BranchId ?? 0; } } catch (Exception ex) diff --git a/CarRent/FormRentals.cs b/CarRent/FormRentals.cs index 39958f7..7c6958d 100644 --- a/CarRent/FormRentals.cs +++ b/CarRent/FormRentals.cs @@ -51,13 +51,15 @@ namespace CarRent { dataGridView.DataSource = list; dataGridView.Columns["ClientId"].Visible = false; + dataGridView.Columns["ClientStringId"].Visible = false; dataGridView.Columns["CarId"].Visible = false; + dataGridView.Columns["CarStringId"].Visible = false; dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["ObjectId"].Visible = false; } } - catch (Exception ex) - {} + catch (Exception) { } } private void LoadData(int id) { @@ -66,9 +68,6 @@ namespace CarRent ClientId = id, }); dataGridView.DataSource = list; - dataGridView.Columns["ClientId"].Visible = false; - dataGridView.Columns["CarId"].Visible = false; - dataGridView.Columns["Id"].Visible = false; } private void филиалыToolStripMenuItem_Click(object sender, EventArgs e) { @@ -211,7 +210,7 @@ namespace CarRent _carLogic.ClearEntity(); _rentalLogic.ClearEntity(); } - catch (Exception ex) { } + catch (Exception) { } } } } diff --git a/CarRent/Program.cs b/CarRent/Program.cs index bf11b4b..910dbfe 100644 --- a/CarRent/Program.cs +++ b/CarRent/Program.cs @@ -1,7 +1,7 @@ using CarRentBusinessLogic.BusinessLogics; using CarRentContracts.BusinessLogicContracts; using CarRentContracts.StoragesContracts; -using CarRentDatabase.Implements; +using CarRentMongoDB.Implements; using ClientRentBusinessLogic.BusinessLogics; using Microsoft.Extensions.DependencyInjection; using System; @@ -10,6 +10,7 @@ namespace CarRent { internal static class Program { + public static string CURRENT_DATABASE = "MongoDB"; private static ServiceProvider? _serviceProvider; public static ServiceProvider? ServiceProvider => _serviceProvider; /// diff --git a/CarRentContracts/BindingModels/BranchBindingModel.cs b/CarRentContracts/BindingModels/BranchBindingModel.cs index a9019d0..dd80ab3 100644 --- a/CarRentContracts/BindingModels/BranchBindingModel.cs +++ b/CarRentContracts/BindingModels/BranchBindingModel.cs @@ -15,6 +15,8 @@ namespace CarRentContracts.BindingModels public string PhoneNumber { get; set; } = string.Empty; - public int Id { get; set; } + public int? Id { get; set; } + + public string? ObjectId { get; set; } = string.Empty; } } diff --git a/CarRentContracts/BindingModels/CarBindingModel.cs b/CarRentContracts/BindingModels/CarBindingModel.cs index 3cb46a6..6d4109b 100644 --- a/CarRentContracts/BindingModels/CarBindingModel.cs +++ b/CarRentContracts/BindingModels/CarBindingModel.cs @@ -20,7 +20,9 @@ namespace CarRentContracts.BindingModels public string LicensePlate { get; set; } = string.Empty; public double RentalCostPerHour { get; set; } - public int BranchId { get; set; } - public int Id { get; set; } + public int? BranchId { get; set; } + public string? BranchStringId { get; set; } = string.Empty; + public int? Id { get; set; } + public string? ObjectId { get; set; } = string.Empty; } } diff --git a/CarRentContracts/BindingModels/ClientBindingModel.cs b/CarRentContracts/BindingModels/ClientBindingModel.cs index 4a3989d..eaa089a 100644 --- a/CarRentContracts/BindingModels/ClientBindingModel.cs +++ b/CarRentContracts/BindingModels/ClientBindingModel.cs @@ -19,6 +19,7 @@ namespace CarRentContracts.BindingModels public string DriveLicenseNumber { get; set; } = string.Empty; - public int Id { get; set; } + public int? Id { get; set; } + public string? ObjectId { get; set; } = string.Empty; } } diff --git a/CarRentContracts/BindingModels/RentalBindingModel.cs b/CarRentContracts/BindingModels/RentalBindingModel.cs index ceaf0e6..f1235f5 100644 --- a/CarRentContracts/BindingModels/RentalBindingModel.cs +++ b/CarRentContracts/BindingModels/RentalBindingModel.cs @@ -18,9 +18,11 @@ namespace CarRentContracts.BindingModels public string? ReviewText { get; set; } = string.Empty; public int? ReviewRating { get; set; } - public int ClientId { get; set; } - public int CarId { get; set; } - - public int Id { get; set; } + public int? ClientId { get; set; } + public string? ClientStringId { get; set; } = string.Empty; + public int? CarId { get; set; } + public string? CarStringId { get; set; } = string.Empty; + public int? Id { get; set; } + public string? ObjectId { get; set; } = string.Empty; } } diff --git a/CarRentContracts/SearchModels/BranchSearchModel.cs b/CarRentContracts/SearchModels/BranchSearchModel.cs index c9b3fbd..1fb3b3b 100644 --- a/CarRentContracts/SearchModels/BranchSearchModel.cs +++ b/CarRentContracts/SearchModels/BranchSearchModel.cs @@ -15,5 +15,7 @@ namespace CarRentContracts.SearchModels public string? PhoneNumber { get; set; } public int? Id { get; set; } + + public string? ObjectId { get; set; } } } diff --git a/CarRentContracts/SearchModels/CarSearchModel.cs b/CarRentContracts/SearchModels/CarSearchModel.cs index 4f15ebf..3206f79 100644 --- a/CarRentContracts/SearchModels/CarSearchModel.cs +++ b/CarRentContracts/SearchModels/CarSearchModel.cs @@ -21,5 +21,6 @@ namespace CarRentContracts.SearchModels public double? RentalCostPerHour { get; set; } public int? Id { get; set; } + public string? ObjectId { get; set; } } } diff --git a/CarRentContracts/SearchModels/ClientSearchModel.cs b/CarRentContracts/SearchModels/ClientSearchModel.cs index 6826977..ca86e10 100644 --- a/CarRentContracts/SearchModels/ClientSearchModel.cs +++ b/CarRentContracts/SearchModels/ClientSearchModel.cs @@ -19,5 +19,6 @@ namespace CarRentContracts.SearchModels public string? DriveLicenseNumber { get; set; } public int? Id { get; set; } + public string? ObjectId { get; set; } } } diff --git a/CarRentContracts/SearchModels/RentalSearchModel.cs b/CarRentContracts/SearchModels/RentalSearchModel.cs index ef0d5f2..07db2cf 100644 --- a/CarRentContracts/SearchModels/RentalSearchModel.cs +++ b/CarRentContracts/SearchModels/RentalSearchModel.cs @@ -20,5 +20,6 @@ namespace CarRentContracts.SearchModels public int? ClientId { get; set; } public int? Id { get; set; } + public string? ObjectId { get; set; } } } diff --git a/CarRentContracts/ViewModels/BranchViewModel.cs b/CarRentContracts/ViewModels/BranchViewModel.cs index 4882801..025d269 100644 --- a/CarRentContracts/ViewModels/BranchViewModel.cs +++ b/CarRentContracts/ViewModels/BranchViewModel.cs @@ -16,6 +16,7 @@ namespace CarRentContracts.ViewModels public string Address { get; set; } = string.Empty; [DisplayName("Номер телефона")] public string PhoneNumber { get; set; } = string.Empty; - public int Id { get; set; } + public int? Id { get; set; } + public string? ObjectId { get; set; } = string.Empty; } } diff --git a/CarRentContracts/ViewModels/CarViewModel.cs b/CarRentContracts/ViewModels/CarViewModel.cs index d08d134..1c9b995 100644 --- a/CarRentContracts/ViewModels/CarViewModel.cs +++ b/CarRentContracts/ViewModels/CarViewModel.cs @@ -1,4 +1,5 @@ -using CarRentDataModels.Models; +using CarRentDataModels; +using CarRentDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel; @@ -24,7 +25,9 @@ namespace CarRentContracts.ViewModels public string LicensePlate { get; set; } = string.Empty; [DisplayName("Цена аренды за час")] public double RentalCostPerHour { get; set; } - public int BranchId { get; set; } - public int Id { get; set; } + public int? BranchId { get; set; } + public string? BranchStringId { get; set; } = string.Empty; + public int? Id { get; set; } + public string? ObjectId { get; set; } = string.Empty; } } diff --git a/CarRentContracts/ViewModels/ClientViewModel.cs b/CarRentContracts/ViewModels/ClientViewModel.cs index e25baa5..1ac1ad0 100644 --- a/CarRentContracts/ViewModels/ClientViewModel.cs +++ b/CarRentContracts/ViewModels/ClientViewModel.cs @@ -20,7 +20,7 @@ namespace CarRentContracts.ViewModels public string Email { get; set; } = string.Empty; [DisplayName("Номер водительских прав")] public string DriveLicenseNumber { get; set; } = string.Empty; - - public int Id { get; set; } + public int? Id { get; set; } + public string? ObjectId { get; set; } = string.Empty; } } diff --git a/CarRentContracts/ViewModels/RentalViewModel.cs b/CarRentContracts/ViewModels/RentalViewModel.cs index 52ceb90..a60a61c 100644 --- a/CarRentContracts/ViewModels/RentalViewModel.cs +++ b/CarRentContracts/ViewModels/RentalViewModel.cs @@ -26,9 +26,11 @@ namespace CarRentContracts.ViewModels public string? ReviewText { get; set; } = string.Empty; [DisplayName("Оценка аренды")] public int? ReviewRating { get; set; } - public int ClientId { get; set; } - public int CarId { get; set; } - - public int Id { get; set; } + public int? ClientId { get; set; } + public string? ClientStringId { get; set; } = string.Empty; + public int? CarId { get; set; } + public string? CarStringId { get; set; } = string.Empty; + public int? Id { get; set; } + public string? ObjectId { get; set; } = string.Empty; } } diff --git a/CarRentDataModels/IId.cs b/CarRentDataModels/IId.cs index 8cf6238..2dc756e 100644 --- a/CarRentDataModels/IId.cs +++ b/CarRentDataModels/IId.cs @@ -2,6 +2,7 @@ { public interface IId { - int Id { get; } + int? Id { get; } + string? ObjectId { get; } } } \ No newline at end of file diff --git a/CarRentDatabase/Models/Branch.cs b/CarRentDatabase/Models/Branch.cs index 7def271..ab8a7c5 100644 --- a/CarRentDatabase/Models/Branch.cs +++ b/CarRentDatabase/Models/Branch.cs @@ -15,13 +15,14 @@ namespace CarRentDatabase.Models { public class Branch : IBranchModel { + [Required] public string Name { get; set; } = string.Empty; [Required] public string Address { get; set; } = string.Empty; [Required] public string PhoneNumber { get; set; } = string.Empty; - public int Id { get; private set; } + public int? Id { get; private set; } [ForeignKey("BranchId")] public virtual List Cars { get; set; } = new(); @@ -56,5 +57,7 @@ namespace CarRentDatabase.Models Address = Address, PhoneNumber = PhoneNumber, }; + + public string ObjectId => throw new NotImplementedException(); } } diff --git a/CarRentDatabase/Models/Car.cs b/CarRentDatabase/Models/Car.cs index d0a3f5a..6c36f9c 100644 --- a/CarRentDatabase/Models/Car.cs +++ b/CarRentDatabase/Models/Car.cs @@ -29,9 +29,9 @@ namespace CarRentDatabase.Models public double RentalCostPerHour { get; set; } [Required] - public int BranchId { get; set; } + public int? BranchId { get; set; } - public int Id { get; private set; } + public int? Id { get; private set; } [ForeignKey("CarId")] List RentalCars { get; set; } = new(); @@ -79,5 +79,7 @@ namespace CarRentDatabase.Models BranchId = BranchId, BranchName = Branch == null ? string.Empty : Branch.Name, }; + + public string ObjectId => throw new NotImplementedException(); } } diff --git a/CarRentDatabase/Models/Client.cs b/CarRentDatabase/Models/Client.cs index 9935314..389a185 100644 --- a/CarRentDatabase/Models/Client.cs +++ b/CarRentDatabase/Models/Client.cs @@ -26,7 +26,7 @@ namespace CarRentDatabase.Models public string DriveLicenseNumber { get; set; } = string.Empty; [Required] - public int Id { get; private set; } + public int? Id { get; private set; } [ForeignKey("ClientId")] List RentalClients { get; set; } = new(); @@ -67,5 +67,7 @@ namespace CarRentDatabase.Models Email = Email, DriveLicenseNumber = DriveLicenseNumber, }; + + public string ObjectId => throw new NotImplementedException(); } } diff --git a/CarRentDatabase/Models/Rental.cs b/CarRentDatabase/Models/Rental.cs index 20148a9..a275526 100644 --- a/CarRentDatabase/Models/Rental.cs +++ b/CarRentDatabase/Models/Rental.cs @@ -22,11 +22,11 @@ namespace CarRentDatabase.Models public string? ReviewText { get; set; } public int? ReviewRating { get ; set; } [Required] - public int ClientId { get; set; } + public int? ClientId { get; set; } [Required] - public int CarId { get; set; } + public int? CarId { get; set; } - public int Id { get; private set; } + public int? Id { get; private set; } public virtual Car Car { get; set; } public virtual Client Client { get; set; } @@ -74,5 +74,7 @@ namespace CarRentDatabase.Models ClientId = ClientId, CarId = CarId, }; + + public string ObjectId => throw new NotImplementedException(); } } diff --git a/CarRentMongoDB/CarRentMongoDB.cs b/CarRentMongoDB/CarRentMongoDB.cs new file mode 100644 index 0000000..dd757f0 --- /dev/null +++ b/CarRentMongoDB/CarRentMongoDB.cs @@ -0,0 +1,34 @@ +using CarRentMongoDB.Models; +using Microsoft.Extensions.Options; +using MongoDB.Driver; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarRentMongoDB +{ + + public class CarRentMongoDB : IDisposable + { + private readonly MongoClient _client; + private readonly IMongoDatabase _database; + + public CarRentMongoDB(string connectionString = "mongodb://localhost:27017", + string databaseName = "lab7SUBD") + { + _client = new MongoClient(connectionString); + _database = _client.GetDatabase(databaseName); + } + + public IMongoCollection GetCollection(string collectionName) + { + return _database.GetCollection(collectionName); + } + + public void Dispose() + { + } + } +} diff --git a/CarRentMongoDB/CarRentMongoDB.csproj b/CarRentMongoDB/CarRentMongoDB.csproj new file mode 100644 index 0000000..371394a --- /dev/null +++ b/CarRentMongoDB/CarRentMongoDB.csproj @@ -0,0 +1,21 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + + + diff --git a/CarRentMongoDB/Implements/BranchStorage.cs b/CarRentMongoDB/Implements/BranchStorage.cs new file mode 100644 index 0000000..2054a0c --- /dev/null +++ b/CarRentMongoDB/Implements/BranchStorage.cs @@ -0,0 +1,129 @@ +using Bogus; +using CarRentContracts.BindingModels; +using CarRentContracts.SearchModels; +using CarRentContracts.StoragesContracts; +using CarRentContracts.ViewModels; +using CarRentMongoDB.Models; +using MongoDB.Bson; +using MongoDB.Driver; + +namespace CarRentMongoDB.Implements +{ + public class BranchStorage : IBranchStorage + { + public void ClearEntity() + { + using var context = new CarRentMongoDB(); + context.GetCollection("Branches") + .DeleteMany(Builders.Filter.Empty); + } + public void BranchInsertList(int num) + { + using var context = new CarRentMongoDB(); + var faker = new Faker("ru"); + for (int i = 0; i < num; ++i) + { + var model = new BranchBindingModel + { + ObjectId = string.Empty, + Name = faker.Company.CompanyName(), + Address = faker.Address.FullAddress(), + PhoneNumber = faker.Phone.PhoneNumber(), + }; + context.GetCollection("Branches") + .InsertOne(Branch.Create(model)); + } + } + public BranchViewModel? GetElement(BranchSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.ObjectId.Equals(string.Empty)) + { + return null; + } + using (var context = new CarRentMongoDB()) + { + var _branches = context.GetCollection("Branches"); + + var filterBuilder = Builders.Filter; + var filter = filterBuilder.Empty; + if (!string.IsNullOrEmpty(model.Name)) + { + filter &= filterBuilder.Eq(x => x.Name, model.Name); + } + if (model.ObjectId.Equals(string.Empty)) + { + filter &= filterBuilder.Eq(x => x.ObjectId, model.ObjectId); + } + return _branches.Find(filter) + .FirstOrDefault() + ?.GetViewModel; + } + } + + public List GetFilteredList(BranchSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new List(); + } + using var context = new CarRentMongoDB(); + var _branches = context.GetCollection("Branches"); + + var filterBuilder = Builders.Filter; + var filter = filterBuilder + .Regex(x => x.Name, new BsonRegularExpression(model.Name)); + var branches = _branches + .Find(filter) + .ToList(); + return branches.Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new CarRentMongoDB(); + var _branches = context.GetCollection("Branches"); + return _branches.Find(Builders.Filter.Empty) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public BranchViewModel? Insert(BranchBindingModel model) + { + using (var context = new CarRentMongoDB()) + { + var _branches = context.GetCollection("Branches"); + + var branch = Branch.Create(model); + _branches.InsertOne(branch); + return branch.GetViewModel; + } + } + + public BranchViewModel? Update(BranchBindingModel model) + { + using var context = new CarRentMongoDB(); + var _branches = context.GetCollection("Branches"); + + var filter = Builders.Filter.Eq(x => x.ObjectId, model.ObjectId); + var branch = _branches.Find(filter).FirstOrDefault(); + if (branch == null) + { + return null; + } + branch.Update(model); + _branches.ReplaceOne(filter, branch); + return branch.GetViewModel; + } + + public BranchViewModel? Delete(BranchBindingModel model) + { + using var context = new CarRentMongoDB(); + var _branches = context.GetCollection("Branches"); + + var filter = Builders.Filter.Eq(x => x.ObjectId, model.ObjectId); + var branch = _branches.FindOneAndDelete(filter); + return branch?.GetViewModel; + } + } +} diff --git a/CarRentMongoDB/Implements/CarStorage.cs b/CarRentMongoDB/Implements/CarStorage.cs new file mode 100644 index 0000000..a4c2619 --- /dev/null +++ b/CarRentMongoDB/Implements/CarStorage.cs @@ -0,0 +1,92 @@ +using Bogus; +using CarRentContracts.BindingModels; +using CarRentContracts.SearchModels; +using CarRentContracts.StoragesContracts; +using CarRentContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarRentMongoDB.Implements +{ + public class CarStorage : ICarStorage + { + /*public void ClearEntity() + { + + } + public CarViewModel? GetElement(CarSearchModel model) + { + + } + public void CarInsertList(int num, List branches) + { + + } + public List GetFilteredList(CarSearchModel model) + { + + } + + public List GetFullList() + { + + } + + public CarViewModel? Insert(CarBindingModel model) + { + + } + + public CarViewModel? Update(CarBindingModel model) + { + + } + + public CarViewModel? Delete(CarBindingModel model) + { + + }*/ + public void CarInsertList(int num, List branches) + { + throw new NotImplementedException(); + } + + public void ClearEntity() + { + throw new NotImplementedException(); + } + + public CarViewModel? Delete(CarBindingModel model) + { + throw new NotImplementedException(); + } + + public CarViewModel? GetElement(CarSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFilteredList(CarSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFullList() + { + throw new NotImplementedException(); + } + + public CarViewModel? Insert(CarBindingModel model) + { + throw new NotImplementedException(); + } + + public CarViewModel? Update(CarBindingModel model) + { + throw new NotImplementedException(); + } + } +} diff --git a/CarRentMongoDB/Implements/ClientStorage.cs b/CarRentMongoDB/Implements/ClientStorage.cs new file mode 100644 index 0000000..ddc25c0 --- /dev/null +++ b/CarRentMongoDB/Implements/ClientStorage.cs @@ -0,0 +1,93 @@ +using CarRentContracts.BindingModels; +using CarRentContracts.SearchModels; +using CarRentContracts.StoragesContracts; +using CarRentContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Bogus; + +namespace CarRentMongoDB.Implements +{ + public class ClientStorage : IClientStorage + { + /*public void ClearEntity() + { + + } + public void ClientInsertList(int num) + { + + } + public ClientViewModel? GetElement(ClientSearchModel model) + { + + } + + public List GetFilteredList(ClientSearchModel model) + { + + } + + public List GetFullList() + { + + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + + } + + public ClientViewModel? Update(ClientBindingModel model) + { + + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + + }*/ + public void ClearEntity() + { + throw new NotImplementedException(); + } + + public void ClientInsertList(int num) + { + throw new NotImplementedException(); + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + throw new NotImplementedException(); + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFilteredList(ClientSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFullList() + { + throw new NotImplementedException(); + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + throw new NotImplementedException(); + } + + public ClientViewModel? Update(ClientBindingModel model) + { + throw new NotImplementedException(); + } + } +} diff --git a/CarRentMongoDB/Implements/RentalStorage.cs b/CarRentMongoDB/Implements/RentalStorage.cs new file mode 100644 index 0000000..3606334 --- /dev/null +++ b/CarRentMongoDB/Implements/RentalStorage.cs @@ -0,0 +1,114 @@ +using Bogus; +using CarRentContracts.BindingModels; +using CarRentContracts.SearchModels; +using CarRentContracts.StoragesContracts; +using CarRentContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarRentMongoDB.Implements +{ + public class RentalStorage : IRentalStorage + { + /*public void ClearEntity() + { + + } + public string TestInsertList(int num, + List clients, + List cars) + { + + } + public string TestReadList(int num) + { + + } + public string TestJoinReadList(int num) + { + + } + public RentalViewModel? GetElement(RentalSearchModel model) + { + + } + + public List GetFilteredList(RentalSearchModel model) + { + + } + + public List GetFullList() + { + + } + + public RentalViewModel? Insert(RentalBindingModel model) + { + + } + + public RentalViewModel? Update(RentalBindingModel model) + { + + } + + public RentalViewModel? Delete(RentalBindingModel model) + { + + }*/ + public void ClearEntity() + { + throw new NotImplementedException(); + } + + public RentalViewModel? Delete(RentalBindingModel model) + { + throw new NotImplementedException(); + } + + public RentalViewModel? GetElement(RentalSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFilteredList(RentalSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFullList() + { + throw new NotImplementedException(); + } + + public RentalViewModel? Insert(RentalBindingModel model) + { + throw new NotImplementedException(); + } + + public string TestInsertList(int num, List clients, List cars) + { + throw new NotImplementedException(); + } + + public string TestJoinReadList(int num) + { + throw new NotImplementedException(); + } + + public string TestReadList(int num) + { + throw new NotImplementedException(); + } + + public RentalViewModel? Update(RentalBindingModel model) + { + throw new NotImplementedException(); + } + } +} diff --git a/CarRentMongoDB/Models/Branch.cs b/CarRentMongoDB/Models/Branch.cs new file mode 100644 index 0000000..164afae --- /dev/null +++ b/CarRentMongoDB/Models/Branch.cs @@ -0,0 +1,70 @@ +using CarRentContracts.BindingModels; +using CarRentContracts.ViewModels; +using CarRentDataModels.Models; +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; + +namespace CarRentMongoDB.Models +{ + public class Branch : IBranchModel + { + [BsonId] + [BsonElement("_id")] + [BsonRepresentation(BsonType.ObjectId)] + public string? ObjectId { get; set; } = string.Empty; + + [BsonRequired] + public string Name { get; set; } = string.Empty; + + [BsonRequired] + public string Address { get; set; } = string.Empty; + + [BsonRequired] + public string PhoneNumber { get; set; } = string.Empty; + + public List Cars { get; set; } = new(); + public static Branch? Create(BranchBindingModel model) + { + if (model == null) + { + return null; + } + return new Branch() + { + Name = model.Name, + Address = model.Address, + PhoneNumber = model.PhoneNumber, + }; + } + public void Update(BranchBindingModel model) + { + if (model == null) + { + return; + } + Name = model.Name; + Address = model.Address; + PhoneNumber = model.PhoneNumber; + } + public BranchViewModel GetViewModel => new() + { + ObjectId = ObjectId ?? string.Empty, + Name = Name, + Address = Address, + PhoneNumber = PhoneNumber, + }; + + [BsonIgnoreIfNull] + [BsonIgnoreIfDefault] + public int? Id { get; set; } + } +} diff --git a/CarRentMongoDB/Models/Car.cs b/CarRentMongoDB/Models/Car.cs new file mode 100644 index 0000000..d33b6b7 --- /dev/null +++ b/CarRentMongoDB/Models/Car.cs @@ -0,0 +1,105 @@ +using CarRentContracts.BindingModels; +using CarRentContracts.ViewModels; +using CarRentDataModels.Models; +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; + +namespace CarRentMongoDB.Models +{ + public class Car : ICarModel + { + [BsonId] + [BsonElement("_id")] + [BsonRepresentation(BsonType.ObjectId)] + public string? ObjectId { get; set; } + + [BsonRequired] + public string Brand { get; set; } = string.Empty; + + [BsonRequired] + public string Model { get; set; } = string.Empty; + + [BsonRequired] + [BsonDateTimeOptions(Kind = DateTimeKind.Utc)] + public DateTime YearOfManuf { get; set; } = DateTime.MinValue.ToUniversalTime(); + + [BsonRequired] + public string Color { get; set; } = string.Empty; + + [BsonRequired] + public string LicensePlate { get; set; } = string.Empty; + + [BsonRequired] + public double RentalCostPerHour { get; set; } + + [BsonRequired] + [BsonElement("branch_id")] + public string BranchStringId { get; set; } + + [BsonRequired] + [BsonIgnoreIfNull] + public List RentalCars { get; set; } = new List(); + + [BsonRequired] + [BsonIgnoreIfNull] + public virtual Branch Branch { get; set; } + + public static Car? Create(CarBindingModel model) + { + if (model == null) + { + return null; + } + return new Car() + { + Brand = model.Brand, + Model = model.Model, + YearOfManuf = model.YearOfManuf.ToUniversalTime(), + Color = model.Color, + LicensePlate = model.LicensePlate, + RentalCostPerHour = model.RentalCostPerHour, + BranchStringId = model.BranchStringId, + }; + } + + public void Update(CarBindingModel model) + { + if (model == null) + { + return; + } + Brand = model.Brand; + Model = model.Model; + YearOfManuf = model.YearOfManuf.ToUniversalTime(); + Color = model.Color; + LicensePlate = model.LicensePlate; + RentalCostPerHour = model.RentalCostPerHour; + } + + public CarViewModel GetViewModel => new() + { + ObjectId = ObjectId ?? string.Empty, + Brand = Brand, + Model = Model, + YearOfManuf = YearOfManuf, + Color = Color, + LicensePlate = LicensePlate, + RentalCostPerHour = RentalCostPerHour, + BranchStringId = BranchStringId, + BranchName = Branch?.Name ?? string.Empty, + }; + + [BsonIgnoreIfNull] + [BsonIgnoreIfDefault] + public int? Id { get; set; } + } +} diff --git a/CarRentMongoDB/Models/Client.cs b/CarRentMongoDB/Models/Client.cs new file mode 100644 index 0000000..a9f194d --- /dev/null +++ b/CarRentMongoDB/Models/Client.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using CarRentDataModels.Models; +using CarRentContracts.BindingModels; +using CarRentContracts.ViewModels; +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; + +namespace CarRentMongoDB.Models +{ + public class Client : IClientModel + { + [BsonId] + [BsonElement("_id")] + [BsonRepresentation(BsonType.ObjectId)] + public string? ObjectId { get; set; } + + [BsonRequired] + public string Name { get; set; } = string.Empty; + + [BsonRequired] + public string Surname { get; set; } = string.Empty; + + [BsonRequired] + public string PhoneNumber { get; set; } = string.Empty; + + [BsonRequired] + public string Email { get; set; } = string.Empty; + + [BsonRequired] + public string DriveLicenseNumber { get; set; } = string.Empty; + + [BsonIgnoreIfNull] + [BsonIgnoreIfDefault] + public List RentalClients { get; set; } = new(); + public static Client? Create(ClientBindingModel model) + { + if (model == null) + { + return null; + } + return new Client() + { + Name = model.Name, + Surname = model.Surname, + PhoneNumber = model.PhoneNumber, + Email = model.Email, + DriveLicenseNumber = model.DriveLicenseNumber, + }; + } + public void Update(ClientBindingModel model) + { + if (model == null) + { + return; + } + Name = model.Name; + Surname = model.Surname; + PhoneNumber = model.PhoneNumber; + Email = model.Email; + DriveLicenseNumber = model.DriveLicenseNumber; + } + public ClientViewModel GetViewModel => new() + { + ObjectId = ObjectId ?? string.Empty, + Name = Name, + Surname = Surname, + PhoneNumber = PhoneNumber, + Email = Email, + DriveLicenseNumber = DriveLicenseNumber, + }; + + [BsonIgnoreIfNull] + [BsonIgnoreIfDefault] + public int? Id { get; set; } + } +} diff --git a/CarRentMongoDB/Models/Rental.cs b/CarRentMongoDB/Models/Rental.cs new file mode 100644 index 0000000..db19884 --- /dev/null +++ b/CarRentMongoDB/Models/Rental.cs @@ -0,0 +1,98 @@ +using CarRentContracts.BindingModels; +using CarRentContracts.BusinessLogicContracts; +using CarRentContracts.ViewModels; +using CarRentDataModels.Models; +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarRentMongoDB.Models +{ + public class Rental : IRentalModel + { + [BsonId] + [BsonElement("_id")] + [BsonRepresentation(BsonType.ObjectId)] + public string? ObjectId { get; set; } + + [BsonRequired] + [BsonElement("start_date")] + public DateTime StartDate { get; set; } + + [BsonRequired] + [BsonElement("end_date")] + public DateTime? EndDate { get; set; } + + [BsonRequired] + [BsonElement("rental_cost")] + public double RentalCost { get; set; } + + [BsonElement("review_text")] + public string? ReviewText { get; set; } + + [BsonElement("review_rating")] + public int? ReviewRating { get; set; } + + [BsonRequired] + [BsonElement("client_id")] + public string? ClientStringId { get; set; } + + [BsonRequired] + [BsonElement("car_id")] + public string? CarStringId { get; set; } + public virtual Car Car { get; set; } + public virtual Client Client { get; set; } + + public static Rental? Create(RentalBindingModel? model) + { + if (model == null) + { + return null; + } + return new Rental() + { + StartDate = model.StartDate, + EndDate = model.EndDate, + RentalCost = model.RentalCost, + ReviewText = model.ReviewText, + ReviewRating = model.ReviewRating, + CarStringId = model.CarStringId, + ClientStringId = model.ClientStringId, + }; + } + + public void Update(RentalBindingModel? model) + { + if (model == null) + { + return; + } + ReviewText = model.ReviewText; + ReviewRating = model.ReviewRating; + } + + public RentalViewModel GetViewModel => new() + { + ObjectId = ObjectId ?? string.Empty, + ClientName = Client == null ? string.Empty : Client.Name, + ClientSurname = Client == null ? string.Empty : Client.Surname, + LicensePlate = Car == null ? string.Empty : Car.LicensePlate, + StartDate = StartDate, + EndDate = EndDate, + RentalCost = RentalCost, + ReviewText = ReviewText, + ReviewRating = ReviewRating, + ClientStringId = ClientStringId, + CarStringId = CarStringId, + }; + + [BsonIgnoreIfNull] + [BsonIgnoreIfDefault] + public int? Id { get; set; } + } +}