feat(backend): initial setup for cms backend service
This commit is contained in:
99
backend/src/routes/content.ts
Normal file
99
backend/src/routes/content.ts
Normal file
@ -0,0 +1,99 @@
|
||||
import { FastifyPluginAsync } from 'fastify';
|
||||
import { z } from 'zod';
|
||||
import { db } from '../config/database.js';
|
||||
import { contentSections } from '../db/schema.js';
|
||||
import { eq } from 'drizzle-orm';
|
||||
|
||||
const contentSectionSchema = z.object({
|
||||
contentJson: z.record(z.any()),
|
||||
});
|
||||
|
||||
const contentRoute: FastifyPluginAsync = async (fastify) => {
|
||||
|
||||
// Get content section
|
||||
fastify.get('/content/:section', {
|
||||
preHandler: [fastify.authenticate],
|
||||
}, async (request, reply) => {
|
||||
const { section } = request.params as { section: string };
|
||||
|
||||
const [content] = await db
|
||||
.select()
|
||||
.from(contentSections)
|
||||
.where(eq(contentSections.sectionName, section))
|
||||
.limit(1);
|
||||
|
||||
if (!content) {
|
||||
return reply.code(404).send({ error: 'Content section not found' });
|
||||
}
|
||||
|
||||
return {
|
||||
section: content.sectionName,
|
||||
content: content.contentJson,
|
||||
updatedAt: content.updatedAt,
|
||||
};
|
||||
});
|
||||
|
||||
// Update content section
|
||||
fastify.put('/content/:section', {
|
||||
schema: {
|
||||
body: contentSectionSchema,
|
||||
},
|
||||
preHandler: [fastify.authenticate],
|
||||
}, async (request, reply) => {
|
||||
const { section } = request.params as { section: string };
|
||||
const { contentJson } = request.body as z.infer<typeof contentSectionSchema>;
|
||||
|
||||
// Check if section exists
|
||||
const [existing] = await db
|
||||
.select()
|
||||
.from(contentSections)
|
||||
.where(eq(contentSections.sectionName, section))
|
||||
.limit(1);
|
||||
|
||||
let result;
|
||||
|
||||
if (existing) {
|
||||
// Update existing
|
||||
[result] = await db
|
||||
.update(contentSections)
|
||||
.set({
|
||||
contentJson,
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(contentSections.sectionName, section))
|
||||
.returning();
|
||||
} else {
|
||||
// Create new
|
||||
[result] = await db
|
||||
.insert(contentSections)
|
||||
.values({
|
||||
sectionName: section,
|
||||
contentJson,
|
||||
})
|
||||
.returning();
|
||||
}
|
||||
|
||||
return {
|
||||
section: result.sectionName,
|
||||
content: result.contentJson,
|
||||
updatedAt: result.updatedAt,
|
||||
};
|
||||
});
|
||||
|
||||
// List all content sections
|
||||
fastify.get('/content', {
|
||||
preHandler: [fastify.authenticate],
|
||||
}, async (request, reply) => {
|
||||
const sections = await db.select().from(contentSections);
|
||||
|
||||
return {
|
||||
sections: sections.map(s => ({
|
||||
section: s.sectionName,
|
||||
content: s.contentJson,
|
||||
updatedAt: s.updatedAt,
|
||||
})),
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
export default contentRoute;
|
||||
Reference in New Issue
Block a user