You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
169 lines
3.7 KiB
169 lines
3.7 KiB
import DatabaseHandler from './databaseHandler.js'
|
|
import TagsDatabase from './tags.js'
|
|
|
|
/**
|
|
* apps
|
|
*
|
|
* @author Björn Hase
|
|
* @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3
|
|
* @link https://gitea.node001.net/HerrHase/tellme-bot.git
|
|
*
|
|
*/
|
|
class AppsDatabase extends DatabaseHandler {
|
|
|
|
constructor() {
|
|
super('apps')
|
|
|
|
// add index for apps to sort by name
|
|
this.createIndex([
|
|
'name'
|
|
], 'name-sort')
|
|
|
|
// add index for apps to sort by name
|
|
this.createIndex([
|
|
'date_started'
|
|
], 'date-started-sort')
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {object} data
|
|
* @return {object}
|
|
*
|
|
*/
|
|
create(data) {
|
|
|
|
data.date_started = 0
|
|
|
|
return this.db.post(data)
|
|
.then((response) => {
|
|
|
|
if (data.tags) {
|
|
const tagsDatabase = new TagsDatabase()
|
|
tagsDatabase.update(data.tags)
|
|
}
|
|
|
|
return this.findOneById(response._id)
|
|
}).catch((error) => {
|
|
console.error(error)
|
|
})
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {object} data
|
|
* @return {object}
|
|
*
|
|
*/
|
|
update(data) {
|
|
return this.db.put(data)
|
|
.then((response) => {
|
|
return this.findOneById(response._id)
|
|
}).catch((error) => {
|
|
console.error(error)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* find one app by id
|
|
*
|
|
* @param {string} id
|
|
* @return {mixed}
|
|
*
|
|
*/
|
|
findOneById(id) {
|
|
const query = {
|
|
'fields': [
|
|
'_id',
|
|
'_rev',
|
|
'name',
|
|
'command',
|
|
'description',
|
|
'thumbnail',
|
|
'tags',
|
|
'date_started'
|
|
],
|
|
'selector': {
|
|
'_id' : id
|
|
}
|
|
}
|
|
|
|
return this.db.find(query).then((documents) => {
|
|
if (documents.docs.length === 0) {
|
|
return null
|
|
} else {
|
|
return documents.docs[0]
|
|
}
|
|
}).catch((error) => {
|
|
console.error(error)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* find apps
|
|
*
|
|
* @return {mixed}
|
|
*
|
|
*/
|
|
find(parameters) {
|
|
|
|
const query = {
|
|
'fields': [
|
|
'_id',
|
|
'_rev',
|
|
'name',
|
|
'command',
|
|
'description',
|
|
'thumbnail',
|
|
'tags',
|
|
'date_started'
|
|
]
|
|
}
|
|
|
|
// adding sort from parameters
|
|
if (parameters.sort) {
|
|
query.sort = []
|
|
query.sort.push(parameters.sort)
|
|
|
|
if (parameters.sort === 'date_started') {
|
|
query.use_index = '_design/date-started-sort'
|
|
query.selector = {
|
|
'date_started': {
|
|
'$regex': ''
|
|
}
|
|
}
|
|
} else {
|
|
query.use_index = '_design/name-sort'
|
|
query.selector = {
|
|
'name': {
|
|
'$regex': ''
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// adding tags from parameters
|
|
if (parameters.tags.length > 0) {
|
|
query.selector.tags = {
|
|
$in: parameters.tags
|
|
}
|
|
}
|
|
|
|
return this.db.find(query).then((documents) => {
|
|
if (documents.warning) {
|
|
console.warning(documents.warning)
|
|
}
|
|
|
|
if (documents.docs.length > 0 && parameters.tags.length > 0) {
|
|
|
|
}
|
|
|
|
return documents.docs
|
|
}).catch((error) => {
|
|
console.error(error)
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
export default AppsDatabase |