mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-03 04:02:44 +00:00
from admin to archive api
This commit is contained in:
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