使用远程方法检查一个日期是否在两个日期之间
using remote Methode check if one date is between two dates
型号如下:
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 中没有预订。
所以我参考了远程方法并且我使用了 lb remote-methode
。
并基于此 link 我写了一个方法
'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)
这是对还是错。因为他引用的所有内容都只针对一个论点..
请提供解决我问题的想法或提示
这是一个基于您的代码的远程方法示例:
'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'}=
}
);
我构建的过滤器意味着:给我所有匹配的值
- 确切位置 AND
- 准确的车型 AND
- (开始日期晚于现在或结束日期早于现在)
startDate
和 endDate
参数不是必需的,因为您必须将 Bookingcar
实例中包含的参数与 now()
.
进行比较
最后,结果将是一个实例数组(对象数组),您不必在远程方法定义的返回值中详细说明。
但是,基本上,在这里,您不需要构建远程方法。您可以简单地使用此过滤器查询环回,因为远程方法中没有特定的逻辑。
型号如下:
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 中没有预订。
所以我参考了远程方法并且我使用了 lb remote-methode
。
并基于此 link
'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)
这是对还是错。因为他引用的所有内容都只针对一个论点..
请提供解决我问题的想法或提示
这是一个基于您的代码的远程方法示例:
'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'}=
}
);
我构建的过滤器意味着:给我所有匹配的值
- 确切位置 AND
- 准确的车型 AND
- (开始日期晚于现在或结束日期早于现在)
startDate
和 endDate
参数不是必需的,因为您必须将 Bookingcar
实例中包含的参数与 now()
.
最后,结果将是一个实例数组(对象数组),您不必在远程方法定义的返回值中详细说明。
但是,基本上,在这里,您不需要构建远程方法。您可以简单地使用此过滤器查询环回,因为远程方法中没有特定的逻辑。