A
A
Ayan Bai2016-05-23 21:49:45
JavaScript
Ayan Bai, 2016-05-23 21:49:45

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
23rMPkO.png
Auto
O55k0gQ.png
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'
    }
});

Auto
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
        }
    }]
});

I find the driver and save a copy of the car
$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);
      });
    };

I update the document
$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);
        })
      });
    }

How to make a change of car, save the drivers who previously drove it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan, 2016-05-23
@wolf47

In my opinion the task is more than SQL.
3 tables - drivers, cars, driver id - car id - time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question