- 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: { id_prev: {
type: String, type: String,
}, },
id_next: {
type: String,
},
progress: { progress: {
type: Number, type: Number,
}, },
@@ -74,14 +71,68 @@ TodoSchema.methods.toJSON = function () {
}; };
TodoSchema.statics.findAllByUserId = function (userId) { TodoSchema.statics.findByUserIdAndCat = function (userId, category) {
var Todo = this; var Todo = this;
return Todo.find({ return Todo.find({
'userId': userId, '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) { TodoSchema.pre('save', function (next) {
// var todo = this; // var todo = this;

View File

@@ -105,6 +105,7 @@ router.patch('/:id', authenticate, (req, res) => {
router.get('/:userId', authenticate, (req, res) => { router.get('/:userId', authenticate, (req, res) => {
var userId = req.params.userId; var userId = req.params.userId;
// var category = req.params.category;
tools.mylog('GET : ', req.params); tools.mylog('GET : ', req.params);
@@ -118,11 +119,13 @@ router.get('/:userId', authenticate, (req, res) => {
} }
// Extract all the todos of the userId only // Extract all the todos of the userId only
Todo.findAllByUserId(userId).then((todos) => { // Todo.findAllByUserIdAndCat(userId, category).then((todos) => {
Todo.getAllTodo(userId).then((objtodos) => {
tools.mylog('todos', todos.length); tools.mylog('todos', objtodos.arrtodos.length);
tools.mylog('categories', objtodos.arrcategories.length);
res.send({ todos: todos }); res.send({ todos: objtodos.arrtodos, categories: objtodos.arrcategories });
}).catch((e) => { }).catch((e) => {
console.log(e); console.log(e);
res.status(400).send(e); res.status(400).send(e);

View File

@@ -81,7 +81,6 @@ const todos = [{
descr: "Primo Task Esempio", descr: "Primo Task Esempio",
enableExpiring: false, enableExpiring: false,
expiring_at: new Date(), expiring_at: new Date(),
id_next: "10000000",
id_prev: "0", id_prev: "0",
modified: false, modified: false,
modify_at: new Date(), modify_at: new Date(),
@@ -98,7 +97,6 @@ const todos = [{
descr: "Secondo Task Esempio", descr: "Secondo Task Esempio",
enableExpiring: false, enableExpiring: false,
expiring_at: new Date(), expiring_at: new Date(),
id_next: "10000000",
id_prev: "1", id_prev: "1",
modified: false, modified: false,
modify_at: new Date(), modify_at: new Date(),
@@ -115,7 +113,6 @@ const todos = [{
descr: "Terzo Task Esempio", descr: "Terzo Task Esempio",
enableExpiring: false, enableExpiring: false,
expiring_at: new Date(), expiring_at: new Date(),
id_next: "10000000",
id_prev: "1", id_prev: "1",
modified: false, modified: false,
modify_at: new Date(), modify_at: new Date(),
@@ -132,7 +129,6 @@ const todos = [{
descr: "Nuovo Quarto Task Esempio da Inserire", descr: "Nuovo Quarto Task Esempio da Inserire",
enableExpiring: false, enableExpiring: false,
expiring_at: new Date(), expiring_at: new Date(),
id_next: "10000000",
id_prev: "2", id_prev: "2",
modified: false, modified: false,
modify_at: new Date(), modify_at: new Date(),

View File

@@ -7,6 +7,8 @@ var Url = require('url-parse');
const mongoose = require('mongoose'); const mongoose = require('mongoose');
const Subscription = mongoose.model('subscribers'); const Subscription = mongoose.model('subscribers');
const server_constants = require('../tools/server_constants');
// SETTINGS WebPush Configuration // SETTINGS WebPush Configuration
const webpush = require('web-push'); const webpush = require('web-push');
@@ -48,7 +50,7 @@ module.exports = {
allfieldTodo: function () { allfieldTodo: function () {
return ['userId', 'pos', 'category', 'descr', 'priority', 'completed', 'created_at', 'modify_at', return ['userId', 'pos', 'category', 'descr', 'priority', 'completed', 'created_at', 'modify_at',
'completed_at', 'expiring_at', 'enableExpiring', 'id_prev', 'id_next', 'progress', 'modified'] 'completed_at', 'expiring_at', 'enableExpiring', 'id_prev', 'progress', 'modified']
}, },
allfieldTodoWithId: function () { allfieldTodoWithId: function () {
@@ -172,8 +174,43 @@ module.exports = {
} }
}); });
},
// **********************
// SORT WITH PREV_ID
// **********************
mapSort: function (linkedList) {
var sortedList = [];
var map = new Map();
var currentId = null;
// console.log('linkedList', linkedList);
// index the linked list by previous_item_id
for (var i = 0; i < linkedList.length; i++) {
var item = linkedList[i];
if (item.id_prev === server_constants.LIST_START) {
// first item
currentId = String(item._id);
// console.log('currentId', currentId);
sortedList.push(item);
} else {
map.set(item.id_prev, i);
}
}
while (sortedList.length < linkedList.length) {
// get the item with a previous item ID referencing the current item
var nextItem = linkedList[map.get(currentId)];
if (nextItem === undefined)
break;
sortedList.push(nextItem);
currentId = String(nextItem._id);
}
// console.log('DOPO sortedList', sortedList);
return sortedList;
} }
// Test
}; };

View File

@@ -13,4 +13,8 @@ module.exports = Object.freeze({
RIS_CODE_HTTP_INVALID_TOKEN: 403, RIS_CODE_HTTP_INVALID_TOKEN: 403,
LIST_END: '10000000',
LIST_START: '0',
}); });