2016-05-05 00:34:54 +08:00
|
|
|
import {AutoCompleteItem, AutoCompeleteTypes} from "../../../common/entities/AutoCompleteItem";
|
|
|
|
import {ISearchManager} from "../ISearchManager";
|
|
|
|
import {DirectoryModel} from "./entities/DirectoryModel";
|
|
|
|
import {PhotoModel} from "./entities/PhotoModel";
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
export class MongoSearchManager implements ISearchManager {
|
2016-05-05 00:34:54 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
constructor() {
|
2016-05-05 00:34:54 +08:00
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
autocomplete(text, cb:(error:any, result:Array<AutoCompleteItem>) => void) {
|
2016-05-05 00:34:54 +08:00
|
|
|
|
|
|
|
console.log("autocomplete: " + text);
|
|
|
|
let items:Array<AutoCompleteItem> = [];
|
2016-05-09 23:04:56 +08:00
|
|
|
PhotoModel.find({name: {$regex: text, $options: "i"}}).limit(10).select('name').exec((err, res:Array<any>) => {
|
|
|
|
if (err || !res) {
|
|
|
|
return cb(err, null);
|
2016-05-05 00:34:54 +08:00
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
items = items.concat(this.encapsulateAutoComplete(res.map(r => r.name), AutoCompeleteTypes.image));
|
2016-05-05 00:34:54 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
DirectoryModel.find({
|
|
|
|
name: {
|
|
|
|
$regex: text,
|
|
|
|
$options: "i"
|
2016-05-05 00:34:54 +08:00
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
}).limit(10).select('name').exec((err, res:Array<any>) => {
|
|
|
|
if (err || !res) {
|
|
|
|
return cb(err, null);
|
|
|
|
}
|
|
|
|
items = items.concat(this.encapsulateAutoComplete(res.map(r => r.name), AutoCompeleteTypes.directory));
|
|
|
|
return cb(null, items);
|
2016-05-05 00:34:54 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
private encapsulateAutoComplete(values:Array<string>, type:AutoCompeleteTypes) {
|
2016-05-05 00:34:54 +08:00
|
|
|
let res = [];
|
2016-05-09 23:04:56 +08:00
|
|
|
values.forEach((value)=> {
|
|
|
|
res.push(new AutoCompleteItem(value, type));
|
2016-05-05 00:34:54 +08:00
|
|
|
});
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|