Giải pháp chuyển đổi số trọn gói cho doanh nghiệp Việt
Tại thư mục : admin/apps/ecommerce/models
Tạo file carcategories.js
Nội dung file
var modelbackendlist = require(`${path_root}/lib/mvc/models/modelbackendlist`);
const factory = require(`${path_root}/lib/Factory.js`);
var model = require(`${path_root}/lib/mvc/models/model`);
const utility = require(`${path_root}/lib/Utilities/Utility.js`);
class carcategories extends modelbackendlist {
constructor() {
super();
}
async getWhereQuery(req, res, next) {
const input = factory.getInput(factory.getReq());
const data = input.getData();
let filter_keyword = input.getString("filter_keyword", "");
let list_filter_carcategories_ids = input.getArray("filter_carcategories", []);
let filter_has_image = input.getInt("filter_has_image", -1);
list_filter_carcategories_ids = list_filter_carcategories_ids.filter(function (el) {
return el != null && el !== "";
});
let aggregate= [];
let match={
};
if(filter_keyword!==""){
match.title={
'$regex': filter_keyword, '$options': 'i'
}
}
aggregate.push(
{ $sort : { modify_date : -1} }
);
if(filter_has_image!==-1){
if(parseInt(filter_has_image)===0)
{
match.total_image=0;
}else{
match.total_image={
$gt:0
};
}
}
aggregate.push({
$match:match
});
return aggregate;
}
getSelectQuery() {
return {
"_id": 1,
"name": 1,
"image": 1,
"note": 1,
"published": 1,
"created_date": 1,
"modify_date": 1
};
}
async getList(req, res, next) {
const db = req.app.db;
let aggregate=await this.getWhereQuery(req, res, next);
aggregate.push({
$project: this.getSelectQuery()
});
aggregate.push({
$limit: this.limit()
});
aggregate.push({
$skip: this.start()
});
const response = await db.carcategories.aggregate(aggregate,{allowDiskUse: true}).toArray();
let list_car=response;
let list_car_id=[];
let list_car2={};
for await (const car of list_car) {
list_car_id.push(car._id.toString());
list_car2[car._id.toString()]=car;
}
const list_image = await db.images
.find({
img_parent_id: {
$in: list_car_id
}
}
)
.toArray();
list_image.forEach(image => {
if(typeof list_car2[image.img_parent_id.toString()].default_photo==="undefined")
list_car2[image.img_parent_id.toString()].default_photo=image;
});
return list_car;
}
tableName() {
return "carcategories";
}
async getData(req, res, next) {
let list_carcategory=await this.getList(req, res, next);
let total=await this.getTotal(req, res, next);
//let total=10;
return {
total:total,
start:this.start(),
limit:this.limit(),
list:list_carcategory
};
}
async getTotal(req, res, next) {
const db = req.app.db;
let aggregate=await this.getWhereQuery(req, res, next);
aggregate.push({ $group: { _id: null, count: { $sum: 1 } } });
const response = await db.carcategories.aggregate(aggregate,{allowDiskUse: true}).toArray();
let total=0;
if(response.length>0){
total=response[0].count;
}
return total;
}
async getCarsByIds(listId = [], req, res, next) {
const db = req.app.db;
const listCars = await db.carcategories
.find({
id: {
$in: listId
}
}
)
.toArray();
return listCars;
}
}
module.exports = carcategories;
Chưa có câu hỏi nào cho bài này.