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.
This commit is contained in:
Robbie Antenesse 2019-12-23 16:51:26 -07:00
parent cf691f5cff
commit aefab612e7
4 changed files with 15 additions and 15 deletions

View File

@ -27,13 +27,8 @@ export class ShelvesController extends ViewController {
} }
getUserShelves () { getUserShelves () {
return Promise.resolve([ return fetch('/api/shelves/get').then(response => response.json()).then(shelves => {
{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 => {
this.state.myShelves = shelves; this.state.myShelves = shelves;
}); });
// return fetch('/api/shelves/get').then(response => response.json);
} }
} }

View File

@ -21,7 +21,7 @@ export const userShelvesView = (shelvesController, emit) => {
} }
if (shelvesController.state.myShelves.length <= 0) { if (shelvesController.state.myShelves.length <= 0) {
shelvesController.getUserShelves().then(result => { shelvesController.getUserShelves().then(() => {
emit('render'); emit('render');
}); });
} }

View File

@ -93,20 +93,18 @@ class ShelfController {
} }
async getLastUpdatedTimestamp (shelf) { async getLastUpdatedTimestamp (shelf) {
const lastEditedItem = await this.itemModel.findOne({ const lastEditedItem = await shelf.getShelfItems({
attributes: ['updatedAt'], attributes: ['updatedAt'],
where: {
shelf: shelf.id,
},
order: [ order: [
[ [
'updatedAt', 'updatedAt',
'DESC' 'DESC'
], ],
], ],
limit: 1,
}); });
if (lastEditedItem && lastEditedItem.updatedAt > shelf.updatedAt) { if (lastEditedItem.length > 0 && (lastEditedItem[0].updatedAt > shelf.updatedAt)) {
return lastEditedItem.updatedAt; return lastEditedItem.updatedAt;
} }
return shelf.updatedAt; return shelf.updatedAt;

View File

@ -7,7 +7,7 @@ async function routes(fastify, options) {
return false; return false;
}); });
fastify.post('/api/shelves/get', async (request, reply) => { fastify.get('/api/shelves/get', async (request, reply) => {
if (!request.isLoggedInUser) { if (!request.isLoggedInUser) {
return reply.code(400).send({ return reply.code(400).send({
error: true, error: true,
@ -15,8 +15,15 @@ async function routes(fastify, options) {
}); });
} }
return request.user.getShelves({ const shelfController = new ShelfController(fastify.models.Shelf, fastify.models.ShelfItem);
attributes: ['id', 'name', 'isDeletable', 'isPublic'],
const shelves = await request.user.getShelves({
attributes: ['id', 'name', 'isDeletable', 'isPublic', 'updatedAt'],
});
return shelves.map(shelf => {
shelf.updatedAt = shelfController.getLastUpdatedTimestamp(shelf);
return shelf;
}); });
}); });