bugfix for remove tags that not longer used

main
HerrHase 2 years ago
parent e501713c99
commit 8fe9046022

@ -19,13 +19,14 @@ class AppsDatabase extends DatabaseHandler {
'name'
], 'name-sort')
// add index for apps to sort by name
// add index for apps to sort by date_started
this.createIndex([
'date_started'
], 'date-started-sort')
}
/**
* create app
*
* @param {object} data
* @return {object}
@ -33,11 +34,13 @@ class AppsDatabase extends DatabaseHandler {
*/
create(data) {
// default for date_started
data.date_started = 0
return this.db.post(data)
.then((response) => {
// if tags are in data update tags also in database
if (data.tags) {
const tagsDatabase = new TagsDatabase()
tagsDatabase.update(data.tags)
@ -45,11 +48,14 @@ class AppsDatabase extends DatabaseHandler {
return this.findOneById(response._id)
}).catch((error) => {
// @TODO handle errors from pouchdb
console.error(error)
})
}
/**
* update app
*
* @param {object} data
* @return {object}
@ -60,26 +66,34 @@ class AppsDatabase extends DatabaseHandler {
.then((response) => {
return this.findOneById(response._id)
}).catch((error) => {
// @TODO handle errors from pouchdb
console.error(error)
})
}
/**
* remove app
*
* @param {object} data
* @param {object} document
* @return {object}
*
*/
remove(data) {
return this.db.remove(data)
remove(document) {
return this.db.remove(document)
.then(async (response) => {
const tagsDatabase = new TagsDatabase()
await tagsDatabase.removeNotNeeded()
// successfull remove document, check for tags that
// not longer used
if (response.ok === true) {
const tagsDatabase = new TagsDatabase()
await tagsDatabase.removeNotNeeded()
}
return true
return response
}).catch((error) => {
// @TODO handle errors from pouchdb
console.error(error)
})
}
@ -115,6 +129,8 @@ class AppsDatabase extends DatabaseHandler {
return documents.docs[0]
}
}).catch((error) => {
// @TODO handle errors from pouchdb
console.error(error)
})
}
@ -148,13 +164,16 @@ class AppsDatabase extends DatabaseHandler {
query.sort = []
query.sort.push(parameters.sort)
// if sort is date_stared change 'date-started-sort'
if (parameters.sort === 'date_started') {
query.use_index = '_design/date-started-sort'
query.selector = {
'date_started': {
'$regex': ''
'$exists': true
}
}
// if sort is name change 'name-sort'
} else {
query.use_index = '_design/name-sort'
query.selector = {
@ -173,16 +192,10 @@ class AppsDatabase extends DatabaseHandler {
}
return this.db.find(query).then((documents) => {
if (documents.warning) {
console.log(documents.warning)
}
if (documents.docs.length > 0 && parameters.tags.length > 0) {
}
return documents.docs
}).catch((error) => {
// @TODO handle errors from pouchdb
console.error(error)
})
}

@ -1,5 +1,6 @@
import DatabaseHandler from './databaseHandler.js'
import AppsStore from './apps.js'
import tagsStore from './../stores/tags.js'
/**
* apps
@ -41,20 +42,22 @@ class TagsDatabase extends DatabaseHandler {
// check for existings tags and remove
// adding tags that are not removed
this.db.find(query).then((documents) => {
console.log(documents)
return this.filterTags(documents, tags)
}).then(async (tags) => {
console.log(tags)
}).then((tags) => {
if (tags.length > 0) {
// adding tags to entries
for (let i = 0; i < tags.length; i++) {
await this.db.post({
this.db.post({
'name': tags[i]
}).then(() => {
// last tag, then remove
if (i === (tags.length - 1)) {
this.removeNotNeeded()
}
})
}
this.removeNotNeeded()
}
})
}
@ -78,25 +81,27 @@ class TagsDatabase extends DatabaseHandler {
}
this.db.find(query).then((tags) => {
console.log(tags.docs)
if (tags.docs.length > 0) {
const appsStore = new AppsStore()
for (let i = 0; i < tags.docs.length; i++) {
//
// create parameters
const parameters = {
tags: [],
sort: 'name'
}
// add tag name to parameters
parameters.tags.push(tags.docs[i].name)
console.log(parameters)
appsStore.find(parameters).then(async (documents) => {
if (!documents.docs) {
await this.db.remove(tags.docs[i])
// find app by tag, if no app is found, remove tag and update filter
appsStore.find(parameters).then((apps) => {
if (apps.length === 0) {
this.db.remove(tags.docs[i]).then(() => {
tagsStore.get()
})
}
})
@ -153,10 +158,6 @@ class TagsDatabase extends DatabaseHandler {
}
return this.db.find(query).then((documents) => {
if (documents.warning) {
console.log(documents.warning)
}
return documents.docs
})
}

@ -85,7 +85,6 @@ export default observable({
appsDatabase.remove(data).then(() => {
this.trigger('close')
tagsStore.get()
})
},

Loading…
Cancel
Save