Create queries for lenses

This commit is contained in:
Sam Becker 2024-06-06 13:47:54 -05:00
parent d8d6c68fc8
commit 2580381373
4 changed files with 88 additions and 0 deletions

47
src/lens/index.ts Normal file
View File

@ -0,0 +1,47 @@
import { Photo } from '@/photo';
import { parameterize } from '@/utility/string';
const LENS_PLACEHOLDER: Lens = { make: 'Lens', model: 'Model' };
export type Lens = {
make: string
model: string
};
export interface LensProps {
params: Lens
}
export interface PhotoLensProps {
params: Lens & { photoId: string }
}
export type LensWithCount = {
lensKey: string
lens: Lens
count: number
}
export type Lenses = LensWithCount[];
export const createLensKey = ({ make, model }: Lens) =>
parameterize(`${make}-${model}`, true);
export const getLensFromParams = ({
make,
model,
}: {
make: string,
model: string,
}): Lens => ({
make: parameterize(make, true),
model: parameterize(model, true),
});
export const lensFromPhoto = (
photo: Photo | undefined,
fallback?: Lens,
): Lens =>
photo?.lensMake && photo?.lensModel
? { make: photo.lensMake, model: photo.lensModel }
: fallback ?? LENS_PLACEHOLDER;

View File

@ -15,6 +15,7 @@ import {
getPhotosMostRecentUpdate,
getPhotosMeta,
getUniqueFocalLengths,
getUniqueLenses,
} from '@/photo/db/query';
import { GetPhotosOptions } from './db';
import { parseCachedPhotoDates, parseCachedPhotosDates } from '@/photo';
@ -30,6 +31,7 @@ import {
PREFIX_TAG,
pathForPhoto,
} from '@/site/paths';
import { createLensKey } from '@/lens';
// Table key
const KEY_PHOTOS = 'photos';
@ -37,6 +39,7 @@ const KEY_PHOTO = 'photo';
// Field keys
const KEY_TAGS = 'tags';
const KEY_CAMERAS = 'cameras';
const KEY_LENSES = 'lenses';
const KEY_FILM_SIMULATIONS = 'film-simulations';
const KEY_FOCAL_LENGTHS = 'focal-lengths';
// Type keys
@ -54,6 +57,10 @@ const getPhotosCacheKeyForOption = (
const value = options[option];
return value ? `${option}-${createCameraKey(value)}` : null;
}
case 'lens': {
const value = options[option];
return value ? `${option}-${createLensKey(value)}` : null;
}
case 'takenBefore':
case 'takenAfterInclusive': {
const value = options[option];
@ -192,6 +199,12 @@ export const getUniqueCamerasCached =
[KEY_PHOTOS, KEY_CAMERAS]
);
export const getUniqueLensesCached =
unstable_cache(
getUniqueLenses,
[KEY_PHOTOS, KEY_LENSES]
);
export const getUniqueFilmSimulationsCached =
unstable_cache(
getUniqueFilmSimulations,

View File

@ -1,4 +1,5 @@
import { Camera } from '@/camera';
import { Lens } from '@/lens';
import { FilmSimulation } from '@/simulation';
import { PRIORITY_ORDER_ENABLED } from '@/site/config';
import { parameterize } from '@/utility/string';
@ -13,6 +14,7 @@ export type GetPhotosOptions = {
query?: string
tag?: string
camera?: Camera
lens?: Lens
simulation?: FilmSimulation
focal?: number
takenBefore?: Date
@ -31,6 +33,7 @@ export const getWheresFromOptions = (
query,
tag,
camera,
lens,
simulation,
focal,
} = options;
@ -71,6 +74,12 @@ export const getWheresFromOptions = (
wheresValues.push(parameterize(camera.make, true));
wheresValues.push(parameterize(camera.model, true));
}
if (lens) {
wheres.push(`LOWER(REPLACE(lens_make, ' ', '-'))=$${valuesIndex++}`);
wheres.push(`LOWER(REPLACE(lens_model, ' ', '-'))=$${valuesIndex++}`);
wheresValues.push(parameterize(lens.make, true));
wheresValues.push(parameterize(lens.model, true));
}
if (simulation) {
wheres.push(`film_simulation=$${valuesIndex++}`);
wheresValues.push(simulation);

View File

@ -22,6 +22,7 @@ import {
} from '.';
import { getWheresFromOptions } from '.';
import { FocalLengths } from '@/focal';
import { Lenses, createLensKey } from '@/lens';
const createPhotosTable = () =>
sql`
@ -294,6 +295,24 @@ export const getUniqueCameras = async () =>
})))
, 'getUniqueCameras');
export const getUniqueLenses = async () =>
safelyQueryPhotos(() => sql`
SELECT DISTINCT lens_make||' '||lens_model as lens,
lens_make, lens_model, COUNT(*)
FROM photos
WHERE hidden IS NOT TRUE
AND trim(lens_make) <> ''
AND trim(lens_model) <> ''
GROUP BY lens_make, lens_model
ORDER BY lens ASC
`.then(({ rows }): Lenses => rows
.map(({ lens_make: make, lens_model: model, count }) => ({
lensKey: createLensKey({ make, model }),
lens: { make, model },
count: parseInt(count, 10),
})))
, 'getUniqueCameras');
export const getUniqueFilmSimulations = async () =>
safelyQueryPhotos(() => sql`
SELECT DISTINCT film_simulation, COUNT(*)