使用远程Methode检查一个日期是否在两个日期之间 [英] using remote Methode check if one date is between two dates

查看:90
本文介绍了使用远程Methode检查一个日期是否在两个日期之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模型如下:

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

关系:

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

现在,我要基于car模型中的数据在选定的日期段和位置(carType)中未进行预订来进行过滤.

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

所以我指的是远程方法,所以我使用了lb remote-methode. 并基于此链接

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

};

但是它有效,我不确定是否 BookingCar.findCar = function(location, startDate, endDate, carType, callback) 这是对还是错.因为他所引用的只是一个论点..

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

推荐答案

以下是基于您的代码的远程方法的示例:

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

  • 确切位置 AND
  • 确切的汽车类型 AND
  • (startDate在此之后 OR endDate在此之前)
  • the exact location AND
  • the exact carType AND
  • (startDate is after now OR endDate is before now)

不需要startDateendDate参数,因为您必须将Bookingcar实例中包含的参数与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.

这篇关于使用远程Methode检查一个日期是否在两个日期之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆