- Update the way to use the data records on Vuex with Getters!

- Fix: mongodb call passing array todos and categiroes already splitted
This commit is contained in:
Paolo Arena
2019-02-27 02:59:02 +01:00
parent d78de1a25c
commit 8f1ee2a7da
5 changed files with 107 additions and 16 deletions

View File

@@ -53,9 +53,6 @@ var TodoSchema = new mongoose.Schema({
id_prev: {
type: String,
},
id_next: {
type: String,
},
progress: {
type: Number,
},
@@ -74,14 +71,68 @@ TodoSchema.methods.toJSON = function () {
};
TodoSchema.statics.findAllByUserId = function (userId) {
TodoSchema.statics.findByUserIdAndCat = function (userId, category) {
var Todo = this;
return Todo.find({
'userId': userId,
'category': category,
});
};
TodoSchema.statics.findAllByUserIdAndCat = function (userId, category = '') {
var Todo = this;
if (category === '') {
return Todo.find({
'userId': userId,
}).then(ris => {
return tools.mapSort(ris)
})
} else {
return Todo.find({
'userId': userId,
'category': category,
});
}
};
TodoSchema.statics.getArrCategoryInTable = function (userId) {
var Todo = this;
return Todo.find({ 'userId': userId }).distinct("category")
.then(arrcategory => {
return arrcategory
})
};
TodoSchema.statics.getAllTodo = async function (userId) {
var Todo = this;
const arralltodo = await Todo.findAllByUserIdAndCat(userId);
// console.log('arralltodo', );
let obj = [];
obj.arrcategories = await Todo.getArrCategoryInTable(userId);
const arrtodos = [];
if (obj.arrcategories.length > 0) {
for (let mycat in obj.arrcategories) {
arrtodos.push(arralltodo.filter(item => item.category === obj.arrcategories[mycat]))
}
}
obj.arrtodos = arrtodos;
// console.log('obj', obj);
return obj;
};
TodoSchema.pre('save', function (next) {
// var todo = this;