Add caption, description fields to Photo

This commit is contained in:
Sam Becker 2024-03-15 20:40:06 -05:00
parent 195569730f
commit 2ec32cac12
3 changed files with 44 additions and 14 deletions

View File

@ -54,6 +54,8 @@ const FORM_METADATA = (
tagOptions?: AnnotatedTag[] tagOptions?: AnnotatedTag[]
): Record<keyof PhotoFormData, FormMeta> => ({ ): Record<keyof PhotoFormData, FormMeta> => ({
title: { label: 'title', capitalize: true }, title: { label: 'title', capitalize: true },
caption: { label: 'caption', capitalize: true },
description: { label: 'description', capitalize: true, hide: true},
tags: { tags: {
label: 'tags', label: 'tags',
tagOptions, tagOptions,

View File

@ -47,6 +47,8 @@ export interface PhotoDbInsert extends PhotoExif {
extension: string extension: string
blurData?: string blurData?: string
title?: string title?: string
caption?: string
description?: string
tags?: string[] tags?: string[]
locationName?: string locationName?: string
priorityOrder?: number priorityOrder?: number

View File

@ -28,6 +28,8 @@ const sqlCreatePhotosTable = () =>
aspect_ratio REAL DEFAULT 1.5, aspect_ratio REAL DEFAULT 1.5,
blur_data TEXT, blur_data TEXT,
title VARCHAR(255), title VARCHAR(255),
caption TEXT,
description TEXT,
tags VARCHAR(255)[], tags VARCHAR(255)[],
make VARCHAR(255), make VARCHAR(255),
model VARCHAR(255), model VARCHAR(255),
@ -50,9 +52,18 @@ const sqlCreatePhotosTable = () =>
) )
`; `;
// MIGRATION 01
const MIGRATION_FIELDS_01 = ['caption', 'description'];
const sqlRunMigration01 = () =>
sql`
ALTER TABLE photos
ADD COLUMN caption TEXT,
ADD COLUMN description TEXT
`;
// Must provide id as 8-character nanoid // Must provide id as 8-character nanoid
export const sqlInsertPhoto = (photo: PhotoDbInsert) => { export const sqlInsertPhoto = (photo: PhotoDbInsert) =>
return sql` safelyQueryPhotos(() => sql`
INSERT INTO photos ( INSERT INTO photos (
id, id,
url, url,
@ -60,6 +71,8 @@ export const sqlInsertPhoto = (photo: PhotoDbInsert) => {
aspect_ratio, aspect_ratio,
blur_data, blur_data,
title, title,
caption,
description,
tags, tags,
make, make,
model, model,
@ -85,6 +98,8 @@ export const sqlInsertPhoto = (photo: PhotoDbInsert) => {
${photo.aspectRatio}, ${photo.aspectRatio},
${photo.blurData}, ${photo.blurData},
${photo.title}, ${photo.title},
${photo.caption},
${photo.description},
${convertArrayToPostgresString(photo.tags)}, ${convertArrayToPostgresString(photo.tags)},
${photo.make}, ${photo.make},
${photo.model}, ${photo.model},
@ -103,17 +118,18 @@ export const sqlInsertPhoto = (photo: PhotoDbInsert) => {
${photo.takenAt}, ${photo.takenAt},
${photo.takenAtNaive} ${photo.takenAtNaive}
) )
`; `);
};
export const sqlUpdatePhoto = (photo: PhotoDbInsert) => export const sqlUpdatePhoto = (photo: PhotoDbInsert) =>
sql` safelyQueryPhotos(() => sql`
UPDATE photos SET UPDATE photos SET
url=${photo.url}, url=${photo.url},
extension=${photo.extension}, extension=${photo.extension},
aspect_ratio=${photo.aspectRatio}, aspect_ratio=${photo.aspectRatio},
blur_data=${photo.blurData}, blur_data=${photo.blurData},
title=${photo.title}, title=${photo.title},
caption=${photo.caption},
description=${photo.description},
tags=${convertArrayToPostgresString(photo.tags)}, tags=${convertArrayToPostgresString(photo.tags)},
make=${photo.make}, make=${photo.make},
model=${photo.model}, model=${photo.model},
@ -133,27 +149,29 @@ export const sqlUpdatePhoto = (photo: PhotoDbInsert) =>
taken_at_naive=${photo.takenAtNaive}, taken_at_naive=${photo.takenAtNaive},
updated_at=${(new Date()).toISOString()} updated_at=${(new Date()).toISOString()}
WHERE id=${photo.id} WHERE id=${photo.id}
`; `);
export const sqlDeletePhotoTagGlobally = (tag: string) => export const sqlDeletePhotoTagGlobally = (tag: string) =>
sql` safelyQueryPhotos(() => sql`
UPDATE photos UPDATE photos
SET tags=ARRAY_REMOVE(tags, ${tag}) SET tags=ARRAY_REMOVE(tags, ${tag})
WHERE ${tag}=ANY(tags) WHERE ${tag}=ANY(tags)
`; `);
export const sqlRenamePhotoTagGlobally = (tag: string, updatedTag: string) => export const sqlRenamePhotoTagGlobally = (tag: string, updatedTag: string) =>
sql` safelyQueryPhotos(() => sql`
UPDATE photos UPDATE photos
SET tags=ARRAY_REPLACE(tags, ${tag}, ${updatedTag}) SET tags=ARRAY_REPLACE(tags, ${tag}, ${updatedTag})
WHERE ${tag}=ANY(tags) WHERE ${tag}=ANY(tags)
`; `);
export const sqlDeletePhoto = (id: string) => export const sqlDeletePhoto = (id: string) =>
sql`DELETE FROM photos WHERE id=${id}`; safelyQueryPhotos(() => sql`DELETE FROM photos WHERE id=${id}`);
const sqlGetPhoto = (id: string) => const sqlGetPhoto = (id: string) =>
sql<PhotoDb>`SELECT * FROM photos WHERE id=${id} LIMIT 1`; safelyQueryPhotos(() =>
sql<PhotoDb>`SELECT * FROM photos WHERE id=${id} LIMIT 1`
);
const sqlGetPhotosCount = async () => sql` const sqlGetPhotosCount = async () => sql`
SELECT COUNT(*) FROM photos SELECT COUNT(*) FROM photos
@ -291,8 +309,16 @@ const safelyQueryPhotos = async <T>(callback: () => Promise<T>): Promise<T> => {
try { try {
result = await callback(); result = await callback();
} catch (e: any) { } catch (e: any) {
if (/relation "photos" does not exist/i.test(e.message)) { if (MIGRATION_FIELDS_01.some(field => new RegExp(
console.log('Creating table "photos" because it did not exist'); `column "${field}" of relation "photos" does not exist`,
'i',
).test(e.message))) {
console.log('Running migration 01 ...');
await sqlRunMigration01();
result = await callback();
} else if (/relation "photos" does not exist/i.test(e.message)) {
// If the table does not exist, create it
console.log('Creating photos table ...');
await sqlCreatePhotosTable(); await sqlCreatePhotosTable();
result = await callback(); result = await callback();
} else if (/endpoint is in transition/i.test(e.message)) { } else if (/endpoint is in transition/i.test(e.message)) {