Промежуточное сохранение. Надеюсь, что на верном пути

This commit is contained in:
Programmist73 2023-05-09 22:54:35 +04:00
parent 6cbdc42514
commit 5a3410d86c
9 changed files with 289 additions and 5 deletions

View File

@ -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<T> ConnectToMongo<T>(in string collection)
{
var client = new MongoClient(ConnectionString);
var db = client.GetDatabase(DatabaseName);
return db.GetCollection<T>(collection);
}
}
}

View File

@ -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<Trucking> Truckings { get; set; } = new List<Trucking>();
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
};
}
}

View File

@ -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<Trucking> Truckings { get; set; } = new List<Trucking>();
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
};
}
}

View File

@ -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<Trucking> Truckings { get; set; } = new List<Trucking>();
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
};
}
}

View File

@ -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>("cargo").Find(x => x.Id == model.Cargo).FirstOrDefault(),
Client = context.ConnectToMongo<Client>("client").Find(x => x.Id == model.Client).FirstOrDefault(),
Transport = context.ConnectToMongo<Transport>("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
};
}
}

View File

@ -14,6 +14,8 @@ namespace TransportCompany
public static ServiceProvider? ServiceProvider => _serviceProvider;
public static ServiceCollection _serviseCollection;
/// <summary>
/// The main entry point for the application.
/// </summary>
@ -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<FormTrucking>());
}
@ -64,5 +66,39 @@ namespace TransportCompany
services.AddTransient<FormTimeCheck>();
services.AddTransient<FormCheckTimeJoin>();
}
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<ICargoStorage, CargoStorage>();
_serviseCollection.AddTransient<IClientStorage, ClientStorage>();
_serviseCollection.AddTransient<ITruckingStorage, TruckingStorage>();
_serviseCollection.AddTransient<ITransportationStorage, TransportationStorage>();
_serviseCollection.AddTransient<ITransportStorage, TransportStorage>();
_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<ICargoStorage, CargoStorage>();
_serviseCollection.AddTransient<IClientStorage, ClientStorage>();
_serviseCollection.AddTransient<ITruckingStorage, TruckingStorage>();
_serviseCollection.AddTransient<ITransportationStorage, TransportationStorage>();
_serviseCollection.AddTransient<ITransportStorage, TransportStorage>();
_serviceProvider = _serviseCollection.BuildServiceProvider();
}*/
}
}

View File

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

View File

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

View File

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