Move formatReferenceSources() to BookReferenceController

This commit is contained in:
Robbie Antenesse 2020-08-25 23:49:05 -06:00
parent 86c5a3bc45
commit f5a4db6dc9
2 changed files with 27 additions and 27 deletions

View File

@ -7,6 +7,30 @@ 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,30 +58,6 @@ 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;
@ -116,8 +92,8 @@ class SearchController {
)
);
return [
...bookReferences.map(match => SearchController.formatReferenceSources(match)),
...extraReferences.map(match => SearchController.formatReferenceSources(match)),
...bookReferences.map(match => BookReferenceController.formatReferenceSources(match)),
...extraReferences.map(match => BookReferenceController.formatReferenceSources(match)),
...searchResults,
];
}