2022-08-24 20:36:40 +00:00
|
|
|
import { json as json$1 } from '@sveltejs/kit';
|
2022-07-07 19:32:55 +00:00
|
|
|
import type { RequestHandler } from '@sveltejs/kit';
|
2022-07-27 22:05:22 +00:00
|
|
|
import type { GameType, SearchQuery } from '$lib/types';
|
|
|
|
|
import { mapAPIGameToBoredGame } from '$lib/util/gameMapper';
|
2022-04-21 02:41:26 +00:00
|
|
|
|
2022-07-19 21:04:32 +00:00
|
|
|
export const POST: RequestHandler = async ({ request }) => {
|
2022-04-21 02:41:26 +00:00
|
|
|
const form = await request.formData();
|
2022-04-24 23:38:12 +00:00
|
|
|
console.log('form', form);
|
2022-04-25 01:00:27 +00:00
|
|
|
const queryParams: SearchQuery = {
|
2022-04-21 02:41:26 +00:00
|
|
|
order_by: 'rank',
|
|
|
|
|
ascending: false,
|
2022-04-25 01:00:27 +00:00
|
|
|
limit: 20,
|
2022-07-07 19:32:55 +00:00
|
|
|
client_id: import.meta.env.VITE_PUBLIC_CLIENT_ID
|
|
|
|
|
};
|
2022-04-21 02:41:26 +00:00
|
|
|
|
2022-07-11 04:17:59 +00:00
|
|
|
const id = form.get('id');
|
2022-08-02 06:26:57 +00:00
|
|
|
const ids = form.get('ids');
|
2022-04-21 02:41:26 +00:00
|
|
|
const minAge = form.get('minAge');
|
|
|
|
|
const minPlayers = form.get('minPlayers');
|
|
|
|
|
const maxPlayers = form.get('maxPlayers');
|
2022-04-24 23:38:12 +00:00
|
|
|
const exactMinAge = form.get('exactMinAge') || false;
|
|
|
|
|
const exactMinPlayers = form.get('exactMinPlayers') || false;
|
|
|
|
|
const exactMaxPlayers = form.get('exactMaxPlayers') || false;
|
2022-04-21 02:41:26 +00:00
|
|
|
const random = form.get('random') === 'on' || false;
|
2022-04-25 01:00:27 +00:00
|
|
|
|
2022-04-22 00:19:48 +00:00
|
|
|
if (minAge) {
|
|
|
|
|
if (exactMinAge) {
|
|
|
|
|
queryParams.min_age = +minAge;
|
|
|
|
|
} else {
|
|
|
|
|
queryParams.gt_min_age = +minAge === 1 ? 0 : +minAge - 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (minPlayers) {
|
|
|
|
|
if (exactMinPlayers) {
|
|
|
|
|
queryParams.min_players = +minPlayers;
|
|
|
|
|
} else {
|
2022-07-07 19:32:55 +00:00
|
|
|
queryParams.gt_min_players = +minPlayers === 1 ? 0 : +minPlayers - 1;
|
2022-04-22 00:19:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (maxPlayers) {
|
|
|
|
|
if (exactMaxPlayers) {
|
|
|
|
|
queryParams.max_players = +maxPlayers;
|
|
|
|
|
} else {
|
|
|
|
|
queryParams.lt_max_players = +maxPlayers + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-11 04:17:59 +00:00
|
|
|
|
|
|
|
|
if (id) {
|
|
|
|
|
queryParams.ids = new Array(`${id}`);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-02 06:26:57 +00:00
|
|
|
if (ids) {
|
|
|
|
|
// TODO: Pass in ids array from localstorage / game store
|
|
|
|
|
queryParams.ids = new Array(ids);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 02:41:26 +00:00
|
|
|
queryParams.random = random;
|
2022-04-24 23:38:12 +00:00
|
|
|
console.log('queryParams', queryParams);
|
2022-04-21 02:41:26 +00:00
|
|
|
|
2022-08-02 06:26:57 +00:00
|
|
|
const newQueryParams: Record<string, string> = {};
|
2022-04-21 02:41:26 +00:00
|
|
|
for (const key in queryParams) {
|
2022-08-02 06:26:57 +00:00
|
|
|
newQueryParams[key] = `${queryParams[key as keyof typeof queryParams]}`;
|
2022-04-21 02:41:26 +00:00
|
|
|
}
|
2022-04-24 23:38:12 +00:00
|
|
|
|
2022-04-21 02:41:26 +00:00
|
|
|
const urlQueryParams = new URLSearchParams(newQueryParams);
|
|
|
|
|
|
2022-08-02 06:26:57 +00:00
|
|
|
const url = `https://api.boardgameatlas.com/api/search${urlQueryParams ? `?${urlQueryParams}` : ''
|
|
|
|
|
}`;
|
2022-04-21 02:41:26 +00:00
|
|
|
const response = await fetch(url, {
|
|
|
|
|
method: 'get',
|
|
|
|
|
headers: {
|
|
|
|
|
'content-type': 'application/json'
|
2022-07-07 19:32:55 +00:00
|
|
|
}
|
2022-04-21 02:41:26 +00:00
|
|
|
});
|
2022-07-27 22:05:22 +00:00
|
|
|
// console.log('response', response);
|
2022-04-21 02:41:26 +00:00
|
|
|
if (response.status === 404) {
|
|
|
|
|
// user hasn't created a todo list.
|
|
|
|
|
// start with an empty array
|
2022-08-24 20:36:40 +00:00
|
|
|
return json$1({
|
|
|
|
|
games: []
|
|
|
|
|
});
|
2022-04-21 02:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
const gameResponse = await response.json();
|
2022-07-27 22:05:22 +00:00
|
|
|
const gameList = gameResponse?.games;
|
|
|
|
|
const games: GameType[] = [];
|
2022-07-28 00:05:54 +00:00
|
|
|
gameList.forEach((game) => {
|
|
|
|
|
games.push(mapAPIGameToBoredGame(game));
|
2022-07-27 22:05:22 +00:00
|
|
|
});
|
2022-04-21 02:41:26 +00:00
|
|
|
console.log('games', games);
|
2022-08-24 20:36:40 +00:00
|
|
|
return json$1({
|
|
|
|
|
games
|
|
|
|
|
});
|
2022-04-21 02:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-24 20:36:40 +00:00
|
|
|
return new Response(undefined, { status: response.status });
|
2022-07-07 19:32:55 +00:00
|
|
|
};
|