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