Update more API endpoints to use new user model

This commit is contained in:
advplyr 2024-08-11 15:15:34 -05:00
parent 9facf77ff1
commit afc16358ca
23 changed files with 856 additions and 404 deletions

View file

@ -1,3 +1,4 @@
const { Request, Response } = require('express')
const Logger = require('../Logger')
const BookFinder = require('../finders/BookFinder')
const PodcastFinder = require('../finders/PodcastFinder')
@ -6,25 +7,51 @@ const MusicFinder = require('../finders/MusicFinder')
const Database = require('../Database')
const { isValidASIN } = require('../utils')
/**
* @typedef RequestUserObjects
* @property {import('../models/User')} userNew
* @property {import('../objects/user/User')} user
*
* @typedef {Request & RequestUserObjects} RequestWithUser
*/
class SearchController {
constructor() {}
/**
* GET: /api/search/books
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async findBooks(req, res) {
const id = req.query.id
const libraryItem = await Database.libraryItemModel.getOldById(id)
const provider = req.query.provider || 'google'
const title = req.query.title || ''
const author = req.query.author || ''
if (typeof provider !== 'string' || typeof title !== 'string' || typeof author !== 'string') {
Logger.error(`[SearchController] findBooks: Invalid request query params`)
return res.status(400).send('Invalid request query params')
}
const results = await BookFinder.search(libraryItem, provider, title, author)
res.json(results)
}
/**
* GET: /api/search/covers
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async findCovers(req, res) {
const query = req.query
const podcast = query.podcast == 1
if (!query.title) {
Logger.error(`[SearchController] findCovers: No title sent in query`)
if (!query.title || typeof query.title !== 'string') {
Logger.error(`[SearchController] findCovers: Invalid title sent in query`)
return res.sendStatus(400)
}
@ -37,10 +64,11 @@ class SearchController {
}
/**
* GET: /api/search/podcasts
* Find podcast RSS feeds given a term
*
* @param {import('express').Request} req
* @param {import('express').Response} res
* @param {RequestWithUser} req
* @param {Response} res
*/
async findPodcasts(req, res) {
const term = req.query.term
@ -56,12 +84,29 @@ class SearchController {
res.json(results)
}
/**
* GET: /api/search/authors
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async findAuthor(req, res) {
const query = req.query.q
if (!query || typeof query !== 'string') {
Logger.error(`[SearchController] findAuthor: Invalid query param`)
return res.status(400).send('Invalid query param')
}
const author = await AuthorFinder.findAuthorByName(query)
res.json(author)
}
/**
* GET: /api/search/chapters
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async findChapters(req, res) {
const asin = req.query.asin
if (!isValidASIN(asin.toUpperCase())) {
@ -74,12 +119,5 @@ class SearchController {
}
res.json(chapterData)
}
async findMusicTrack(req, res) {
const tracks = await MusicFinder.searchTrack(req.query || {})
res.json({
tracks
})
}
}
module.exports = new SearchController()