Skip to content

Commit

Permalink
Prepare API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
LetrixZ committed Jun 24, 2024
1 parent 4d56dcc commit 1ff2ebd
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 32 deletions.
10 changes: 10 additions & 0 deletions server/src/api/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,27 @@ pub struct ArchiveData {
pub hash: String,
pub pages: i16,
pub size: i64,
#[serde(skip_serializing_if = "Option::is_none")]
pub cover: Option<ImageDimensions>,
pub thumbnail: i16,
pub images: Vec<Image>,
pub created_at: NaiveDateTime,
pub released_at: NaiveDateTime,
#[serde(skip_serializing_if = "<[_]>::is_empty")]
pub artists: Vec<Taxonomy>,
#[serde(skip_serializing_if = "<[_]>::is_empty")]
pub circles: Vec<Taxonomy>,
#[serde(skip_serializing_if = "<[_]>::is_empty")]
pub magazines: Vec<Taxonomy>,
#[serde(skip_serializing_if = "<[_]>::is_empty")]
pub events: Vec<Taxonomy>,
#[serde(skip_serializing_if = "<[_]>::is_empty")]
pub publishers: Vec<Taxonomy>,
#[serde(skip_serializing_if = "<[_]>::is_empty")]
pub parodies: Vec<Taxonomy>,
#[serde(skip_serializing_if = "<[_]>::is_empty")]
pub tags: Vec<Tag>,
#[serde(skip_serializing_if = "<[_]>::is_empty")]
pub sources: Vec<Source>,
}

Expand Down Expand Up @@ -194,6 +203,7 @@ pub struct ArchiveListItem {
pub slug: String,
pub hash: String,
pub title: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub cover: Option<ImageDimensions>,
#[serde(skip_serializing_if = "<[_]>::is_empty")]
pub artists: Vec<Taxonomy>,
Expand Down
45 changes: 28 additions & 17 deletions server/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ pub async fn fetch_archive_data(
).fetch_optional(pool).await?;

if let Some(row) = row {
let cover = row
.cover
.map(|cover: serde_json::Value| serde_json::from_value(cover).ok())
.unwrap_or_default()
.filter(|cover: &ImageDimensions| cover.width.is_some() || cover.height.is_some());

let archive = Archive {
id: row.id,
slug: row.slug,
Expand All @@ -340,10 +346,7 @@ pub async fn fetch_archive_data(
pages: row.pages.unwrap_or_default(),
size: row.size,
thumbnail: row.thumbnail,
cover: row
.cover
.map(|cover| serde_json::from_value(cover).ok())
.unwrap_or_default(),
cover,
images: row
.images
.and_then(|images| serde_json::from_value(images).ok())
Expand Down Expand Up @@ -720,19 +723,27 @@ pub async fn search(

let archives = rows
.iter()
.map(|row| ArchiveListItem {
id: row.get(0),
slug: row.get(1),
hash: row.get(2),
title: row.get(3),
cover: row.try_get::<Json<_>, _>(4).map(|r| r.0).unwrap_or(None),
artists: row.get::<Json<_>, _>(5).0,
circles: row.get::<Json<_>, _>(6).0,
magazines: row.get::<Json<_>, _>(7).0,
events: row.get::<Json<_>, _>(8).0,
publishers: row.get::<Json<_>, _>(9).0,
parodies: row.get::<Json<_>, _>(10).0,
tags: row.get::<Json<_>, _>(11).0,
.map(|row| {
let cover = row
.try_get::<Json<_>, _>(4)
.map(|r| r.0)
.unwrap_or(None)
.filter(|cover: &ImageDimensions| cover.width.is_some() || cover.height.is_some());

ArchiveListItem {
id: row.get(0),
slug: row.get(1),
hash: row.get(2),
title: row.get(3),
cover,
artists: row.get::<Json<_>, _>(5).0,
circles: row.get::<Json<_>, _>(6).0,
magazines: row.get::<Json<_>, _>(7).0,
events: row.get::<Json<_>, _>(8).0,
publishers: row.get::<Json<_>, _>(9).0,
parodies: row.get::<Json<_>, _>(10).0,
tags: row.get::<Json<_>, _>(11).0,
}
})
.collect();

Expand Down
6 changes: 3 additions & 3 deletions web/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ export interface Archive {
id: number;
slug: string;
title: string;
description: string | null;
description?: string;
hash: string;
pages: number;
size: number;
cover: ImageDimensions | null;
cover?: ImageDimensions;
thumbnail: number;
images: Image[];
created_at: string;
Expand All @@ -18,7 +18,7 @@ export interface Archive {
publishers?: Taxonomy[];
parodies?: Taxonomy[];
tags?: Tag[];
sources: Source[];
sources?: Source[];
}

export type ArchiveId = Pick<Archive, 'id' | 'slug'>;
Expand Down
10 changes: 7 additions & 3 deletions web/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ export const tagWeights: [string, number][] = [
];

export function isSpread(image: Image) {
return image.width > image.height;
if (image.width && image.height) {
return image.width > image.height;
}

return false;
}

// https://stackoverflow.com/a/1349426
Expand Down Expand Up @@ -249,12 +253,12 @@ export function randomInt(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

export async function handleFetchError(res: Response) {
export async function handleFetchError<T>(res: Response) {
if (!res.ok) {
const { message } = await res.json();
error(res.status, { status: res.status, message });
} else {
return res.json();
return res.json() as T;
}
}

Expand Down
2 changes: 1 addition & 1 deletion web/src/routes/(app)/g/[id]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@
</InfoSection>
{/if}

{#if archive.sources.length}
{#if archive.sources?.length}
<InfoSection name="Sources">
<div class="flex flex-wrap gap-2">
{#each archive.sources as source}
Expand Down
10 changes: 7 additions & 3 deletions web/src/routes/api/g/[id]/+server.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { env } from '$env/dynamic/private';
import { env as publicEnv } from '$env/dynamic/public';
import type { Archive } from '$lib/models';
import { handleFetchError } from '$lib/utils';
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';

export const GET: RequestHandler = async ({ params, setHeaders }) => {
const data = (await fetch(`${env.SERVER_URL}/archive/${params.id}`).then(
handleFetchError
)) as Promise<Archive>;
const data = await fetch(`${env.SERVER_URL}/archive/${params.id}`)
.then(handleFetchError<Archive>)
.then((archive) => ({
...archive,
thumbnail_url: `${publicEnv.PUBLIC_CDN_URL}/image/${archive.hash}/cover`,
}));

setHeaders({ 'cache-control': 'public, max-age=300' });

Expand Down
17 changes: 12 additions & 5 deletions web/src/routes/api/library/+server.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { env } from '$env/dynamic/private';
import { env as publicEnv } from '$env/dynamic/public';
import type { LibraryPage } from '$lib/models';
import { handleFetchError } from '$lib/utils';
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { handleFetchError } from '$lib/utils';
import type { LibraryPage } from '$lib/models';

export const GET: RequestHandler = async ({ url, setHeaders }) => {
const data = (await fetch(`${env.SERVER_URL}/library${url.search}`).then(
handleFetchError
)) as Promise<LibraryPage>;
const data = await fetch(`${env.SERVER_URL}/library${url.search}`)
.then(handleFetchError<LibraryPage>)
.then((libraryPage) => ({
...libraryPage,
archives: libraryPage.archives.map((archive) => ({
...archive,
thumbnail_url: `${publicEnv.PUBLIC_CDN_URL}/image/${archive.hash}/cover`,
})),
}));

setHeaders({ 'cache-control': 'public, max-age=300' });

Expand Down

0 comments on commit 1ff2ebd

Please sign in to comment.