From faa2e70e083405744e75e98228a387539391a12a Mon Sep 17 00:00:00 2001 From: ntr Date: Mon, 8 Aug 2022 11:22:05 +1000 Subject: [PATCH] search --- client/src/app.tsx | 2 ++ client/src/routes/search.tsx | 51 ++++++++++++++++++++++++++++++++++++ server/src/api.ts | 8 ++++++ server/src/discogs.ts | 13 ++++++++- 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 client/src/routes/search.tsx diff --git a/client/src/app.tsx b/client/src/app.tsx index 25523f5..d30eff9 100644 --- a/client/src/app.tsx +++ b/client/src/app.tsx @@ -8,6 +8,7 @@ import actions from './actions'; import Header from './components/header'; import Home from './routes/home'; +import Search from './routes/search'; import User from './routes/user'; const store = mcratesStore(); @@ -20,6 +21,7 @@ const App = () => (
+ diff --git a/client/src/routes/search.tsx b/client/src/routes/search.tsx new file mode 100644 index 0000000..6186153 --- /dev/null +++ b/client/src/routes/search.tsx @@ -0,0 +1,51 @@ +import { h } from 'preact'; +import { useState, useEffect } from 'preact/hooks'; +import { connect } from 'unistore/preact'; +import axios from 'axios'; + +import style from './style.less'; +import actions from './../actions'; +import Bundle from './../components/bundle'; + +function result(r) { + return ( +
{r.title}
+ ); +} + +const User = () => { + const [artist, setArtist] = useState('') + const [title, setTitle] = useState(''); + const [results, setResults] = useState([]); + + async function fetchResults() { + const { data } = await axios.post(`http://localhost:41337/want/search`, { + artist, + title, + }); + console.log(data); + setResults(data); + } + + function handleSubmit(e) { + e.preventDefault(); + fetchResults(); + } + + return ( +
+
+ + + + + +
+
+ {results.map(result)} +
+
+ ); +}; + +export default User; diff --git a/server/src/api.ts b/server/src/api.ts index 0179785..1bd0ea0 100644 --- a/server/src/api.ts +++ b/server/src/api.ts @@ -106,6 +106,8 @@ export default function createApi(log: Log, pg: Pg, discogs) { router.get('/bundles', async (ctx, next) => { const bundles = await getBundles(); + const search = await discogs.searchMaster('nine inch nails', 'the fragile'); + log.info(search); ctx.response.body = bundles; }) @@ -146,6 +148,12 @@ export default function createApi(log: Log, pg: Pg, discogs) { ctx.response.body = want; }); + router.post('/want/search', async function wantPost(ctx, next) { + const { artist, title } = ctx.request.body; + const results = await discogs.search(artist, title); + ctx.response.body = results; + }); + app.use(router.routes()); return { diff --git a/server/src/discogs.ts b/server/src/discogs.ts index 9caeff3..89f91be 100644 --- a/server/src/discogs.ts +++ b/server/src/discogs.ts @@ -11,7 +11,7 @@ import { logAxiosError } from './utils'; import { Pg } from './pg'; export default function discogs(log: Log, pg: Pg) { - const TOKEN = 'wRTXuYZGOPsIQgKRGKCAkUkidGtvpSfQgPWkYIpD'; + const TOKEN = 'Discogs token=wRTXuYZGOPsIQgKRGKCAkUkidGtvpSfQgPWkYIpD'; const apiBase = 'https://api.discogs.com' const discogsBase = 'https://www.discogs.com' @@ -62,6 +62,16 @@ export default function discogs(log: Log, pg: Pg) { return data; } + async function search(artist: string, title: string) { + const url = new URL(`${apiBase}/database/search`); + url.searchParams.append('artist', artist); + url.searchParams.append('title', title); + url.searchParams.append('type', 'release'); + url.searchParams.append('format', 'vinyl'); + const { data: { results } } = await api.get(url.href); + return results; + } + async function inventory(user: string) { const res = await api.get(`${apiBase}/users/${user}/inventory`); return res.data.listings; @@ -103,5 +113,6 @@ export default function discogs(log: Log, pg: Pg) { listings, listing, release, + search, }; } \ No newline at end of file