Answer the question
In order to leave comments, you need to log in
How to attach and detach the car to the driver?
For the taxi application, you need to assign a car to a driver, the history of drivers must be saved to the collection of drivers. I can't get the logic right in my head. Help plz.
Drivers
Auto
Model
var DriversSchema = new Schema({
name: {
type: String,
required: true,
trim: true,
unique: true,
dropDups: true
},
id: {
type: Number,
required: true,
unique: true,
dropDups: true
},
car: {
type: Schema.ObjectId,
ref: 'Car'
}
});
var CarsSchema = new Schema({
number: {
type: String,
unique: true,
dropDups: true,
required: true
},
model: {
type: String,
required: true
},
mileage: Number,
color: String,
isBusy: {
type: Boolean,
default: false
},
drivers: [{
date: Date,
driver: {
type: Schema.ObjectId,
ref: 'Driver',
required: true
}
}]
});
$scope.findOne = function() {
Drivers.get({
driverId: $stateParams.driverId
}, function(driver) {
console.log('driver ', driver);
$scope.driver = driver;
$scope.oldCar = driver.car;
$scope.cars.unshift(driver.car);
});
};
$scope.update = function(isValid) {
if (isValid) {
var driver = $scope.driver;
if (!driver.updated) {
driver.updated = [];
}
driver.updated.push(new Date().getTime());
driver.user = MeanUser.user;
driver.acl = MeanUser.acl;
console.log('update driver ', driver);
if(driver.car){
driver.car.isBusy = true;
}
driver.$update(function() {
if($scope.oldCar){
updateCar($scope.oldCar);
}
$location.path('drivers/' + driver._id);
});
}
else {
$scope.submitted = true;
}
};
function updateCar(oldCar) {
Cars.get({carId: oldCar._id}, function (car) {
car.isBusy = false;
car.$update(function (response) {
console.log('OldCar updated ', response);
})
});
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question