Compare commits

...

6 Commits

Author SHA1 Message Date
advplyr 672672dd2a JSDoc formatting updates 4 weeks ago
advplyr fd22a6f51d
Merge pull request #2896 from CoffeeKnyte/master 4 weeks ago
CoffeeKnyte 5041f80cb0
Added limit variable to getAuthorsWithCount() 1 month ago
CoffeeKnyte 7229cfce84
Added limit 10 to getAuthorsWithCount() call 1 month ago
CoffeeKnyte 95cdb23efb
split getAuthorsWithCount to 2 lighter functions 1 month ago
CoffeeKnyte 182527bfa8
Update LibraryController.js 1 month ago
  1. 4
      server/controllers/LibraryController.js
  2. 70
      server/utils/queries/authorFilters.js

@ -605,12 +605,12 @@ class LibraryController {
}
if (req.library.isBook) {
const authors = await authorFilters.getAuthorsWithCount(req.library.id)
const authors = await authorFilters.getAuthorsWithCount(req.library.id, 10)
const genres = await libraryItemsBookFilters.getGenresWithCount(req.library.id)
const bookStats = await libraryItemsBookFilters.getBookLibraryStats(req.library.id)
const longestBooks = await libraryItemsBookFilters.getLongestBooks(req.library.id, 10)
stats.totalAuthors = authors.length
stats.totalAuthors = await authorFilters.getAuthorsTotalCount(req.library.id)
stats.authorsWithCount = authors
stats.totalGenres = genres.length
stats.genresWithCount = genres

@ -2,46 +2,62 @@ const Sequelize = require('sequelize')
const Database = require('../../Database')
module.exports = {
/**
* Get authors total count
*
* @param {string} libraryId
* @returns {Promise<number>} count
*/
async getAuthorsTotalCount(libraryId) {
const authorsCount = await Database.authorModel.count({
where: {
libraryId: libraryId
}
})
return authorsCount
},
/**
* Get authors with count of num books
* @param {string} libraryId
* @returns {{id:string, name:string, count:number}}
*
* @param {string} libraryId
* @param {number} limit
* @returns {Promise<{id:string, name:string, count:number}>}
*/
async getAuthorsWithCount(libraryId) {
const authors = await Database.authorModel.findAll({
where: [
async getAuthorsWithCount(libraryId, limit) {
const authors = await Database.bookAuthorModel.findAll({
include: [
{
libraryId
},
Sequelize.where(Sequelize.literal('count'), {
[Sequelize.Op.gt]: 0
})
],
attributes: [
'id',
'name',
[Sequelize.literal('(SELECT count(*) FROM bookAuthors ba WHERE ba.authorId = author.id)'), 'count']
model: Database.authorModel,
as: 'author', // Use the correct alias as defined in your associations
attributes: ['name'],
where: {
libraryId: libraryId
}
}
],
order: [
['count', 'DESC']
]
attributes: ['authorId', [Sequelize.fn('COUNT', Sequelize.col('authorId')), 'count']],
group: ['authorId', 'author.id'], // Include 'author.id' to satisfy GROUP BY with JOIN
order: [[Sequelize.literal('count'), 'DESC']],
limit: limit
})
return authors.map(au => {
return authors.map((au) => {
return {
id: au.id,
name: au.name,
count: au.dataValues.count
id: au.authorId,
name: au.author.name,
count: au.get('count') // Use get method to access aliased attributes
}
})
},
/**
* Search authors
* @param {string} libraryId
* @param {string} query
*
* @param {string} libraryId
* @param {string} query
* @param {number} limit
* @param {number} offset
* @returns {object[]} oldAuthor with numBooks
* @returns {Promise<Object[]>} oldAuthor with numBooks
*/
async search(libraryId, query, limit, offset) {
const authors = await Database.authorModel.findAll({
@ -52,9 +68,7 @@ module.exports = {
libraryId
},
attributes: {
include: [
[Sequelize.literal('(SELECT count(*) FROM bookAuthors ba WHERE ba.authorId = author.id)'), 'numBooks']
]
include: [[Sequelize.literal('(SELECT count(*) FROM bookAuthors ba WHERE ba.authorId = author.id)'), 'numBooks']]
},
limit,
offset

Loading…
Cancel
Save