mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-03 19:12:41 +00:00
from admin to archive api
This commit is contained in:
@@ -76,28 +76,6 @@ router.get('/admin/lookup/:id', mw.adminOnly, async (req, res, next) => {
|
||||
return res.status(500).json({ error: 'Internal Server Error' });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
/* Searches for matching title, also attempts to partial match */
|
||||
router.get('/admin/archive/:title', mw.adminOnly, async (req, res, next) => {
|
||||
try {
|
||||
const brews = await HomebrewModel.find({
|
||||
title: { $regex: req.params.title, $options: 'i' },
|
||||
published: true
|
||||
}).exec();
|
||||
|
||||
if (!brews || brews.length === 0) {
|
||||
// No published documents found with the given title
|
||||
return res.status(404).json({ error: 'Published documents not found' });
|
||||
}
|
||||
|
||||
return res.json(brews);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return res.status(500).json({ error: 'Internal Server Error' });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
/* Find 50 brews that aren't compressed yet */
|
||||
router.get('/admin/finduncompressed', mw.adminOnly, (req, res)=>{
|
||||
|
||||
@@ -67,6 +67,7 @@ app.use((req, res, next)=>{
|
||||
|
||||
app.use(homebrewApi);
|
||||
app.use(require('./admin.api.js'));
|
||||
app.use(require('./archive.api.js'));
|
||||
|
||||
const HomebrewModel = require('./homebrew.model.js').model;
|
||||
const welcomeText = require('fs').readFileSync('client/homebrew/pages/homePage/welcome_msg.md', 'utf8');
|
||||
@@ -480,6 +481,11 @@ app.use(async (err, req, res, next)=>{
|
||||
res.status(err.status || err.response?.status || 500).send(err);
|
||||
return;
|
||||
}
|
||||
if(err.originalUrl?.startsWith('/archive/')) {
|
||||
// console.log('archive error');
|
||||
res.status(err.status || err.response?.status || 500).send(err);
|
||||
return;
|
||||
}
|
||||
|
||||
// console.log('non-API error');
|
||||
const status = err.status || err.code || 500;
|
||||
@@ -503,6 +509,8 @@ app.use(async (err, req, res, next)=>{
|
||||
res.send(page);
|
||||
});
|
||||
|
||||
|
||||
|
||||
app.use((req, res)=>{
|
||||
if(!res.headersSent) {
|
||||
console.error('Headers have not been sent, responding with a server error.', req.url);
|
||||
|
||||
31
server/archive.api.js
Normal file
31
server/archive.api.js
Normal file
@@ -0,0 +1,31 @@
|
||||
const HomebrewModel = require('./homebrew.model.js').model;
|
||||
const router = require('express').Router();
|
||||
const asyncHandler = require('express-async-handler');
|
||||
|
||||
|
||||
const archive = {
|
||||
archiveApi : router,
|
||||
/* Searches for matching title, also attempts to partial match */
|
||||
findBrews : async (req, res, next) => {
|
||||
try {
|
||||
const brews = await HomebrewModel.find({
|
||||
title: { $regex: req.params.query, $options: 'i' },
|
||||
published: true
|
||||
}).exec();
|
||||
|
||||
if (!brews || brews.length === 0) {
|
||||
// No published documents found with the given title
|
||||
return res.status(404).json({ error: 'Published documents not found' });
|
||||
}
|
||||
|
||||
return res.json(brews);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return res.status(500).json({ error: 'Internal Server Error' });
|
||||
}
|
||||
}
|
||||
}
|
||||
router.get('/archive/:query', asyncHandler(archive.findBrews));
|
||||
|
||||
|
||||
module.exports = archive;
|
||||
Reference in New Issue
Block a user