private static Car ToShortRentsModel(DB.Cars dbItem, UnitOfWork unitOfWork) { var car = new Car() { Id = dbItem.Id, Color = (CarColor)dbItem.Color, BodyNumber = dbItem.BodyNumber, Enginge = dbItem.Engine, Kpp = dbItem.Kpp, Number = dbItem.Number, Sts = dbItem.Sts, TO = dbItem.TO, Year = dbItem.Year, Insurance = dbItem.Insurance, Model = CarModels.Read(dbItem.Model.Id, unitOfWork), Mileage = dbItem.Mileage, SiteId = dbItem.SiteId, User = Users.Read(dbItem.User.Id, unitOfWork), Deleted = dbItem.Deleted }; if (dbItem.CarRental != null) { car.CarRental = CarRentals.Read(dbItem.CarRental.Id, unitOfWork); } foreach (var rent in dbItem.RentsCollection) { car.Rents.Add(Rents.Read(rent.Id, unitOfWork)); } return(car); }
private static int AddNew(Car item) { using (var unitOfWork = new UnitOfWork()) { DB.Cars dbItem; using (var items = new XPCollection <DB.Cars>(unitOfWork)) { dbItem = new DB.Cars(unitOfWork) { Color = (int)item.Color, Engine = item.Enginge, Kpp = item.Kpp, Number = item.Number, Sts = item.Sts, TO = item.TO, Year = item.Year, Body = (int)item.BodyType, Insurance = item.Insurance, BodyNumber = item.BodyNumber, Mileage = item.Mileage, SiteId = item.SiteId, Deleted = item.Deleted }; items.Add(FillDependencies(unitOfWork, dbItem, item)); } unitOfWork.CommitChanges(); AddDependencies(unitOfWork, item, dbItem.Id); unitOfWork.CommitChanges(); return(dbItem.Id); } }
private static Car ToModel(DB.Cars dbItem, UnitOfWork unitOfWork) { var car = new Car() { Id = dbItem.Id, Color = (CarColor)dbItem.Color, BodyType = (Body)dbItem.Body, BodyNumber = dbItem.BodyNumber, Enginge = dbItem.Engine, Kpp = dbItem.Kpp, Number = dbItem.Number, Sts = dbItem.Sts, TO = dbItem.TO, Year = dbItem.Year, Insurance = dbItem.Insurance, Model = CarModels.Read(dbItem.Model.Id, unitOfWork), Mileage = dbItem.Mileage, SiteId = dbItem.SiteId, User = Users.Read(dbItem.User.Id, unitOfWork), Deleted = dbItem.Deleted }; if (dbItem.CarRental != null) { car.CarRental = CarRentals.Read(dbItem.CarRental.Id, unitOfWork); } foreach (var damage in dbItem.DamagesCollection.Where(damage => !damage.Archive)) { car.Damages.Add(Damages.Read(damage.Id, unitOfWork)); } foreach (var rent in dbItem.RentsCollection) { car.Rents.Add(Rents.Read(rent.Id, unitOfWork)); } foreach (var order in dbItem.OrdersCollection.Where(q => !q.Deleted)) { var cor = new CarOrder() { ClientId = order.Client.Id, DateFrom = order.DateFrom, DateTo = order.DateTo, Id = order.Id, Territory = order.Territory }; foreach (var serv in order.OrderServicesCollection) { cor.Services.Add(Services.Read(serv.Service.Id, unitOfWork)); } car.Orders.Add(cor); } return(car); }
private static DB.Cars FillDependencies(UnitOfWork unitOfWork, DB.Cars dbItem, Car item) { using (var items = new XPCollection <DB.CarModels>(unitOfWork)) { dbItem.Model = items.FirstOrDefault(q => q.Id == item.Model.Id); } using (var items = new XPCollection <DB.Users>(unitOfWork)) { dbItem.User = items.FirstOrDefault(q => q.Id == item.User.Id); } using (var items = new XPCollection <DB.CarRentals>(unitOfWork)) { dbItem.CarRental = items.FirstOrDefault(q => q.Id == item.CarRental.Id); } return(dbItem); }