91 lines
3.7 KiB
JavaScript
91 lines
3.7 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.listActiveAdvertisements = listActiveAdvertisements;
|
|
exports.listAllAdvertisements = listAllAdvertisements;
|
|
exports.getAdvertisementById = getAdvertisementById;
|
|
exports.createAdvertisement = createAdvertisement;
|
|
exports.updateAdvertisement = updateAdvertisement;
|
|
exports.deleteAdvertisement = deleteAdvertisement;
|
|
const connection_1 = require("../db/connection");
|
|
const activeWindowClause = `(is_active = TRUE
|
|
AND (starts_at IS NULL OR starts_at <= NOW())
|
|
AND (ends_at IS NULL OR ends_at >= NOW()))`;
|
|
async function listActiveAdvertisements(placement) {
|
|
if (placement) {
|
|
return (0, connection_1.query)(`SELECT * FROM advertisements
|
|
WHERE placement = $1 AND ${activeWindowClause}
|
|
ORDER BY sort_order ASC, created_at DESC`, [placement]);
|
|
}
|
|
return (0, connection_1.query)(`SELECT * FROM advertisements
|
|
WHERE ${activeWindowClause}
|
|
ORDER BY placement ASC, sort_order ASC, created_at DESC`);
|
|
}
|
|
async function listAllAdvertisements() {
|
|
return (0, connection_1.query)('SELECT * FROM advertisements ORDER BY placement ASC, sort_order ASC, created_at DESC');
|
|
}
|
|
async function getAdvertisementById(id) {
|
|
const rows = await (0, connection_1.query)('SELECT * FROM advertisements WHERE id = $1', [id]);
|
|
return rows[0] ?? null;
|
|
}
|
|
async function createAdvertisement(input) {
|
|
const rows = await (0, connection_1.query)(`INSERT INTO advertisements (
|
|
placement, title, subtitle, cta_label, link_url, image_url,
|
|
background_gradient, accent_color, is_active, sort_order, starts_at, ends_at
|
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
|
|
RETURNING *`, [
|
|
input.placement ?? 'home_hero',
|
|
input.title,
|
|
input.subtitle ?? null,
|
|
input.ctaLabel ?? null,
|
|
input.linkUrl ?? null,
|
|
input.imageUrl ?? null,
|
|
input.backgroundGradient ?? null,
|
|
input.accentColor ?? '#7C5CFF',
|
|
input.isActive ?? true,
|
|
input.sortOrder ?? 0,
|
|
input.startsAt ?? null,
|
|
input.endsAt ?? null,
|
|
]);
|
|
return rows[0];
|
|
}
|
|
async function updateAdvertisement(id, input) {
|
|
const existing = await getAdvertisementById(id);
|
|
if (!existing)
|
|
return null;
|
|
const rows = await (0, connection_1.query)(`UPDATE advertisements SET
|
|
placement = $2,
|
|
title = $3,
|
|
subtitle = $4,
|
|
cta_label = $5,
|
|
link_url = $6,
|
|
image_url = $7,
|
|
background_gradient = $8,
|
|
accent_color = $9,
|
|
is_active = $10,
|
|
sort_order = $11,
|
|
starts_at = $12,
|
|
ends_at = $13,
|
|
updated_at = NOW()
|
|
WHERE id = $1
|
|
RETURNING *`, [
|
|
id,
|
|
input.placement ?? existing.placement,
|
|
input.title ?? existing.title,
|
|
input.subtitle !== undefined ? input.subtitle : existing.subtitle,
|
|
input.ctaLabel !== undefined ? input.ctaLabel : existing.cta_label,
|
|
input.linkUrl !== undefined ? input.linkUrl : existing.link_url,
|
|
input.imageUrl !== undefined ? input.imageUrl : existing.image_url,
|
|
input.backgroundGradient !== undefined ? input.backgroundGradient : existing.background_gradient,
|
|
input.accentColor ?? existing.accent_color ?? '#7C5CFF',
|
|
input.isActive ?? existing.is_active,
|
|
input.sortOrder ?? existing.sort_order,
|
|
input.startsAt !== undefined ? input.startsAt : existing.starts_at,
|
|
input.endsAt !== undefined ? input.endsAt : existing.ends_at,
|
|
]);
|
|
return rows[0] ?? null;
|
|
}
|
|
async function deleteAdvertisement(id) {
|
|
const rows = await (0, connection_1.query)('DELETE FROM advertisements WHERE id = $1 RETURNING id', [id]);
|
|
return rows.length > 0;
|
|
}
|
|
//# sourceMappingURL=advertisementRepository.js.map
|