From aefab612e7190197dbd8be2dc7c6f6d84a1d25d2 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Mon, 23 Dec 2019 16:51:26 -0700 Subject: [PATCH] Add get user shelves from db: Add GET /api/shelves/get endpoint; Turn off test data and pull shelves from database; Update getLastUpdatedTimestamp to use association function. --- app/views/shelves/controller.js | 7 +------ app/views/shelves/userShelves/index.js | 2 +- server/controllers/shelf.js | 8 +++----- server/routes/shelf.js | 13 ++++++++++--- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/app/views/shelves/controller.js b/app/views/shelves/controller.js index 45808e5..2383cea 100644 --- a/app/views/shelves/controller.js +++ b/app/views/shelves/controller.js @@ -27,13 +27,8 @@ export class ShelvesController extends ViewController { } getUserShelves () { - return Promise.resolve([ - {id:1,name:'Test Shelf',isDeletable:false,isPublilc:false}, - {id:2,name:'Deletable Shelf',isDeletable:true,isPublilc:false}, - {id:3,name:'Public Shelf',isDeletable:true,isPublilc:true}, - ]).then(shelves => { + return fetch('/api/shelves/get').then(response => response.json()).then(shelves => { this.state.myShelves = shelves; }); - // return fetch('/api/shelves/get').then(response => response.json); } } \ No newline at end of file diff --git a/app/views/shelves/userShelves/index.js b/app/views/shelves/userShelves/index.js index c75168f..e32c3a5 100644 --- a/app/views/shelves/userShelves/index.js +++ b/app/views/shelves/userShelves/index.js @@ -21,7 +21,7 @@ export const userShelvesView = (shelvesController, emit) => { } if (shelvesController.state.myShelves.length <= 0) { - shelvesController.getUserShelves().then(result => { + shelvesController.getUserShelves().then(() => { emit('render'); }); } diff --git a/server/controllers/shelf.js b/server/controllers/shelf.js index 996473e..0475ff8 100644 --- a/server/controllers/shelf.js +++ b/server/controllers/shelf.js @@ -93,20 +93,18 @@ class ShelfController { } async getLastUpdatedTimestamp (shelf) { - const lastEditedItem = await this.itemModel.findOne({ + const lastEditedItem = await shelf.getShelfItems({ attributes: ['updatedAt'], - where: { - shelf: shelf.id, - }, order: [ [ 'updatedAt', 'DESC' ], ], + limit: 1, }); - if (lastEditedItem && lastEditedItem.updatedAt > shelf.updatedAt) { + if (lastEditedItem.length > 0 && (lastEditedItem[0].updatedAt > shelf.updatedAt)) { return lastEditedItem.updatedAt; } return shelf.updatedAt; diff --git a/server/routes/shelf.js b/server/routes/shelf.js index 6bce576..7dcfaa9 100644 --- a/server/routes/shelf.js +++ b/server/routes/shelf.js @@ -7,7 +7,7 @@ async function routes(fastify, options) { return false; }); - fastify.post('/api/shelves/get', async (request, reply) => { + fastify.get('/api/shelves/get', async (request, reply) => { if (!request.isLoggedInUser) { return reply.code(400).send({ error: true, @@ -15,8 +15,15 @@ async function routes(fastify, options) { }); } - return request.user.getShelves({ - attributes: ['id', 'name', 'isDeletable', 'isPublic'], + const shelfController = new ShelfController(fastify.models.Shelf, fastify.models.ShelfItem); + + const shelves = await request.user.getShelves({ + attributes: ['id', 'name', 'isDeletable', 'isPublic', 'updatedAt'], + }); + + return shelves.map(shelf => { + shelf.updatedAt = shelfController.getLastUpdatedTimestamp(shelf); + return shelf; }); });