using remote Methode check if one date is between

2019-08-16 16:48发布

The models are as follows:

car 
   - id(number),brand(String),model(String),color(String)
bookingCar
   - id (number),carId (number),startDate(date),endDate(date),location(string),carType(Sring)

Relationships:

car has many bookingCar (foreign key: carId)
bookingCar belongs to car (foreign key: carId)

Now I want to filter data from car model based on which is not booking in the selected date period and location,carType.

so i refer remote methode and I used lb remote-methode. and based on this link enter link description here I wrote a method

'use strict';

module.exports = function(Bookingcar) {
var BookingCar = require('./booking-car.json');

BookingCar.findCar = function(location, startDate, endDate, carType, callback) {
    var results;
    // TODO
    const d1 = startDate.split("-");
    const d2 = endDate.split("-");
    var c = dateCheck.split("-");

    var from = new Date(d1[2], parseInt(d1[1])-1, d1[0]);  
    var to   = new Date(d2[2], parseInt(d2[1])-1, d2[0]);
    var check = new Date(c[2], parseInt(c[1])-1, c[0]);

    if((check <= to && check >= from)){
        var filter = { include: [
        'brand', 
        'model', 
        'color', 
        'carType',
        'revenue_li_cpy',
        'revenue_li_ex_date',  
        'ins_cpy',
        'ins_cpy',   
        'ins_exp_date',
        'car_photo',   
        'ownerNICId_companyName'],
         fields: [
         'id','brand', 
         'model', 
         'color', 
         'carType',
         'revenue_li_cpy',
         'revenue_li_ex_date',  
         'ins_cpy',
         'ins_cpy',   
         'ins_exp_date',
         'car_photo',   
         'ownerNICId_companyName'], where: { and: [{ location: location }, { carType: carType },{ startDate: d1 },{ endDate: d2 }, { status: 1 }] } };
    }
    Bookingcar.find(location, startDate, endDate, carType, {

            include: [{
            relation: 'car'}
        ]},
        function(err, results) {
            if (err)
                console.log(err);
            else {
                callback(null, results);
            }
        });

  };

  Bookingcar.remoteMethod(
    'findCar', {
        http: {path: '/findCar', verb: 'get'},
        accepts: [
        {arg: 'location', type: 'string'},
        {arg: 'startDate', type: 'date'},
        {arg: 'endDate', type: 'date'},
        {arg: 'carType', type: 'string'}
    ],
        returns: [
        { arg: 'id', type: 'number', description: 'id', required: true, root: true },
        { arg: 'brand', type: 'string',  required: true, root: true },
        { arg: 'model', type: 'string', required: false, root: true },
        { arg: 'color', type: 'string',  required: true, root: true },
        { arg: 'carType', type: 'string', required: false, root: true },
        { arg: 'regiNo', type: 'string',  required: true, root: true },
        { arg: 'revenue_li_cpy', type: 'string', required: false, root: true },
        { arg: 'revenue_li_ex_date', type: 'date',  required: true, root: true },
        { arg: 'ins_cpy', type: 'string', required: false, root: true },
        { arg: 'ins_cpy', type: 'string',  required: true, root: true },
        { arg: 'ins_exp_date', type: 'date', required: false, root: true },
        { arg: 'car_photo', type: 'string',  required: true, root: true },
        { arg: 'ownerNICId_companyName', type: 'string', required: false, root: true },
    ]
    }
);

};

but it work and I have ambiguous whether BookingCar.findCar = function(location, startDate, endDate, carType, callback) this is wright or wrong. because all he reference are only for one argument ..

Please give an idea or hint to solve my issue

1条回答
你好瞎i
2楼-- · 2019-08-16 17:18

Here is an example of remote method based on your code :

'use strict';

module.exports = function(Bookingcar) {

  BookingCar.findCar = function(location, carType) {
    return new Promise((resolve, reject) => {
      const filter = {
        include: ['car'],   // Include the car relation
        where: {
          and: [
            {location: location},
            {carType: carType},
            or: [
              {startDate: {gt: Date.now()}},
              {endDate: {lt: Date.now()}}
            ]
          ]
        }
      };
      Bookincar.find(filter, (err, results) => {
        if (err) return reject(err);
        // you can add more logic on the results here if needed
        resolve(results);
      });
    });
  });

  Bookingcar.remoteMethod(
    'findCar', {
      http: {path: '/findCar', verb: 'get'},
      accepts: [
        {arg: 'location', type: 'string', required: true},
        {arg: 'carType', type: 'string', required: true}
      ],
      returns: {arg: 'cars', type: 'array'}=
    }
);

The filter I've built means : give me all values that matches

  • the exact location AND
  • the exact carType AND
  • (startDate is after now OR endDate is before now)

The startDate and endDate parameters are not necessary because you have to compare the ones included in the Bookingcar instances with now().

Finally, the result will be an array of instances (array of objects), you don't have to detail that in the returned values of the remote method definition.

But, basically, here, you don't need to build a remote method. You can simply query loopback with this filter because there's no specific logic inside the remote method.

查看更多
登录 后发表回答