Global another fiaxtion.

This commit is contained in:
Programmist73 2023-05-10 12:27:29 +04:00
parent 54155b8a5e
commit fc980a2b47
10 changed files with 263 additions and 32 deletions

View File

@ -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>("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>("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<TransportViewModel> GetFilteredList(TransportSearchModel model)
{
throw new NotImplementedException();
if (!model.Id.HasValue)
{
return new();
}
var transportCollection = context.ConnectToMongo<Transport>("transport");
return transportCollection.Find(x => x.Id == model.MongoId)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<TransportViewModel> GetFullList()
{
throw new NotImplementedException();
var transportCollection = context.ConnectToMongo<Transport>("transport");
return transportCollection.Find(_ => true)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public TransportViewModel? Insert(TransportBindingModel model)
{
throw new NotImplementedException();
var transportCollection = context.ConnectToMongo<Transport>("transport");
var newTransport = Transport.Create(model);
if (newTransport == null)
{
return null;
}
transportCollection.InsertOne(newTransport);
return newTransport.GetViewModel;
}
//добавление экземпляров сущности из Postgresql
public bool InsertFromPostgres(List<TransportViewModel> model, List<TransportationViewModel> modelT)
public bool InsertFromPostgres(List<TransportViewModel> model)
{
var transportCollection = context.ConnectToMongo<Transport>("transport");
List<Transport> 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>("transport");
var transport = transportCollection.Find(x => x.Id == model.MongoId).FirstOrDefault();
if (transport == null)
{
return null;
}
transport.Update(model);
var filter = Builders<Transport>.Filter.Eq("Id", model.MongoId);
transportCollection.ReplaceOne(filter, transport, new ReplaceOptions { IsUpsert = true });
return transport.GetViewModel;
}
}
}

View File

@ -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>("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>("trucking");
return truckingCollection.Find(x => !string.IsNullOrEmpty(model.MongoId) && x.Id == model.MongoId)
.FirstOrDefault()
?.GetViewModel;
}
public List<TruckingViewModel> GetFilteredList(TruckingSearchModel model)
{
if (string.IsNullOrEmpty(model.MongoId))
{
return new();
}
var truckingCollection = context.ConnectToMongo<Trucking>("trucking");
return truckingCollection.Find(x => !string.IsNullOrEmpty(model.MongoId) && x.Id == model.MongoId)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<TruckingViewModel> GetFullList()
{
var truckingCollection = context.ConnectToMongo<Trucking>("trucking");
return truckingCollection.Find(_ => true)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public TruckingViewModel? Insert(TruckingBindingModel model)
{
var truckingCollection = context.ConnectToMongo<Trucking>("trucking");
var newTrucking = Trucking.Create(context, model);
if (newTrucking == null)
{
return null;
}
truckingCollection.InsertOne(newTrucking);
return newTrucking.GetViewModel;
}
public bool InsertFromPostgres(List<TruckingViewModel> model)
{
var clientCollection = context.ConnectToMongo<Trucking>("client");
var cargoCollection = context.ConnectToMongo<Trucking>("cargo");
var transportCollection = context.ConnectToMongo<Trucking>("transport");
var truckingCollection = context.ConnectToMongo<Trucking>("trucking");
//список, который по итогу и будем вставлять
List<Trucking> 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<ClientViewModel> clients, List<CargoViewModel> cargos, List<TransportViewModel> transports, List<TransportationViewModel> transportations)
{
//может и надо сделать
throw new NotImplementedException();
}
public TruckingViewModel? Update(TruckingBindingModel model)
{
var truckingCollection = context.ConnectToMongo<Trucking>("trucking");
var trucking = truckingCollection.Find(x => x.Id == model.MongoId).FirstOrDefault();
if (trucking == null)
{
return null;
}
trucking.Update(model);
var filter = Builders<Trucking>.Filter.Eq("Id", model.MongoId);
truckingCollection.ReplaceOne(filter, trucking, new ReplaceOptions { IsUpsert = true });
return trucking.GetViewModel;
}
}
}

View File

@ -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
};
}
}

View File

@ -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)
{

View File

@ -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<ICargoStorage, CargoStorage>();
services.AddTransient<IClientStorage, ClientStorage>();
services.AddTransient<ITransportStorage, TransportStorage>();
services.AddTransient<ICargoStorage, TransportCompanyDatabaseImplements.Implements.CargoStorage>();
services.AddTransient<IClientStorage, TransportCompanyDatabaseImplements.Implements.ClientStorage>();
services.AddTransient<ITransportStorage, TransportCompanyDatabaseImplements.Implements.TransportStorage>();
services.AddTransient<ITransportationStorage, TransportationStorage>();
services.AddTransient<ITruckingStorage, TruckingStorage>();
services.AddTransient<ITruckingStorage, TransportCompanyDatabaseImplements.Implements.TruckingStorage>();
services.AddTransient<ICargoLogic, CargoLogic>();
services.AddTransient<IClientLogic, ClientLogic>();
@ -67,6 +68,7 @@ namespace TransportCompany
services.AddTransient<FormCheckTimeJoin>();
}
//ðàáîòà ñ 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<ICargoStorage, CargoStorage>();
_serviseCollection.AddTransient<IClientStorage, ClientStorage>();
_serviseCollection.AddTransient<ITruckingStorage, TruckingStorage>();
_serviseCollection.AddTransient<ICargoStorage, TransportCompanyDatabaseImplements.Implements.CargoStorage>();
_serviseCollection.AddTransient<IClientStorage, TransportCompanyDatabaseImplements.Implements.ClientStorage>();
_serviseCollection.AddTransient<ITruckingStorage, TransportCompanyDatabaseImplements.Implements.TruckingStorage>();
_serviseCollection.AddTransient<ITransportationStorage, TransportationStorage>();
_serviseCollection.AddTransient<ITransportStorage, TransportStorage>();
_serviseCollection.AddTransient<ITransportStorage, TransportCompanyDatabaseImplements.Implements.TransportStorage>();
_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<ICargoStorage, CargoStorage>();
_serviseCollection.AddTransient<IClientStorage, ClientStorage>();
_serviseCollection.AddTransient<ITruckingStorage, TruckingStorage>();
_serviseCollection.AddTransient<ITransportationStorage, TransportationStorage>();
_serviseCollection.AddTransient<ITransportStorage, TransportStorage>();
_serviseCollection.AddTransient<ICargoStorage, TransportCompamyMongoDBImplementer.Implements.CargoStorage>();
_serviseCollection.AddTransient<IClientStorage, TransportCompamyMongoDBImplementer.Implements.ClientStorage>();
_serviseCollection.AddTransient<ITruckingStorage, TransportCompamyMongoDBImplementer.Implements.TruckingStorage>();
_serviseCollection.AddTransient<ITransportStorage, TransportCompamyMongoDBImplementer.Implements.TransportStorage>();
_serviceProvider = _serviseCollection.BuildServiceProvider();
}*/
}
}
}

View File

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

View File

@ -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;
}

View File

@ -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("Тип перевозки")]

View File

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

View File

@ -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