From fc980a2b4776f918c50becb0ebee88f4650479eb Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Wed, 10 May 2023 12:27:29 +0400 Subject: [PATCH] Global another fiaxtion. --- .../Implements/TransportStorage.cs | 81 +++++++-- .../Implements/TruckingStorage.cs | 157 ++++++++++++++++++ .../Models/Transport.cs | 7 +- .../Models/Trucking.cs | 6 +- TransportCompany/TransportCompany/Program.cs | 32 ++-- .../StoragesContracts/ITransportStorage.cs | 2 +- .../ViewModels/TransportViewModel.cs | 2 + .../ViewModels/TruckingViewModel.cs | 5 + .../Implements/TransportStorage.cs | 2 +- .../Models/Trucking.cs | 1 + 10 files changed, 263 insertions(+), 32 deletions(-) create mode 100644 TransportCompany/TransportCompamyMongoDBImplementer/Implements/TruckingStorage.cs diff --git a/TransportCompany/TransportCompamyMongoDBImplementer/Implements/TransportStorage.cs b/TransportCompany/TransportCompamyMongoDBImplementer/Implements/TransportStorage.cs index 5131a1d..9d629d6 100644 --- a/TransportCompany/TransportCompamyMongoDBImplementer/Implements/TransportStorage.cs +++ b/TransportCompany/TransportCompamyMongoDBImplementer/Implements/TransportStorage.cs @@ -1,4 +1,5 @@ -using System; +using MongoDB.Driver; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -17,41 +18,87 @@ namespace TransportCompamyMongoDBImplementer.Implements public TransportViewModel? Delete(TransportBindingModel model) { - throw new NotImplementedException(); + var transportCollection = context.ConnectToMongo("transport"); + + var element = transportCollection.Find(rec => rec.Id == model.MongoId).FirstOrDefault(); + + if (element != null) + { + transportCollection.DeleteOne(x => x.Id == model.MongoId); + + return element.GetViewModel; + } + + return null; } public TransportViewModel? GetElement(TransportSearchModel model) { - throw new NotImplementedException(); + if (string.IsNullOrEmpty(model.Tranport) && string.IsNullOrEmpty(model.MongoId)) + { + return null; + } + + var transportCollection = context.ConnectToMongo("transport"); + + return transportCollection.Find(x => (!string.IsNullOrEmpty(model.MongoId) && x.Id == model.MongoId) + && x.TransportType == model.Tranport || (!string.IsNullOrEmpty(model.MongoId) && x.Id == model.MongoId)) + .FirstOrDefault() + ?.GetViewModel; } public List GetFilteredList(TransportSearchModel model) { - throw new NotImplementedException(); + if (!model.Id.HasValue) + { + return new(); + } + + var transportCollection = context.ConnectToMongo("transport"); + + return transportCollection.Find(x => x.Id == model.MongoId) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); } public List GetFullList() { - throw new NotImplementedException(); + var transportCollection = context.ConnectToMongo("transport"); + + return transportCollection.Find(_ => true) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); } public TransportViewModel? Insert(TransportBindingModel model) { - throw new NotImplementedException(); + var transportCollection = context.ConnectToMongo("transport"); + + var newTransport = Transport.Create(model); + + if (newTransport == null) + { + return null; + } + + transportCollection.InsertOne(newTransport); + + return newTransport.GetViewModel; } //добавление экземпляров сущности из Postgresql - public bool InsertFromPostgres(List model, List modelT) + public bool InsertFromPostgres(List model) { var transportCollection = context.ConnectToMongo("transport"); List models = model.Select(x => Transport.Create(new() { Tranport = x.Tranport, + TransportationType = "Что-то" })).ToList(); - - transportCollection.InsertMany(models); return true; @@ -59,7 +106,21 @@ namespace TransportCompamyMongoDBImplementer.Implements public TransportViewModel? Update(TransportBindingModel model) { - throw new NotImplementedException(); + var transportCollection = context.ConnectToMongo("transport"); + + var transport = transportCollection.Find(x => x.Id == model.MongoId).FirstOrDefault(); + + if (transport == null) + { + return null; + } + + transport.Update(model); + + var filter = Builders.Filter.Eq("Id", model.MongoId); + transportCollection.ReplaceOne(filter, transport, new ReplaceOptions { IsUpsert = true }); + + return transport.GetViewModel; } } } diff --git a/TransportCompany/TransportCompamyMongoDBImplementer/Implements/TruckingStorage.cs b/TransportCompany/TransportCompamyMongoDBImplementer/Implements/TruckingStorage.cs new file mode 100644 index 0000000..e1540f1 --- /dev/null +++ b/TransportCompany/TransportCompamyMongoDBImplementer/Implements/TruckingStorage.cs @@ -0,0 +1,157 @@ +using MongoDB.Driver; +using System; +using System.Collections.Generic; +using System.Diagnostics.Contracts; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TransportCompamyMongoDBImplementer.Models; +using TransportCompanyContracts.BindingModels; +using TransportCompanyContracts.SearchModels; +using TransportCompanyContracts.StoragesContracts; +using TransportCompanyContracts.ViewModels; + +namespace TransportCompamyMongoDBImplementer.Implements +{ + public class TruckingStorage : ITruckingStorage + { + private DBMSContext context = new(); + + public TruckingViewModel? Delete(TruckingBindingModel model) + { + var truckingCollection = context.ConnectToMongo("trucking"); + + var element = truckingCollection.Find(rec => rec.Id == model.MongoId).FirstOrDefault(); + + if (element != null) + { + truckingCollection.DeleteOne(x => x.Id == model.MongoId); + + return element.GetViewModel; + } + + return null; + } + + public string FirstJoin() + { + throw new NotImplementedException(); + } + + public TruckingViewModel? GetElement(TruckingSearchModel model) + { + + if (!model.Id.HasValue || !model.ClientId.HasValue) + { + return null; + } + + var truckingCollection = context.ConnectToMongo("trucking"); + + return truckingCollection.Find(x => !string.IsNullOrEmpty(model.MongoId) && x.Id == model.MongoId) + .FirstOrDefault() + ?.GetViewModel; + } + + public List GetFilteredList(TruckingSearchModel model) + { + if (string.IsNullOrEmpty(model.MongoId)) + { + return new(); + } + + var truckingCollection = context.ConnectToMongo("trucking"); + + return truckingCollection.Find(x => !string.IsNullOrEmpty(model.MongoId) && x.Id == model.MongoId) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + var truckingCollection = context.ConnectToMongo("trucking"); + + return truckingCollection.Find(_ => true) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public TruckingViewModel? Insert(TruckingBindingModel model) + { + var truckingCollection = context.ConnectToMongo("trucking"); + + var newTrucking = Trucking.Create(context, model); + + if (newTrucking == null) + { + return null; + } + + truckingCollection.InsertOne(newTrucking); + + return newTrucking.GetViewModel; + } + + public bool InsertFromPostgres(List model) + { + var clientCollection = context.ConnectToMongo("client"); + var cargoCollection = context.ConnectToMongo("cargo"); + var transportCollection = context.ConnectToMongo("transport"); + var truckingCollection = context.ConnectToMongo("trucking"); + + //список, который по итогу и будем вставлять + List models = new(); + + foreach(var element in model) + { + var newTrucking = new TruckingBindingModel(); + + newTrucking.DateStart = element.DateStart; + newTrucking.DateEnd = element.DateEnd; + newTrucking.Price = element.Price; + newTrucking.Cargo = cargoCollection.Find(x => x.Cargo.TypeCargo == element.Cargo).FirstOrDefault().Id; + newTrucking.Client = clientCollection.Find(x => x.Client.Email == element.ClientEmail).FirstOrDefault().Id; + newTrucking.Transport = transportCollection.Find(x => x.Transport.TransportType == element.TransportName).FirstOrDefault().Id; + + models.Add(Trucking.Create(context, newTrucking)); + } + + truckingCollection.InsertMany(models); + + return true; + } + + public string TestGetFullList() + { + //может и надо сделать + throw new NotImplementedException(); + } + + public string TestRandomInsert(int count, List clients, List cargos, List transports, List transportations) + { + //может и надо сделать + throw new NotImplementedException(); + } + + public TruckingViewModel? Update(TruckingBindingModel model) + { + var truckingCollection = context.ConnectToMongo("trucking"); + + var trucking = truckingCollection.Find(x => x.Id == model.MongoId).FirstOrDefault(); + + if (trucking == null) + { + return null; + } + + trucking.Update(model); + + var filter = Builders.Filter.Eq("Id", model.MongoId); + truckingCollection.ReplaceOne(filter, trucking, new ReplaceOptions { IsUpsert = true }); + + return trucking.GetViewModel; + } + } +} diff --git a/TransportCompany/TransportCompamyMongoDBImplementer/Models/Transport.cs b/TransportCompany/TransportCompamyMongoDBImplementer/Models/Transport.cs index a98be2d..47cace8 100644 --- a/TransportCompany/TransportCompamyMongoDBImplementer/Models/Transport.cs +++ b/TransportCompany/TransportCompamyMongoDBImplementer/Models/Transport.cs @@ -28,7 +28,8 @@ namespace TransportCompamyMongoDBImplementer.Models return new Transport() { Id = model.MongoId, - TransportType = model.Tranport + TransportType = model.Tranport, + TransportationType = model.TransportationType }; } @@ -36,12 +37,14 @@ namespace TransportCompamyMongoDBImplementer.Models { Id = model.MongoId; TransportType = model.Tranport; + TransportationType = model.TransportationType; } public TransportViewModel GetViewModel => new() { MongoId = Id, - Tranport = TransportType + Tranport = TransportType, + TransportationType = TransportationType }; } } diff --git a/TransportCompany/TransportCompamyMongoDBImplementer/Models/Trucking.cs b/TransportCompany/TransportCompamyMongoDBImplementer/Models/Trucking.cs index b5d1ecf..4eb1236 100644 --- a/TransportCompany/TransportCompamyMongoDBImplementer/Models/Trucking.cs +++ b/TransportCompany/TransportCompamyMongoDBImplementer/Models/Trucking.cs @@ -23,11 +23,11 @@ namespace TransportCompamyMongoDBImplementer.Models public DateTime DateEnd { get; set; } - public virtual Cargo Cargo { get; set; } = null!; + public Cargo Cargo { get; set; } - public virtual Client Client { get; set; } = null!; + public Client Client { get; set; } - public virtual Transport Transport { get; set; } = null!; + public Transport Transport { get; set; } public static Trucking? Create( DBMSContext context, TruckingBindingModel model) { diff --git a/TransportCompany/TransportCompany/Program.cs b/TransportCompany/TransportCompany/Program.cs index 62feabc..07a682f 100644 --- a/TransportCompany/TransportCompany/Program.cs +++ b/TransportCompany/TransportCompany/Program.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; +using TransportCompamyMongoDBImplementer.Implements; using TransportCompanyBusinessLogic.BusinessLogic; using TransportCompanyContracts.BusinessLogicsContracts; using TransportCompanyContracts.StoragesContracts; @@ -39,11 +40,11 @@ namespace TransportCompany option.AddNLog("nlog.config"); }); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -67,6 +68,7 @@ namespace TransportCompany services.AddTransient(); } + // Postgresql public static void ConnectPostgres() { _serviseCollection.Remove(_serviseCollection.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(ICargoStorage))); @@ -75,16 +77,17 @@ namespace TransportCompany _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(); + _serviseCollection.AddTransient(); _serviseCollection.AddTransient(); - _serviseCollection.AddTransient(); + _serviseCollection.AddTransient(); _serviceProvider = _serviseCollection.BuildServiceProvider(); } - /*public static void ConnectMongo() + // MongoDB + public static void ConnectMongo() { _serviseCollection.Remove(_serviseCollection.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(ICargoStorage))); _serviseCollection.Remove(_serviseCollection.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(IClientStorage))); @@ -92,13 +95,12 @@ namespace TransportCompany _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(); + _serviseCollection.AddTransient(); + _serviseCollection.AddTransient(); + _serviseCollection.AddTransient(); + _serviseCollection.AddTransient(); _serviceProvider = _serviseCollection.BuildServiceProvider(); - }*/ + } } } \ No newline at end of file diff --git a/TransportCompany/TransportCompanyContracts/StoragesContracts/ITransportStorage.cs b/TransportCompany/TransportCompanyContracts/StoragesContracts/ITransportStorage.cs index c99aa1a..0df0388 100644 --- a/TransportCompany/TransportCompanyContracts/StoragesContracts/ITransportStorage.cs +++ b/TransportCompany/TransportCompanyContracts/StoragesContracts/ITransportStorage.cs @@ -23,6 +23,6 @@ namespace TransportCompanyContracts.StoragesContracts TransportViewModel? Delete(TransportBindingModel model); - bool InsertFromPostgres(List model, List modelT); + bool InsertFromPostgres(List model); } } diff --git a/TransportCompany/TransportCompanyContracts/ViewModels/TransportViewModel.cs b/TransportCompany/TransportCompanyContracts/ViewModels/TransportViewModel.cs index ed86512..b440cb3 100644 --- a/TransportCompany/TransportCompanyContracts/ViewModels/TransportViewModel.cs +++ b/TransportCompany/TransportCompanyContracts/ViewModels/TransportViewModel.cs @@ -14,6 +14,8 @@ namespace TransportCompanyContracts.ViewModels public string? MongoId { get; set; } + public string? TransportationType { get; set; } + [DisplayName("Вид транспорта")] public string Tranport { get; set; } = string.Empty; } diff --git a/TransportCompany/TransportCompanyContracts/ViewModels/TruckingViewModel.cs b/TransportCompany/TransportCompanyContracts/ViewModels/TruckingViewModel.cs index f278951..9448949 100644 --- a/TransportCompany/TransportCompanyContracts/ViewModels/TruckingViewModel.cs +++ b/TransportCompany/TransportCompanyContracts/ViewModels/TruckingViewModel.cs @@ -10,8 +10,10 @@ namespace TransportCompanyContracts.ViewModels { public class TruckingViewModel : ITruckingModel { + [DisplayName("Номер")] public int Id { get; set; } + [DisplayName("Номер")] public string? MongoId { get; set; } public int ClientId { get; set; } @@ -25,6 +27,9 @@ namespace TransportCompanyContracts.ViewModels [DisplayName("Отчество")] public string ClientPatronymic { get; set; } = string.Empty; + [DisplayName("Почта")] + public string ClientEmail { get; set; } = string.Empty; + public int TransportationId { get; set; } [DisplayName("Тип перевозки")] diff --git a/TransportCompany/TransportCompanyDatabaseImplements/Implements/TransportStorage.cs b/TransportCompany/TransportCompanyDatabaseImplements/Implements/TransportStorage.cs index ced6559..8301c74 100644 --- a/TransportCompany/TransportCompanyDatabaseImplements/Implements/TransportStorage.cs +++ b/TransportCompany/TransportCompanyDatabaseImplements/Implements/TransportStorage.cs @@ -115,7 +115,7 @@ namespace TransportCompanyDatabaseImplements.Implements } } - public bool InsertFromPostgres(List model, List modelT) + public bool InsertFromPostgres(List model) { throw new NotImplementedException(); } diff --git a/TransportCompany/TransportCompanyDatabaseImplements/Models/Trucking.cs b/TransportCompany/TransportCompanyDatabaseImplements/Models/Trucking.cs index dad1135..0b16ea5 100644 --- a/TransportCompany/TransportCompanyDatabaseImplements/Models/Trucking.cs +++ b/TransportCompany/TransportCompanyDatabaseImplements/Models/Trucking.cs @@ -81,6 +81,7 @@ public partial class Trucking ClientName = Client == null ? string.Empty : Client.Name, ClientSurname = Client == null ? string.Empty : Client.Surname, ClientPatronymic = Client == null ? string.Empty : Client.Patronymic, + ClientEmail = Client == null ? string.Empty : Client.Email, TypeTransportation = Transportation == null ? string.Empty : Transportation.TransportationType, TransportName = Transport == null ? string.Empty : Transport.TransportType, Cargo = Cargo == null ? string.Empty : Cargo.TypeCargo