diff --git a/TransportCompany/TransportCompamyMongoDBImplementer/Implements/ClientStorage.cs b/TransportCompany/TransportCompamyMongoDBImplementer/Implements/ClientStorage.cs new file mode 100644 index 0000000..e447f20 --- /dev/null +++ b/TransportCompany/TransportCompamyMongoDBImplementer/Implements/ClientStorage.cs @@ -0,0 +1,205 @@ +using MongoDB.Driver; +using System; +using System.Collections.Generic; +using System.Diagnostics; +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 ClientStorage : IClientStorage + { + private DBMSContext context = new(); + + public List GetFullList() + { + var clientCollection = context.ConnectToMongo("client"); + + return clientCollection.Find(_ => true) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.MongoId)) + { + return new(); + } + + var clientCollection = context.ConnectToMongo("client"); + + return clientCollection.Find(x => x.Id.Contains(model.MongoId)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && string.IsNullOrEmpty(model.MongoId)) + { + return null; + } + + var clientCollection = context.ConnectToMongo("client"); + + return clientCollection.Find(x => (!string.IsNullOrEmpty(model.MongoId) && x.Id == model.MongoId)) + .FirstOrDefault() + ?.GetViewModel; + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + var clientCollection = context.ConnectToMongo("client"); + + var newClient = Client.Create(model); + + if (newClient == null) + { + return null; + } + + clientCollection.InsertOne(newClient); + + return newClient.GetViewModel; + } + + //метод для замера вставки большого кол-ва клиентов в бд + public string TestRandomInsert(int count, string[] _name, string[] _surname, string[] _patronymic, string[] _telephone, string[] _email) + { + /*using var context = new ElegevContext(); + + Random rnd = new Random(DateTime.Now.ToString().GetHashCode()); + + int lastId = context.Clients.Count() > 0 ? context.Clients.Max(x => x.Id) + 1 : 1; + + for (int i = 0; i < count; i++) + { + var model = new Client + { + Id = lastId, + Name = _name[rnd.Next(0, _name.Length)], + Surname = _surname[rnd.Next(0, _surname.Length)], + Patronymic = _patronymic[rnd.Next(0, _patronymic.Length)], + Telephone = _telephone[rnd.Next(0, _telephone.Length)], + Email = _email[rnd.Next(0, _email.Length)], + }; + + lastId++; + + context.Clients.Add(model); + } + + //старт замера времени добавления в бд + Stopwatch stopwatch = new(); + + stopwatch.Start(); + + context.SaveChanges(); + + stopwatch.Stop(); + + return stopwatch.ElapsedMilliseconds.ToString();*/ + + return null; + } + + + public string SecondJoin() + { + /*using var context = new ElegevContext(); + + Random rnd = new Random(DateTime.Now.ToString().GetHashCode()); + + //старт замера времени добавления в бд + Stopwatch stopwatch = new(); + + stopwatch.Start(); + + var secondJoin = from t in context.Set() + from c in context.Set().Where(c => c.Id == t.ClientId) + select new { c, t }; + + //ВСЁ ГОТОВО ДЛЯ СЛЕДУЮЩЕГО ЗАМЕРА + + foreach (var element in secondJoin) + { + element.t.Update(new TruckingBindingModel + { + Id = element.t.Id, + ClientId = element.t.ClientId, + CargoId = element.t.CargoId, + Price = element.t.Price, + DateStart = element.t.DateStart.AddDays(-10), + DateEnd = element.t.DateEnd.AddDays(-10), + TransportationId = element.t.TransportationId, + TransportId = element.t.TransportId, + }); + } + + context.SaveChanges(); + + stopwatch.Stop(); + + return stopwatch.ElapsedMilliseconds.ToString();*/ + + return null; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + var clientCollection = context.ConnectToMongo("client"); + + var client = clientCollection.Find(x => x.Id == model.MongoId).FirstOrDefault(); + + if (client == null) + { + return null; + } + + client.Update(model); + + var filter = Builders.Filter.Eq("Id", model.MongoId); + clientCollection.ReplaceOne(filter, client, new ReplaceOptions { IsUpsert = true }); + + return client.GetViewModel; + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + var clientCollection = context.ConnectToMongo("client"); + + var element = clientCollection.Find(rec => rec.Id == model.MongoId).FirstOrDefault(); + + if (element != null) + { + clientCollection.DeleteOne(x => x.Id == model.MongoId); + + return element.GetViewModel; + } + + return null; + } + + //добавление экземпляров сущности из Postgresql + public bool InsertFromPostgres(List model) + { + var clientCollection = context.ConnectToMongo("client"); + + List models = model.Select(x => Client.Create(new() { Name = x.Name, Surname = x.Surname, + Patronymic = x.Patronymic, Email = x.Email, Telephone = x.Telephone })).ToList(); + + clientCollection.InsertMany(models); + + return true; + } + } +} diff --git a/TransportCompany/TransportCompamyMongoDBImplementer/Implements/TransportStorage.cs b/TransportCompany/TransportCompamyMongoDBImplementer/Implements/TransportStorage.cs new file mode 100644 index 0000000..5131a1d --- /dev/null +++ b/TransportCompany/TransportCompamyMongoDBImplementer/Implements/TransportStorage.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +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 TransportStorage : ITransportStorage + { + private DBMSContext context = new(); + + public TransportViewModel? Delete(TransportBindingModel model) + { + throw new NotImplementedException(); + } + + public TransportViewModel? GetElement(TransportSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFilteredList(TransportSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFullList() + { + throw new NotImplementedException(); + } + + public TransportViewModel? Insert(TransportBindingModel model) + { + throw new NotImplementedException(); + } + + //добавление экземпляров сущности из Postgresql + public bool InsertFromPostgres(List model, List modelT) + { + var transportCollection = context.ConnectToMongo("transport"); + + List models = model.Select(x => Transport.Create(new() + { + Tranport = x.Tranport, + })).ToList(); + + + + transportCollection.InsertMany(models); + + return true; + } + + public TransportViewModel? Update(TransportBindingModel model) + { + throw new NotImplementedException(); + } + } +} diff --git a/TransportCompany/TransportCompamyMongoDBImplementer/Models/Transport.cs b/TransportCompany/TransportCompamyMongoDBImplementer/Models/Transport.cs index 82f0506..a98be2d 100644 --- a/TransportCompany/TransportCompamyMongoDBImplementer/Models/Transport.cs +++ b/TransportCompany/TransportCompamyMongoDBImplementer/Models/Transport.cs @@ -19,6 +19,8 @@ namespace TransportCompamyMongoDBImplementer.Models public string TransportType { get; set; } = null!; + public string TransportationType { get; set; } = null!; + //public virtual ICollection Truckings { get; set; } = new List(); public static Transport Create(TransportBindingModel model) diff --git a/TransportCompany/TransportCompanyContracts/StoragesContracts/ITransportStorage.cs b/TransportCompany/TransportCompanyContracts/StoragesContracts/ITransportStorage.cs index 0df0388..c99aa1a 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); + bool InsertFromPostgres(List model, List modelT); } } diff --git a/TransportCompany/TransportCompanyDatabaseImplements/Implements/TransportStorage.cs b/TransportCompany/TransportCompanyDatabaseImplements/Implements/TransportStorage.cs index 8301c74..ced6559 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) + public bool InsertFromPostgres(List model, List modelT) { throw new NotImplementedException(); }