From 5a3410d86c05da5fa7336f3cc3a9d5a94624700c Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Tue, 9 May 2023 22:54:35 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=BE=D0=B5=20=D1=81=D0=BE=D1=85=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5.=20=D0=9D=D0=B0=D0=B4?= =?UTF-8?q?=D0=B5=D1=8E=D1=81=D1=8C,=20=D1=87=D1=82=D0=BE=20=D0=BD=D0=B0?= =?UTF-8?q?=20=D0=B2=D0=B5=D1=80=D0=BD=D0=BE=D0=BC=20=D0=BF=D1=83=D1=82?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DBMSContext.cs | 14 +++- .../Models/Cargo.cs | 43 +++++++++++ .../Models/Client.cs | 63 +++++++++++++++ .../Models/Transport.cs | 45 +++++++++++ .../Models/Trucking.cs | 76 +++++++++++++++++++ TransportCompany/TransportCompany/Program.cs | 42 +++++++++- .../BindingModels/CargoBindingModel.cs | 2 +- .../BindingModels/TransportBindingModel.cs | 3 + .../BindingModels/TruckingBindingModel.cs | 6 ++ 9 files changed, 289 insertions(+), 5 deletions(-) create mode 100644 TransportCompany/TransportCompamyMongoDBImplementer/Models/Cargo.cs create mode 100644 TransportCompany/TransportCompamyMongoDBImplementer/Models/Client.cs create mode 100644 TransportCompany/TransportCompamyMongoDBImplementer/Models/Transport.cs create mode 100644 TransportCompany/TransportCompamyMongoDBImplementer/Models/Trucking.cs diff --git a/TransportCompany/TransportCompamyMongoDBImplementer/DBMSContext.cs b/TransportCompany/TransportCompamyMongoDBImplementer/DBMSContext.cs index 9f8c15c..6fea2f6 100644 --- a/TransportCompany/TransportCompamyMongoDBImplementer/DBMSContext.cs +++ b/TransportCompany/TransportCompamyMongoDBImplementer/DBMSContext.cs @@ -1,4 +1,5 @@ -using System; +using MongoDB.Driver; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,5 +9,16 @@ namespace TransportCompamyMongoDBImplementer { public class DBMSContext { + private const string ConnectionString = "mongodb://192.168.1.135:27017"; + + private const string DatabaseName = "transportcompany"; + + public IMongoCollection ConnectToMongo(in string collection) + { + var client = new MongoClient(ConnectionString); + var db = client.GetDatabase(DatabaseName); + + return db.GetCollection(collection); + } } } diff --git a/TransportCompany/TransportCompamyMongoDBImplementer/Models/Cargo.cs b/TransportCompany/TransportCompamyMongoDBImplementer/Models/Cargo.cs new file mode 100644 index 0000000..d92976e --- /dev/null +++ b/TransportCompany/TransportCompamyMongoDBImplementer/Models/Cargo.cs @@ -0,0 +1,43 @@ +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TransportCompanyContracts.BindingModels; +using TransportCompanyContracts.ViewModels; + +namespace TransportCompamyMongoDBImplementer.Models +{ + public class Cargo + { + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string Id { get; set; } + + public string TypeCargo { get; set; } = null!; + + //public virtual ICollection Truckings { get; set; } = new List(); + + public static Cargo Create(CargoBindingModel model) + { + return new Cargo() + { + Id = model.MongoId, + TypeCargo = model.TypeCargo + }; + } + + public void Update(CargoBindingModel model) + { + TypeCargo = model.TypeCargo; + } + + public CargoViewModel GetViewModel => new() + { + MongoId = Id, + TypeCargo = TypeCargo + }; + } +} diff --git a/TransportCompany/TransportCompamyMongoDBImplementer/Models/Client.cs b/TransportCompany/TransportCompamyMongoDBImplementer/Models/Client.cs new file mode 100644 index 0000000..c077503 --- /dev/null +++ b/TransportCompany/TransportCompamyMongoDBImplementer/Models/Client.cs @@ -0,0 +1,63 @@ +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TransportCompanyContracts.BindingModels; +using TransportCompanyContracts.ViewModels; + +namespace TransportCompamyMongoDBImplementer.Models +{ + public class Client + { + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string Id { get; set; } + + public string Name { get; set; } = null!; + + public string Surname { get; set; } = null!; + + public string Patronymic { get; set; } = null!; + + public string Telephone { get; set; } = null!; + + public string Email { get; set; } = null!; + + //public virtual ICollection Truckings { get; set; } = new List(); + + public static Client Create(ClientBindingModel model) + { + return new Client() + { + Id = model.MongoId, + Name = model.Name, + Surname = model.Surname, + Patronymic = model.Patronymic, + Telephone = model.Telephone, + Email = model.Email, + }; + } + + public void Update(ClientBindingModel model) + { + Name = model.Name; + Surname = model.Surname; + Patronymic = model.Patronymic; + Telephone = model.Telephone; + Email = model.Email; + } + + public ClientViewModel GetViewModel => new() + { + MongoId = Id, + Name = Name, + Surname = Surname, + Patronymic = Patronymic, + Telephone = Telephone, + Email = Email + }; + } +} diff --git a/TransportCompany/TransportCompamyMongoDBImplementer/Models/Transport.cs b/TransportCompany/TransportCompamyMongoDBImplementer/Models/Transport.cs new file mode 100644 index 0000000..82f0506 --- /dev/null +++ b/TransportCompany/TransportCompamyMongoDBImplementer/Models/Transport.cs @@ -0,0 +1,45 @@ +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TransportCompanyContracts.BindingModels; +using TransportCompanyContracts.ViewModels; + +namespace TransportCompamyMongoDBImplementer.Models +{ + public class Transport + { + + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string Id { get; set; } + + public string TransportType { get; set; } = null!; + + //public virtual ICollection Truckings { get; set; } = new List(); + + public static Transport Create(TransportBindingModel model) + { + return new Transport() + { + Id = model.MongoId, + TransportType = model.Tranport + }; + } + + public void Update(TransportBindingModel model) + { + Id = model.MongoId; + TransportType = model.Tranport; + } + + public TransportViewModel GetViewModel => new() + { + MongoId = Id, + Tranport = TransportType + }; + } +} diff --git a/TransportCompany/TransportCompamyMongoDBImplementer/Models/Trucking.cs b/TransportCompany/TransportCompamyMongoDBImplementer/Models/Trucking.cs new file mode 100644 index 0000000..b5d1ecf --- /dev/null +++ b/TransportCompany/TransportCompamyMongoDBImplementer/Models/Trucking.cs @@ -0,0 +1,76 @@ +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TransportCompanyContracts.BindingModels; +using TransportCompanyContracts.ViewModels; +using MongoDB.Driver; + +namespace TransportCompamyMongoDBImplementer.Models +{ + public class Trucking + { + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string Id { get; set; } + + public double Price { get; set; } + + public DateTime DateStart { get; set; } + + public DateTime DateEnd { get; set; } + + public virtual Cargo Cargo { get; set; } = null!; + + public virtual Client Client { get; set; } = null!; + + public virtual Transport Transport { get; set; } = null!; + + public static Trucking? Create( DBMSContext context, TruckingBindingModel model) + { + if (model == null) + { + return null; + } + + return new Trucking() + { + Id = model.MongoId, + Price = model.Price, + DateStart = model.DateStart, + DateEnd = model.DateEnd, + Cargo = context.ConnectToMongo("cargo").Find(x => x.Id == model.Cargo).FirstOrDefault(), + Client = context.ConnectToMongo("client").Find(x => x.Id == model.Client).FirstOrDefault(), + Transport = context.ConnectToMongo("transport").Find(x => x.Id == model.Transport).FirstOrDefault() + }; + } + + public void Update(TruckingBindingModel model) + { + if (model == null) + { + return; + } + + Price = model.Price; + DateStart = model.DateStart; + DateEnd = model.DateEnd; + } + + public TruckingViewModel GetViewModel => new() + { + MongoId = Id, + Price = Price, + DateStart = DateStart, + DateEnd = DateEnd, + ClientName = Client == null ? string.Empty : Client.Name, + ClientSurname = Client == null ? string.Empty : Client.Surname, + ClientPatronymic = Client == null ? string.Empty : Client.Patronymic, + TransportName = Transport == null ? string.Empty : Transport.TransportType, + Cargo = Cargo == null ? string.Empty : Cargo.TypeCargo + }; + } +} diff --git a/TransportCompany/TransportCompany/Program.cs b/TransportCompany/TransportCompany/Program.cs index fcba0f4..62feabc 100644 --- a/TransportCompany/TransportCompany/Program.cs +++ b/TransportCompany/TransportCompany/Program.cs @@ -14,6 +14,8 @@ namespace TransportCompany public static ServiceProvider? ServiceProvider => _serviceProvider; + public static ServiceCollection _serviseCollection; + /// /// The main entry point for the application. /// @@ -23,9 +25,9 @@ namespace TransportCompany // To customize application configuration such as set high DPI settings or default font; // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - var services = new ServiceCollection(); - ConfigureServices(services); - _serviceProvider = services.BuildServiceProvider(); + _serviseCollection = new ServiceCollection(); + ConfigureServices(_serviseCollection); + _serviceProvider = _serviseCollection.BuildServiceProvider(); Application.Run(_serviceProvider.GetRequiredService()); } @@ -64,5 +66,39 @@ namespace TransportCompany services.AddTransient(); services.AddTransient(); } + + public static void ConnectPostgres() + { + _serviseCollection.Remove(_serviseCollection.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(ICargoStorage))); + _serviseCollection.Remove(_serviseCollection.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(IClientStorage))); + _serviseCollection.Remove(_serviseCollection.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(ITruckingStorage))); + _serviseCollection.Remove(_serviseCollection.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(ITransportStorage))); + _serviseCollection.Remove(_serviseCollection.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(ITransportationStorage))); + + _serviseCollection.AddTransient(); + _serviseCollection.AddTransient(); + _serviseCollection.AddTransient(); + _serviseCollection.AddTransient(); + _serviseCollection.AddTransient(); + + _serviceProvider = _serviseCollection.BuildServiceProvider(); + } + + /*public static void ConnectMongo() + { + _serviseCollection.Remove(_serviseCollection.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(ICargoStorage))); + _serviseCollection.Remove(_serviseCollection.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(IClientStorage))); + _serviseCollection.Remove(_serviseCollection.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(ITruckingStorage))); + _serviseCollection.Remove(_serviseCollection.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(ITransportStorage))); + _serviseCollection.Remove(_serviseCollection.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(ITransportationStorage))); + + _serviseCollection.AddTransient(); + _serviseCollection.AddTransient(); + _serviseCollection.AddTransient(); + _serviseCollection.AddTransient(); + _serviseCollection.AddTransient(); + + _serviceProvider = _serviseCollection.BuildServiceProvider(); + }*/ } } \ No newline at end of file diff --git a/TransportCompany/TransportCompanyContracts/BindingModels/CargoBindingModel.cs b/TransportCompany/TransportCompanyContracts/BindingModels/CargoBindingModel.cs index d660d35..0bd6167 100644 --- a/TransportCompany/TransportCompanyContracts/BindingModels/CargoBindingModel.cs +++ b/TransportCompany/TransportCompanyContracts/BindingModels/CargoBindingModel.cs @@ -11,7 +11,7 @@ namespace TransportCompanyContracts.BindingModels { public int Id { get; set; } - public string? MongoID { get; set; } + public string? MongoId { get; set; } public string TypeCargo { get; set; } = string.Empty; } diff --git a/TransportCompany/TransportCompanyContracts/BindingModels/TransportBindingModel.cs b/TransportCompany/TransportCompanyContracts/BindingModels/TransportBindingModel.cs index 3acb6f0..c326d62 100644 --- a/TransportCompany/TransportCompanyContracts/BindingModels/TransportBindingModel.cs +++ b/TransportCompany/TransportCompanyContracts/BindingModels/TransportBindingModel.cs @@ -13,6 +13,9 @@ namespace TransportCompanyContracts.BindingModels public string? MongoId { get; set; } + //для MongoDB + public string? TransportationType { get; set; } + public string Tranport { get; set; } = string.Empty; } } diff --git a/TransportCompany/TransportCompanyContracts/BindingModels/TruckingBindingModel.cs b/TransportCompany/TransportCompanyContracts/BindingModels/TruckingBindingModel.cs index 343c169..4fecbed 100644 --- a/TransportCompany/TransportCompanyContracts/BindingModels/TruckingBindingModel.cs +++ b/TransportCompany/TransportCompanyContracts/BindingModels/TruckingBindingModel.cs @@ -26,5 +26,11 @@ namespace TransportCompanyContracts.BindingModels public int TransportationId { get; set; } public int TransportId { get; set; } + + public string Client { get; set; } + + public string Cargo { get; set; } + + public string Transport { get; set; } } }