Compare commits

..

No commits in common. "68621314cdf74253d12d633cd67c07a1df279899" and "86c5a3bc45b6d70a3fc196358e0a60c0c4bdbb94" have entirely different histories.

4 changed files with 65 additions and 119 deletions

View File

@ -7,30 +7,6 @@ class BookReferenceController {
this.lang = language;
}
static formatReferenceSources(reference) {
const referenceSources = Object.keys(reference.sources);
const reformattedSources = referenceSources.map(source => {
const uri = reference.sources[source];
let link;
switch (source) {
default:
case 'inventaire': {
link = `${Inventaire.url}/entity/${uri}`
break;
}
}
return {
source,
uri,
link,
}
});
reference.sources = reformattedSources;
return reference;
}
async createOrUpdateReference(source, sourceId, skipSearchBySourceCodes = false) {
const searchController = new SearchController(this.models);
if (!skipSearchBySourceCodes) {

View File

@ -2,8 +2,8 @@ const fetch = require('node-fetch');
const { Op, fn, col } = require('sequelize');
const BooksController = require('../bookData');
const BookReferenceController = require('../bookReference');
const { quickSearchInventaire } = require('./Inventaire');
const Inventaire = require('../bookData/Inventaire');
const defaultSearchOptions = {
searchBy: 'name', // A column name in the BookReference model, mainly 'name' or 'description'
@ -58,6 +58,30 @@ class SearchController {
};
}
static formatReferenceSources(reference) {
const referenceSources = Object.keys(reference.sources);
const reformattedSources = referenceSources.map(source => {
const uri = reference.sources[source];
let link;
switch (source) {
default:
case 'inventaire': {
link = `${Inventaire.url}/entity/${uri}`
break;
}
}
return {
source,
uri,
link,
}
});
reference.sources = reformattedSources;
return reference;
}
async search(searchTerm, options = defaultSearchOptions) {
const searchBy = options.searchBy.replace('title', 'name').replace('author', 'description');
const { source, language } = options;
@ -92,8 +116,8 @@ class SearchController {
)
);
return [
...bookReferences.map(match => BookReferenceController.formatReferenceSources(match)),
...extraReferences.map(match => BookReferenceController.formatReferenceSources(match)),
...bookReferences.map(match => SearchController.formatReferenceSources(match)),
...extraReferences.map(match => SearchController.formatReferenceSources(match)),
...searchResults,
];
}

View File

@ -1,6 +1,4 @@
const fetch = require('node-fetch');
const { Op, fn, col } = require('sequelize');
const BookReferenceController = require('./bookReference');
class ShelfController {
@ -128,79 +126,9 @@ class ShelfController {
};
}
// REMINDER: Factor in privacy levels
return this.getFakeShelf();
const shelf = await this.models.Shelf.findByPk(shelfId, {
include: [
{
as: 'User',
model: this.models.User,
attributes: ['id', 'username', 'displayName'],
required: true,
},
{
as: 'ShelfItems',
model: this.models.ShelfItem,
attributes: [
'id',
'bookEdition',
'order'
],
required: false,
include: [
{
as: 'Statuses',
model: this.models.Status,
attributes: ['text', 'progress', 'createdAt', 'updatedAt'],
required: false,
},
{
as: 'BookReference',
model: this.models.BookReference,
attributes: ['name', 'description', 'sources', 'covers'],
required: true,
// include: [
// {
// as: 'Interactions',
// model: this.models.Review,
// attributes: ['id'],
// required: false,
// },
// {
// as: 'Reviews',
// model: this.models.Review,
// attributes: ['id'],
// required: false,
// },
// {
// as: 'Ratings',
// model: this.models.Review,
// attributes: ['rating'],
// required: false,
// },
// ],
// group: [
// col('BookReference.id'),
// col('Interactions.id'),
// col('Reviews.id'),
// col('Ratings.id'),
// ],
},
],
orderBy: [[col('ShelfItems.order'), 'ASC']],
},
],
attributes: [
[col('Shelf.id'), 'id'],
'name',
'userId',
'permissionLevel',
'isDeletable',
'createdAt',
'updatedAt',
[fn('COUNT', col('ShelfItems.id')), 'numShelfItems'],
],
});
const shelf = await this.shelfModel.findByPk(shelfId);
if (shelf === null) {
return {
error: `Shelf with ID ${shelfId} not found.`,
@ -208,8 +136,6 @@ class ShelfController {
}
shelf.updatedAt = this.getLastUpdatedTimestamp(shelf);
shelf.isPublic = shelf.permissionLevel === 0;
return shelf;
}
@ -272,7 +198,7 @@ class ShelfController {
async scrubShelfData (shelf, currentUser) {
const userOwnsShelf = currentUser.id === shelf.userId;
const shelfUser = userOwnsShelf ? null : shelf.User;
const shelfUser = userOwnsShelf ? null : await shelf.getUser({ attributes: ['username, displayName'] });
let user = {};
if (shelfUser !== null) {
user.name = shelfUser.displayName;
@ -280,25 +206,40 @@ class ShelfController {
} else {
user = null;
}
// Untested
const shelfItems = await Promise.all(shelf.ShelfItems.map(async (shelfItem) => {
const bookReference = BookReferenceController.formatReferenceSources(shelfItem.BookReference);
const reviews = await bookReference.getInteractions({
const rawShelfItems = await shelf.getShelfItems({
attributes: ['bookId', 'createdAt', 'updatedAt'],
order: [['order', 'ASC']],
})
const bookReferenceMap = await Promise.all(shelfItems.map(shelfItem => {
return shelfItem.getBookReference({
attributes: ['name', 'description', 'sources', 'covers'],
});
}));
const bookReferenceStatuses = await Promise.all(bookReferenceMap.map(bookReference => {
return bookReference.getStatuses({
where: {
userId: shelf.userId,
}, // Return all reviews for any bookEdition so they can be filtered on frontend.
attributes: ['text', 'rating', 'bookEdition'],
},
order: [['updatedAt', 'DESC']],
});
return {
title: bookReference.name,
author: bookReference.description,
sources: bookReference.sources,
covers: bookReference.covers,
updates: shelfItem.Statuses,
reviews,
};
}));
const bookReferences = bookReferenceMap.map(bookReference => {
bookReference.updates = bookReferenceStatuses.findAll(status => status.type === 1);
bookReference.rating = bookReferenceStatuses.find(status => status.type === 3);
bookReference.review = bookReferenceStatuses.find(status => status.type === 4);
return bookReference;
});
const shelfItems = rawShelfItems.map(shelfItem => {
const bookReference = bookReferences.find(bookData => bookData.id === shelfItem.bookId);
shelfItem.title = bookReference.name;
shelfItem.author = bookReference.description;
shelfItem.sources = bookReference.sources;
shelfItem.covers = bookReference.covers;
shelfItem.updates = bookReference.updates;
shelfItem.rating = bookReference.rating.data;
shelfItem.review = bookReference.review.data;
return shelfItem;
})
const shelfData = {
name: shelf.name,

View File

@ -16,6 +16,11 @@ module.exports = models => {
onDelete: 'SET NULL',
});
Shelf.belongsTo(User, {
foreignKey: 'permissionLevel',
onDelete: 'CASCADE',
});
Shelf.hasMany(ShelfItem, {
foreignKey: 'shelfId',
});