mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-12 06:42:40 +00:00
search by author
This commit is contained in:
@@ -18,6 +18,9 @@ const request = require('../../utils/request-middleware.js');
|
|||||||
|
|
||||||
const VaultPage = (props) => {
|
const VaultPage = (props) => {
|
||||||
const [title, setTitle] = useState(props.query.title || '');
|
const [title, setTitle] = useState(props.query.title || '');
|
||||||
|
//state author and owner
|
||||||
|
const [author, setAuthor] = useState(props.query.author || '');
|
||||||
|
const [owner, setOwner] = useState(props.query.owner !== 'false');
|
||||||
const [legacy, setLegacy] = useState(props.query.legacy !== 'false');
|
const [legacy, setLegacy] = useState(props.query.legacy !== 'false');
|
||||||
const [v3, setV3] = useState(props.query.v3 !== 'false');
|
const [v3, setV3] = useState(props.query.v3 !== 'false');
|
||||||
const [count, setCount] = useState(props.query.count || 20);
|
const [count, setCount] = useState(props.query.count || 20);
|
||||||
@@ -28,6 +31,8 @@ const VaultPage = (props) => {
|
|||||||
const [error, setError] = useState(null);
|
const [error, setError] = useState(null);
|
||||||
|
|
||||||
const titleRef = useRef(null);
|
const titleRef = useRef(null);
|
||||||
|
const authorRef = useRef(null);
|
||||||
|
const ownerRef = useRef(null);
|
||||||
const countRef = useRef(null);
|
const countRef = useRef(null);
|
||||||
const v3Ref = useRef(null);
|
const v3Ref = useRef(null);
|
||||||
const legacyRef = useRef(null);
|
const legacyRef = useRef(null);
|
||||||
@@ -46,13 +51,19 @@ const VaultPage = (props) => {
|
|||||||
setSearching(false);
|
setSearching(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateUrl = (title, page, count, v3, legacy) => {
|
const updateUrl = (title, author, owner, count, v3, legacy, page) => {
|
||||||
const url = new URL(window.location.href);
|
const url = new URL(window.location.href);
|
||||||
const urlParams = new URLSearchParams();
|
const urlParams = new URLSearchParams();
|
||||||
|
|
||||||
Object.entries({ title, v3, legacy, count, page }).forEach(
|
Object.entries({
|
||||||
([key, value]) => urlParams.set(key, value)
|
title,
|
||||||
);
|
author,
|
||||||
|
owner,
|
||||||
|
count,
|
||||||
|
v3,
|
||||||
|
legacy,
|
||||||
|
page,
|
||||||
|
}).forEach(([key, value]) => urlParams.set(key, value));
|
||||||
|
|
||||||
url.search = urlParams.toString();
|
url.search = urlParams.toString();
|
||||||
window.history.replaceState(null, null, url);
|
window.history.replaceState(null, null, url);
|
||||||
@@ -62,14 +73,22 @@ const VaultPage = (props) => {
|
|||||||
setSearching(true);
|
setSearching(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
|
|
||||||
const performSearch = async ({ title, count, v3, legacy }) => {
|
const performSearch = async ({
|
||||||
updateUrl(title, page, count, v3, legacy);
|
title,
|
||||||
if (title && (v3 || legacy)) {
|
author,
|
||||||
|
owner,
|
||||||
|
count,
|
||||||
|
v3,
|
||||||
|
legacy,
|
||||||
|
}) => {
|
||||||
|
updateUrl(title, author, owner, count, v3, legacy, page);
|
||||||
|
if ((title || author) && (v3 || legacy)) {
|
||||||
try {
|
try {
|
||||||
const response = await request.get(
|
const response = await request.get(
|
||||||
`/api/vault?title=${title}&v3=${v3}&legacy=${legacy}&count=${count}&page=${page}`
|
`/api/vault?title=${title}&author=${author}&owner=${owner}&v3=${v3}&legacy=${legacy}&count=${count}&page=${page}`
|
||||||
);
|
);
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
|
console.log(response.body.brews);
|
||||||
updateStateWithBrews(response.body.brews, page);
|
updateStateWithBrews(response.body.brews, page);
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Error: ${response.status}`);
|
throw new Error(`Error: ${response.status}`);
|
||||||
@@ -93,10 +112,10 @@ const VaultPage = (props) => {
|
|||||||
const loadTotal = async ({ title, v3, legacy }) => {
|
const loadTotal = async ({ title, v3, legacy }) => {
|
||||||
setTotalBrews(null);
|
setTotalBrews(null);
|
||||||
setError(null);
|
setError(null);
|
||||||
if (title) {
|
if ((title || author) && (v3 || legacy)) {
|
||||||
try {
|
try {
|
||||||
const response = await request.get(
|
const response = await request.get(
|
||||||
`/api/vault/total?title=${title}&v3=${v3}&legacy=${legacy}`
|
`/api/vault/total?title=${title}&author=${author}&owner=${owner}&v3=${v3}&legacy=${legacy}`
|
||||||
);
|
);
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
@@ -115,23 +134,26 @@ const VaultPage = (props) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const title = titleRef.current.value || '';
|
const title = titleRef.current.value || '';
|
||||||
|
const author = authorRef.current.value || '';
|
||||||
|
const owner = ownerRef.current.checked != false;
|
||||||
const count = countRef.current.value || 10;
|
const count = countRef.current.value || 10;
|
||||||
const v3 = v3Ref.current.checked != false;
|
const v3 = v3Ref.current.checked != false;
|
||||||
const legacy = legacyRef.current.checked != false;
|
const legacy = legacyRef.current.checked != false;
|
||||||
|
|
||||||
|
console.log(author);
|
||||||
if (update) {
|
if (update) {
|
||||||
setTitle(title);
|
setTitle(title);
|
||||||
setCount(count);
|
setCount(count);
|
||||||
setV3(v3);
|
setV3(v3);
|
||||||
setLegacy(legacy);
|
setLegacy(legacy);
|
||||||
|
|
||||||
performSearch({ title, count, v3, legacy });
|
performSearch({ title, author, owner, count, v3, legacy });
|
||||||
} else {
|
} else {
|
||||||
performSearch({ title, count, v3, legacy });
|
performSearch({ title, author, owner, count, v3, legacy });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (total) {
|
if (total) {
|
||||||
loadTotal({ title, v3, legacy });
|
loadTotal({ title, author, owner, v3, legacy });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -152,15 +174,19 @@ const VaultPage = (props) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const validateForm = () => {
|
const validateForm = () => {
|
||||||
const submitButton = searchButtonRef.current;
|
//form validity: title or author must be written, and at least one renderer set
|
||||||
const textInput = titleRef.current;
|
const { current: submitButton } = searchButtonRef;
|
||||||
const legacyCheckbox = legacyRef.current;
|
const { current: titleInput } = titleRef;
|
||||||
const v3Checkbox = v3Ref.current;
|
const { current: legacyCheckbox } = legacyRef;
|
||||||
|
const { current: v3Checkbox } = v3Ref;
|
||||||
|
const { current: authorInput } = authorRef;
|
||||||
|
|
||||||
const isTextValid = textInput.validity.valid && textInput.value;
|
const isTitleValid = titleInput.validity.valid && titleInput.value;
|
||||||
|
const isAuthorValid = authorInput.validity.valid && authorInput.value;
|
||||||
const isCheckboxChecked = legacyCheckbox.checked || v3Checkbox.checked;
|
const isCheckboxChecked = legacyCheckbox.checked || v3Checkbox.checked;
|
||||||
|
|
||||||
submitButton.disabled = !(isTextValid && isCheckboxChecked);
|
submitButton.disabled =
|
||||||
|
!(isTitleValid || isAuthorValid) || !isCheckboxChecked;
|
||||||
};
|
};
|
||||||
|
|
||||||
const renderForm = () => (
|
const renderForm = () => (
|
||||||
@@ -176,6 +202,7 @@ const VaultPage = (props) => {
|
|||||||
defaultValue={title}
|
defaultValue={title}
|
||||||
onKeyUp={validateForm}
|
onKeyUp={validateForm}
|
||||||
pattern=".{3,}"
|
pattern=".{3,}"
|
||||||
|
title="At least 3 characters"
|
||||||
onKeyDown={(e) => {
|
onKeyDown={(e) => {
|
||||||
if (e.key === 'Enter') {
|
if (e.key === 'Enter') {
|
||||||
if (!searchButtonRef.current.disabled) {
|
if (!searchButtonRef.current.disabled) {
|
||||||
@@ -190,6 +217,35 @@ const VaultPage = (props) => {
|
|||||||
Tip! you can use <code>-</code> to negate words, and{' '}
|
Tip! you can use <code>-</code> to negate words, and{' '}
|
||||||
<code>"word"</code> to specify an exact string.
|
<code>"word"</code> to specify an exact string.
|
||||||
</small>
|
</small>
|
||||||
|
<label>
|
||||||
|
Author of the brew
|
||||||
|
<input
|
||||||
|
ref={authorRef}
|
||||||
|
type="text"
|
||||||
|
name="author"
|
||||||
|
pattern=".{1,}"
|
||||||
|
defaultValue={author}
|
||||||
|
onKeyUp={validateForm}
|
||||||
|
onKeyDown={(e) => {
|
||||||
|
if (e.key === 'Enter') {
|
||||||
|
if (!searchButtonRef.current.disabled) {
|
||||||
|
loadPage(1, true, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
placeholder="Gazook89"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<label>
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
name="owner"
|
||||||
|
ref={ownerRef}
|
||||||
|
defaultChecked={owner}
|
||||||
|
onChange={validateForm}
|
||||||
|
/>
|
||||||
|
Author is the owner
|
||||||
|
</label>
|
||||||
<label>
|
<label>
|
||||||
Results per page
|
Results per page
|
||||||
<select ref={countRef} name="count" defaultValue={count}>
|
<select ref={countRef} name="count" defaultValue={count}>
|
||||||
@@ -202,7 +258,6 @@ const VaultPage = (props) => {
|
|||||||
|
|
||||||
<label>
|
<label>
|
||||||
<input
|
<input
|
||||||
className="renderer"
|
|
||||||
ref={v3Ref}
|
ref={v3Ref}
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
defaultChecked={v3}
|
defaultChecked={v3}
|
||||||
@@ -213,7 +268,6 @@ const VaultPage = (props) => {
|
|||||||
|
|
||||||
<label>
|
<label>
|
||||||
<input
|
<input
|
||||||
className="renderer"
|
|
||||||
ref={legacyRef}
|
ref={legacyRef}
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
defaultChecked={legacy}
|
defaultChecked={legacy}
|
||||||
@@ -325,7 +379,7 @@ const VaultPage = (props) => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (title === '') {
|
if (title === '' && author === '') {
|
||||||
return (
|
return (
|
||||||
<div className="foundBrews noBrews">
|
<div className="foundBrews noBrews">
|
||||||
<h3>No search yet</h3>
|
<h3>No search yet</h3>
|
||||||
|
|||||||
@@ -64,10 +64,14 @@ body {
|
|||||||
select { margin : 0 10px; }
|
select { margin : 0 10px; }
|
||||||
|
|
||||||
input {
|
input {
|
||||||
margin : 0 10px;
|
margin : 0 10px;
|
||||||
|
|
||||||
&.renderer {
|
&:invalid {
|
||||||
position : relative;
|
background : rgb(255, 188, 181);
|
||||||
|
}
|
||||||
|
|
||||||
|
&[type='checkbox'] {
|
||||||
|
position : relative;
|
||||||
display : inline-block;
|
display : inline-block;
|
||||||
width : 50px;
|
width : 50px;
|
||||||
height : 30px;
|
height : 30px;
|
||||||
@@ -84,9 +88,9 @@ body {
|
|||||||
&::before,&::after {
|
&::before,&::after {
|
||||||
position : absolute;
|
position : absolute;
|
||||||
inset : 0;
|
inset : 0;
|
||||||
|
z-index : 5;
|
||||||
padding-top : 2px;
|
padding-top : 2px;
|
||||||
text-align : center;
|
text-align : center;
|
||||||
z-index : 5;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
&::before {
|
&::before {
|
||||||
|
|||||||
@@ -1,33 +1,30 @@
|
|||||||
const HomebrewModel = require('./homebrew.model.js').model;
|
const express = require('express');
|
||||||
const router = require('express').Router();
|
|
||||||
const asyncHandler = require('express-async-handler');
|
const asyncHandler = require('express-async-handler');
|
||||||
|
const HomebrewModel = require('./homebrew.model.js').model;
|
||||||
|
|
||||||
const buildTitleConditions = (inputString) => {
|
const router = express.Router();
|
||||||
return [
|
|
||||||
{
|
const buildTitleConditions = (title) => {
|
||||||
$text: {
|
if (!title) return {};
|
||||||
$search: inputString,
|
return {
|
||||||
$caseSensitive: false,
|
$text: {
|
||||||
},
|
$search: title,
|
||||||
|
$caseSensitive: false,
|
||||||
},
|
},
|
||||||
];
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const buildAuthorConditions = (author, owner) => {
|
||||||
|
if (!author) return {};
|
||||||
|
return owner ? { 'authors.0': author } : { authors: author };
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleErrorResponse = (res, error, functionName) => {
|
const handleErrorResponse = (res, error, functionName) => {
|
||||||
let status;
|
const status = error.response?.status || 500;
|
||||||
let message;
|
const message =
|
||||||
|
status === 503 ? 'Service Unavailable' : 'Internal Server Error';
|
||||||
|
|
||||||
if (error.response && error.response.status) {
|
console.error(`Error in ${functionName}:`, error);
|
||||||
status = error.response.status;
|
|
||||||
} else {
|
|
||||||
status = 500;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (status === 503) {
|
|
||||||
message = 'Service Unavailable';
|
|
||||||
} else {
|
|
||||||
message = 'Internal Server Error';
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.status(status).json({
|
return res.status(status).json({
|
||||||
errorCode: status.toString(),
|
errorCode: status.toString(),
|
||||||
@@ -37,43 +34,35 @@ const handleErrorResponse = (res, error, functionName) => {
|
|||||||
|
|
||||||
const buildBrewsQuery = (legacy, v3) => {
|
const buildBrewsQuery = (legacy, v3) => {
|
||||||
const renderers = [];
|
const renderers = [];
|
||||||
|
if (legacy === 'true') renderers.push('legacy');
|
||||||
|
if (v3 === 'true') renderers.push('V3');
|
||||||
|
|
||||||
if (legacy === 'true') {
|
const brewsQuery = { published: true };
|
||||||
renderers.push('legacy');
|
if (renderers.length > 0) brewsQuery.renderer = { $in: renderers };
|
||||||
}
|
|
||||||
|
|
||||||
if (v3 === 'true') {
|
|
||||||
renderers.push('V3');
|
|
||||||
}
|
|
||||||
|
|
||||||
const brewsQuery = {
|
|
||||||
published: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (renderers.length > 0) {
|
|
||||||
brewsQuery.renderer = { $in: renderers };
|
|
||||||
}
|
|
||||||
|
|
||||||
return brewsQuery;
|
return brewsQuery;
|
||||||
};
|
};
|
||||||
|
|
||||||
const vault = {
|
const vault = {
|
||||||
findBrews: async (req, res, next) => {
|
findBrews: async (req, res) => {
|
||||||
try {
|
try {
|
||||||
console.log(`Query as received in vault api for findBrews:`);
|
console.log(`Query as received in vault api for findBrews:`);
|
||||||
console.table(req.query);
|
console.table(req.query);
|
||||||
|
|
||||||
const title = req.query.title || '';
|
const title = req.query.title || '';
|
||||||
|
const author = req.query.author || '';
|
||||||
|
const owner = req.query.owner === 'true';
|
||||||
const page = Math.max(parseInt(req.query.page) || 1, 1);
|
const page = Math.max(parseInt(req.query.page) || 1, 1);
|
||||||
const mincount = 10;
|
const mincount = 10;
|
||||||
const count = Math.max(parseInt(req.query.count) || 20, mincount);
|
const count = Math.max(parseInt(req.query.count) || 20, mincount);
|
||||||
const skip = (page - 1) * count;
|
const skip = (page - 1) * count;
|
||||||
|
|
||||||
const brewsQuery = buildBrewsQuery(req.query.legacy, req.query.v3);
|
const brewsQuery = buildBrewsQuery(req.query.legacy, req.query.v3);
|
||||||
const titleConditionsArray = buildTitleConditions(title);
|
const titleConditions = buildTitleConditions(title);
|
||||||
|
const authorConditions = buildAuthorConditions(author, owner);
|
||||||
|
|
||||||
const titleQuery = {
|
const combinedQuery = {
|
||||||
$and: [brewsQuery, ...titleConditionsArray],
|
$and: [brewsQuery, titleConditions, authorConditions]
|
||||||
};
|
};
|
||||||
|
|
||||||
const projection = {
|
const projection = {
|
||||||
@@ -82,35 +71,51 @@ const vault = {
|
|||||||
text: 0,
|
text: 0,
|
||||||
textBin: 0,
|
textBin: 0,
|
||||||
};
|
};
|
||||||
const brews = await HomebrewModel.find(titleQuery, projection)
|
const brews = await HomebrewModel.find(combinedQuery, projection)
|
||||||
.skip(skip)
|
.skip(skip)
|
||||||
.limit(count)
|
.limit(count)
|
||||||
.maxTimeMS(5000)
|
.maxTimeMS(5000)
|
||||||
.exec();
|
.exec();
|
||||||
|
|
||||||
|
console.log('query', JSON.stringify(combinedQuery, null, 2));
|
||||||
return res.json({ brews, page });
|
return res.json({ brews, page });
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return handleErrorResponse(res, error, 'findBrews');
|
return handleErrorResponse(res, error, 'findBrews');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
findTotal: async (req, res) => {
|
findTotal: async (req, res) => {
|
||||||
console.log(`Query as received in vault api for totalBrews:`);
|
console.log(`Query as received in vault api for totalBrews:`);
|
||||||
console.table(req.query);
|
console.table(req.query);
|
||||||
try {
|
try {
|
||||||
const title = req.query.title || '';
|
const title = req.query.title || '';
|
||||||
|
const author = req.query.author || '';
|
||||||
|
const owner = req.query.owner === 'true';
|
||||||
|
|
||||||
const brewsQuery = buildBrewsQuery(req.query.legacy, req.query.v3);
|
const brewsQuery = buildBrewsQuery(req.query.legacy, req.query.v3);
|
||||||
const titleConditionsArray = buildTitleConditions(title);
|
const titleConditions = buildTitleConditions(title);
|
||||||
|
const authorConditions = buildAuthorConditions(author, owner);
|
||||||
|
|
||||||
const titleQuery = {
|
const combinedQuery = {
|
||||||
$and: [brewsQuery, ...titleConditionsArray],
|
$and: [brewsQuery, titleConditions, authorConditions]
|
||||||
};
|
};
|
||||||
const totalBrews = await HomebrewModel.countDocuments(titleQuery);
|
|
||||||
console.log('when returning, totalbrews is ', totalBrews, 'for the query ',JSON.stringify(titleQuery));
|
|
||||||
return res.json({ totalBrews });
|
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
'Combined Query:',
|
||||||
|
JSON.stringify(combinedQuery, null, 2)
|
||||||
|
);
|
||||||
|
|
||||||
|
const totalBrews = await HomebrewModel.countDocuments(
|
||||||
|
combinedQuery
|
||||||
|
);
|
||||||
|
console.log(
|
||||||
|
'when returning, totalbrews is ',
|
||||||
|
totalBrews,
|
||||||
|
'for the query',
|
||||||
|
JSON.stringify(combinedQuery)
|
||||||
|
);
|
||||||
|
return res.json({ totalBrews });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return handleErrorResponse(res, error, 'findTotal');
|
return handleErrorResponse(res, error, 'findTotal');
|
||||||
|
|||||||
Reference in New Issue
Block a user