Add admin slideshow, horizontal animated calendar, and weather strip
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { requireRole } from '@/lib/auth';
|
||||
import { db } from '@/lib/db';
|
||||
|
||||
export async function PUT(request: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
await requireRole('admin');
|
||||
const id = parseInt(params.id, 10);
|
||||
const body = await request.json();
|
||||
const { date, title, type, description, color, active } = body;
|
||||
|
||||
const { rows } = await db.query(
|
||||
`UPDATE calendar_events SET date = $1, title = $2, type = $3, description = $4, color = $5, active = $6
|
||||
WHERE id = $7 RETURNING *`,
|
||||
[date, title, type, description || null, color || '#E8A849', active, id]
|
||||
);
|
||||
|
||||
if (rows.length === 0) {
|
||||
return NextResponse.json({ error: 'Event not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ data: rows[0] });
|
||||
} catch (error: any) {
|
||||
if ((error as Error).message === 'Unauthorized') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
console.error('PUT /api/calendar_events/:id error:', error);
|
||||
return NextResponse.json({ error: error.message || 'Failed to update event' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(request: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
await requireRole('admin');
|
||||
const id = parseInt(params.id, 10);
|
||||
await db.query('DELETE FROM calendar_events WHERE id = $1', [id]);
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch (error: any) {
|
||||
if ((error as Error).message === 'Unauthorized') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
console.error('DELETE /api/calendar_events/:id error:', error);
|
||||
return NextResponse.json({ error: error.message || 'Failed to delete event' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { requireRole } from '@/lib/auth';
|
||||
import { db } from '@/lib/db';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const year = parseInt(request.nextUrl.searchParams.get('year') || '', 10) || new Date().getFullYear();
|
||||
const start = `${year}-01-01`;
|
||||
const end = `${year}-12-31`;
|
||||
|
||||
const { rows } = await db.query(
|
||||
`SELECT * FROM calendar_events
|
||||
WHERE date BETWEEN $1 AND $2 AND active = TRUE
|
||||
ORDER BY date`,
|
||||
[start, end]
|
||||
);
|
||||
|
||||
return NextResponse.json({ data: rows });
|
||||
} catch (error: any) {
|
||||
console.error('GET /api/calendar_events error:', error);
|
||||
return NextResponse.json({ error: error.message || 'Failed to fetch events' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
await requireRole('admin');
|
||||
const body = await request.json();
|
||||
const { date, title, type, description, color, active } = body;
|
||||
|
||||
if (!date || !title || !type) {
|
||||
return NextResponse.json({ error: 'Missing required fields' }, { status: 400 });
|
||||
}
|
||||
|
||||
const { rows } = await db.query(
|
||||
`INSERT INTO calendar_events (date, title, type, description, color, active)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING *`,
|
||||
[date, title, type, description || null, color || '#E8A849', active !== false]
|
||||
);
|
||||
|
||||
return NextResponse.json({ data: rows[0] });
|
||||
} catch (error: any) {
|
||||
if ((error as Error).message === 'Unauthorized') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
console.error('POST /api/calendar_events error:', error);
|
||||
return NextResponse.json({ error: error.message || 'Failed to create event' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { requireRole } from '@/lib/auth';
|
||||
import { db } from '@/lib/db';
|
||||
|
||||
export async function PUT(request: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
await requireRole('admin');
|
||||
const id = parseInt(params.id, 10);
|
||||
const body = await request.json();
|
||||
const { title, subtitle, image_id, image_url, active, sort_order } = body;
|
||||
|
||||
const { rows } = await db.query(
|
||||
`UPDATE slides SET title = $1, subtitle = $2, image_id = $3, image_url = $4, active = $5, sort_order = $6
|
||||
WHERE id = $7 RETURNING *`,
|
||||
[title || null, subtitle || null, image_id || null, image_url || null, active, sort_order || 0, id]
|
||||
);
|
||||
|
||||
if (rows.length === 0) {
|
||||
return NextResponse.json({ error: 'Slide not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ data: rows[0] });
|
||||
} catch (error: any) {
|
||||
if ((error as Error).message === 'Unauthorized') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
console.error('PUT /api/slides/:id error:', error);
|
||||
return NextResponse.json({ error: error.message || 'Failed to update slide' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(request: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
await requireRole('admin');
|
||||
const id = parseInt(params.id, 10);
|
||||
await db.query('DELETE FROM slides WHERE id = $1', [id]);
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch (error: any) {
|
||||
if ((error as Error).message === 'Unauthorized') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
console.error('DELETE /api/slides/:id error:', error);
|
||||
return NextResponse.json({ error: error.message || 'Failed to delete slide' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { requireRole } from '@/lib/auth';
|
||||
import { db } from '@/lib/db';
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const { rows } = await db.query(
|
||||
`SELECT s.*, i.data as image_data
|
||||
FROM slides s
|
||||
LEFT JOIN images i ON s.image_id = i.id
|
||||
WHERE s.active = TRUE
|
||||
ORDER BY s.sort_order, s.id`
|
||||
);
|
||||
return NextResponse.json({ data: rows });
|
||||
} catch (error: any) {
|
||||
console.error('GET /api/slides error:', error);
|
||||
return NextResponse.json({ error: error.message || 'Failed to fetch slides' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
await requireRole('admin');
|
||||
const body = await request.json();
|
||||
const { title, subtitle, image_id, image_url, active, sort_order } = body;
|
||||
|
||||
const { rows } = await db.query(
|
||||
`INSERT INTO slides (title, subtitle, image_id, image_url, active, sort_order)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING *`,
|
||||
[title || null, subtitle || null, image_id || null, image_url || null, active !== false, sort_order || 0]
|
||||
);
|
||||
|
||||
return NextResponse.json({ data: rows[0] });
|
||||
} catch (error: any) {
|
||||
if ((error as Error).message === 'Unauthorized') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
console.error('POST /api/slides error:', error);
|
||||
return NextResponse.json({ error: error.message || 'Failed to create slide' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
// Free public weather API for Otavalo, Ecuador (lat=-0.2329, lon=-78.2603)
|
||||
const url = 'https://api.open-meteo.com/v1/forecast?latitude=-0.2329&longitude=-78.2603¤t_weather=true&daily=temperature_2m_max,temperature_2m_min,weathercode&timezone=America/Guayaquil';
|
||||
const res = await fetch(url, { next: { revalidate: 1800 } });
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error('Open-Meteo response not OK');
|
||||
}
|
||||
|
||||
const json = await res.json();
|
||||
|
||||
const codeMap: Record<number, string> = {
|
||||
0: 'Clear', 1: 'Mainly clear', 2: 'Partly cloudy', 3: 'Overcast',
|
||||
45: 'Fog', 48: 'Fog', 51: 'Light drizzle', 53: 'Drizzle', 55: 'Heavy drizzle',
|
||||
61: 'Light rain', 63: 'Rain', 65: 'Heavy rain', 71: 'Light snow', 73: 'Snow',
|
||||
75: 'Heavy snow', 80: 'Rain showers', 81: 'Rain showers', 82: 'Heavy showers',
|
||||
95: 'Thunderstorm', 96: 'Thunderstorm', 99: 'Thunderstorm'
|
||||
};
|
||||
|
||||
const current = json.current_weather || {};
|
||||
const daily = json.daily || {};
|
||||
|
||||
return NextResponse.json({
|
||||
data: {
|
||||
current: {
|
||||
temperature: current.temperature,
|
||||
windspeed: current.windspeed,
|
||||
weathercode: current.weathercode,
|
||||
summary: codeMap[current.weathercode] || 'Unknown',
|
||||
},
|
||||
today: {
|
||||
max: daily.temperature_2m_max?.[0],
|
||||
min: daily.temperature_2m_min?.[0],
|
||||
weathercode: daily.weathercode?.[0],
|
||||
summary: codeMap[daily.weathercode?.[0]] || 'Unknown',
|
||||
},
|
||||
},
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('Weather API error:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
data: {
|
||||
current: { temperature: 18, windspeed: 6, weathercode: 1, summary: 'Mainly clear' },
|
||||
today: { max: 22, min: 10, weathercode: 1, summary: 'Mainly clear' },
|
||||
},
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user