Another fixation.

This commit is contained in:
Programmist73 2023-05-10 00:23:01 +04:00
parent baf0177016
commit 54155b8a5e
5 changed files with 274 additions and 2 deletions

View File

@ -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<ClientViewModel> GetFullList()
{
var clientCollection = context.ConnectToMongo<Client>("client");
return clientCollection.Find(_ => true)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.MongoId))
{
return new();
}
var clientCollection = context.ConnectToMongo<Client>("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>("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>("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<Trucking>()
from c in context.Set<Client>().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>("client");
var client = clientCollection.Find(x => x.Id == model.MongoId).FirstOrDefault();
if (client == null)
{
return null;
}
client.Update(model);
var filter = Builders<Client>.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>("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<ClientViewModel> model)
{
var clientCollection = context.ConnectToMongo<Client>("client");
List<Client> 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;
}
}
}

View File

@ -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<TransportViewModel> GetFilteredList(TransportSearchModel model)
{
throw new NotImplementedException();
}
public List<TransportViewModel> GetFullList()
{
throw new NotImplementedException();
}
public TransportViewModel? Insert(TransportBindingModel model)
{
throw new NotImplementedException();
}
//добавление экземпляров сущности из Postgresql
public bool InsertFromPostgres(List<TransportViewModel> model, List<TransportationViewModel> modelT)
{
var transportCollection = context.ConnectToMongo<Transport>("transport");
List<Transport> models = model.Select(x => Transport.Create(new()
{
Tranport = x.Tranport,
})).ToList();
transportCollection.InsertMany(models);
return true;
}
public TransportViewModel? Update(TransportBindingModel model)
{
throw new NotImplementedException();
}
}
}

View File

@ -19,6 +19,8 @@ namespace TransportCompamyMongoDBImplementer.Models
public string TransportType { get; set; } = null!; public string TransportType { get; set; } = null!;
public string TransportationType { get; set; } = null!;
//public virtual ICollection<Trucking> Truckings { get; set; } = new List<Trucking>(); //public virtual ICollection<Trucking> Truckings { get; set; } = new List<Trucking>();
public static Transport Create(TransportBindingModel model) public static Transport Create(TransportBindingModel model)

View File

@ -23,6 +23,6 @@ namespace TransportCompanyContracts.StoragesContracts
TransportViewModel? Delete(TransportBindingModel model); TransportViewModel? Delete(TransportBindingModel model);
bool InsertFromPostgres(List<TransportViewModel> model); bool InsertFromPostgres(List<TransportViewModel> model, List<TransportationViewModel> modelT);
} }
} }

View File

@ -115,7 +115,7 @@ namespace TransportCompanyDatabaseImplements.Implements
} }
} }
public bool InsertFromPostgres(List<TransportViewModel> model) public bool InsertFromPostgres(List<TransportViewModel> model, List<TransportationViewModel> modelT)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }