// db/photos.js const knex = require('./knex'); async function insertOrUpdatePhoto(row) { // row deve contenere almeno id, user, cartella, name, path, thumb1, thumb2, fast_hash, mtimeMs, etc. const exists = await knex('photos').where({ id: row.id }).first(); if (!exists) { await knex('photos').insert({ id: row.id, user: row.user, cartella: row.cartella, name: row.name, path: row.path, thumb1: row.thumb1 || null, thumb2: row.thumb2 || null, mime_type: row.mime_type || null, width: row.width || null, height: row.height || null, rotation: row.rotation || null, size_bytes: row.size_bytes || null, mtimeMs: row.mtimeMs || null, taken_at: row.taken_at || null, fast_hash: row.fast_hash || null, created_at: knex.fn.now(), updated_at: knex.fn.now() }); } else { await knex('photos').where({ id: row.id }).update({ path: row.path, thumb1: row.thumb1 || exists.thumb1, thumb2: row.thumb2 || exists.thumb2, mime_type: row.mime_type || exists.mime_type, width: row.width || exists.width, height: row.height || exists.height, rotation: row.rotation || exists.rotation, size_bytes: row.size_bytes || exists.size_bytes, mtimeMs: row.mtimeMs || exists.mtimeMs, taken_at: row.taken_at || exists.taken_at, fast_hash: row.fast_hash || exists.fast_hash, updated_at: knex.fn.now(), deleted_at: null }); } } async function markPhotoDeletedHard(id, user) { await knex('photos').where({ id, user }).del(); await knex('deleted_hard').insert({ id, user, deleted_at: knex.fn.now() }).catch(() => {}); } async function getPhotosSince(user, sinceIso, limit = 1000) { // ritorna foto aggiornate dopo sinceIso (ISO string) return knex('photos') .where({ user }) .andWhere('updated_at', '>', sinceIso) .orderBy('updated_at', 'asc') .limit(limit) .select('*'); } async function getDeletedHardSince(user, sinceIso) { return knex('deleted_hard') .where({ user }) .andWhere('deleted_at', '>', sinceIso) .select('id', 'deleted_at'); } module.exports = { insertOrUpdatePhoto, markPhotoDeletedHard, getPhotosSince, getDeletedHardSince };