Readlebee/server/routes/shelf.js

154 lines
4.7 KiB
JavaScript
Raw Normal View History

2019-12-01 23:31:34 +01:00
const fs = require('fs');
const path = require('path');
const ShelfController = require('../controllers/shelf');
async function routes(fastify, options) {
fastify.get('/api/shelf/test', async (request, reply) => {
return false;
});
fastify.get('/api/shelves/get', async (request, reply) => {
if (!request.isLoggedInUser) {
return reply.code(400).send({
error: true,
message: 'api.not_logged_in',
});
}
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;
});
});
fastify.get('/api/shelf/get/:shelfId/:domain', async (request, reply) => {
if (typeof request.params.shelfId === 'undefined') {
2019-12-01 23:31:34 +01:00
return reply.code(400).send({
error: true,
2020-01-05 03:34:26 +01:00
message: 'api.shelf.get.missing_id',
2019-12-01 23:31:34 +01:00
});
}
if (isNaN(parseInt(request.params.shelfId))) {
return reply.code(400).send({
error: true,
message: 'api.shelf.get.invalid_id',
});
}
if (request.params.domain.trim() !== '') {
return ShelfController.CheckExternalDomainForShelf(request.params.domain.trim(), request.params.shelfId);
}
2019-12-01 23:31:34 +01:00
2020-01-05 03:34:26 +01:00
const shelfController = new ShelfController(fastify.models.Shelf, fastify.models.ShelfItem);
const shelf = await shelfController.getShelfById(request.params.shelfId);
2020-01-05 03:34:26 +01:00
if (typeof shelf.error !== 'undefined') {
shelf.message = 'api.shelf.get.nonexistent_shelf';
return reply.code(400).send(shelf);
}
const userCanViewShelf = await shelfController.userCanViewShelf(request.user, shelf);
console.log('can view?', userCanViewShelf);
2020-01-05 03:34:26 +01:00
if (userCanViewShelf !== true) {
return reply.code(400).send({
error: true,
message: 'api.shelf.get.access_denied', // Should potentially be nonexistent shelf message instead?
});
}
// const shelfData = await shelfController.scrubShelfData(shelf, request.user);
// return reply.send(shelfData);
2020-01-05 03:34:26 +01:00
return reply.send(shelf);
});
fastify.post('/api/shelf/create', async (request, reply) => {
if (!request.isLoggedInUser) {
2019-12-01 23:31:34 +01:00
return reply.code(400).send({
error: true,
2020-01-05 03:34:26 +01:00
message: 'api.not_logged_in',
2019-12-01 23:31:34 +01:00
});
}
2020-01-05 03:34:26 +01:00
2019-12-01 23:31:34 +01:00
request.body.shelfName = request.body.shelfName.trim();
const userShelves = await request.user.getShelves({
attributes: ['name'],
});
const shelfNameIsValid = ShelfController.newShelfNameIsValid(
request.body.shelfName,
userShelves.map(shelf => shelf.name)
);
if (shelfNameIsValid !== true) {
return reply.code(400).send(shelfNameIsValid);
}
2020-01-05 03:34:26 +01:00
const shelfController = new ShelfController(fastify.models.Shelf, fastify.models.ShelfItem);
2019-12-01 23:31:34 +01:00
2020-01-05 03:34:26 +01:00
const newShelf = shelfController.createShelf(request.user, request.body.shelfName);
2019-12-01 23:31:34 +01:00
if (typeof newShelf.error !== 'undefined' && newShelf.error !== false) {
newShelf.message = 'api.shelf.create.fail';
return reply.code(400).send(newShelf);
}
return reply.send({
error: false,
message: 'api.shelf.create.success',
});
});
2019-12-11 21:15:55 +01:00
fastify.post('/api/shelf/rename', async (request, reply) => {
if (!request.isLoggedInUser) {
return reply.code(400).send({
error: true,
message: 'api.not_logged_in',
});
}
if (typeof request.body.shelfId === 'undefined') {
return reply.code(400).send({
error: true,
message: 'api.shelf.rename.missing_id',
});
}
if (typeof request.body.shelfName === 'undefined') {
return reply.code(400).send({
error: true,
message: 'api.shelf.rename.missing_name',
});
}
request.body.shelfName = request.body.shelfName.trim();
const userShelves = await request.user.getShelves({
attributes: ['name'],
});
const shelfNameIsValid = ShelfController.newShelfNameIsValid(
request.body.shelfName,
userShelves.map(shelf => shelf.name)
);
if (shelfNameIsValid !== true) {
return reply.code(400).send(shelfNameIsValid);
}
2020-01-05 03:34:26 +01:00
const shelfController = new ShelfController(fastify.models.Shelf, fastify.models.ShelfItem);
2019-12-11 21:15:55 +01:00
2020-01-05 03:34:26 +01:00
const newShelf = shelfController.renameShelf(request.user, request.body.shelfId, request.body.shelfName);
2019-12-11 21:15:55 +01:00
if (typeof newShelf.error !== 'undefined' && newShelf.error !== false) {
newShelf.message = 'api.shelf.rename.fail';
return reply.code(400).send(newShelf);
}
return reply.send({
error: false,
message: 'api.shelf.rename.success',
});
});
2019-12-01 23:31:34 +01:00
}
module.exports = routes;