Server Functions for getting services
This commit is contained in:
parent
e6203baf9e
commit
1a75df9851
|
@ -0,0 +1,85 @@
|
|||
// import type { Category } from '$lib/types/service';
|
||||
// import fs from 'fs';
|
||||
// import path from 'path';
|
||||
|
||||
// export const getCategories = async (): Promise<Category[]> => {
|
||||
// const contentDir = await getContentDir();
|
||||
// const categories: Category[] = [];
|
||||
|
||||
// for (const categoryDir of contentDir.dirs) {
|
||||
// const categoryData = await import(`../content/${categoryDir.name}/category.json`);
|
||||
// const services = await Promise.all(
|
||||
// categoryDir.files
|
||||
// .filter((file) => file.name.endsWith('.md'))
|
||||
// .map(async (file) => {
|
||||
// const { metadata, content } = await getMetadata(file.content);
|
||||
// return {
|
||||
// ...metadata,
|
||||
// content,
|
||||
// };
|
||||
// })
|
||||
// );
|
||||
|
||||
// categories.push({
|
||||
// ...categoryData.default,
|
||||
// services,
|
||||
// });
|
||||
// }
|
||||
|
||||
// return categories;
|
||||
// };
|
||||
|
||||
// // -----------------
|
||||
// const contentPath: string = path.join(process.cwd(), 'src', 'content');
|
||||
|
||||
// const getCategories = async ( contentPath: string ): Promise<Category[]> => {
|
||||
// try {
|
||||
// // read directories - entries - in contentPath
|
||||
// const entries = await fs.promises.readdir( contentPath );
|
||||
|
||||
// const categoryDirs = await Promise.all(
|
||||
// entries.map(async (entry) => {
|
||||
// // /src/content/example-service
|
||||
// const entryPath = path.join(contentPath, entry);
|
||||
// // file statistict
|
||||
// const stats = fs.promises.stat(entryPath);
|
||||
|
||||
// // Ensure stats is an object before accessing properties
|
||||
// if (typeof stats === 'object' && stats !== null && 'isDirectory' in stats) {
|
||||
// return
|
||||
// }
|
||||
|
||||
|
||||
// return
|
||||
// })
|
||||
// return
|
||||
// }
|
||||
|
||||
|
||||
import type { Category, Service } from '$lib/types/service';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
export const getCategories = async (): Promise<Category[]> => {
|
||||
const contentDir = path.join(process.cwd(), 'src', 'content');
|
||||
const categoryDirs = fs.readdirSync(contentDir, { withFileTypes: true })
|
||||
.filter(dirent => dirent.isDirectory())
|
||||
.map(dirent => dirent.name);
|
||||
|
||||
const categories: Category[] = await Promise.all(
|
||||
categoryDirs.map(async categoryId => {
|
||||
const categoryJsonPath = path.join(contentDir, categoryId, `${categoryId}.json`);
|
||||
const categoryData = JSON.parse(fs.readFileSync(categoryJsonPath, 'utf-8'));
|
||||
|
||||
return {
|
||||
...categoryData,
|
||||
services: categoryData.services.map((service: Service) => ({
|
||||
...service,
|
||||
id: `${categoryId}/${service.id}`,
|
||||
})),
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return categories;
|
||||
};
|
|
@ -0,0 +1,62 @@
|
|||
// import type { PageServerLoad } from './$types';
|
||||
// import path from 'path';
|
||||
// import fs from 'fs';
|
||||
// import { error } from '@sveltejs/kit';
|
||||
// import { fetchServiceData } from '$lib/utils/fetchServiceData';
|
||||
|
||||
// export const load: PageServerLoad = async ({ params }) => {
|
||||
// const { categoryId, serviceId } = params;
|
||||
|
||||
// try {
|
||||
// const serviceData = await fetchServiceData(categoryId, serviceId);
|
||||
|
||||
// return {
|
||||
// service: serviceData
|
||||
// };
|
||||
// } catch (err) {
|
||||
// throw error(404, `Service ${serviceId} not found`);
|
||||
// }
|
||||
// };
|
||||
|
||||
// src/routes/[categoryId]/[serviceId]/+page.server.ts
|
||||
import { error } from '@sveltejs/kit';
|
||||
import { getCategories } from '$lib/utils/getCategories';
|
||||
|
||||
export const load = async ({ params }) => {
|
||||
const { categoryId, serviceId } = params;
|
||||
const categories = await getCategories();
|
||||
const category = categories.find(cat => cat.id === categoryId);
|
||||
|
||||
if (!category) {
|
||||
throw error(404, 'Category not found');
|
||||
}
|
||||
|
||||
const service = category.services.find(serv => serv.id === `${categoryId}/${serviceId}`);
|
||||
|
||||
if (!service) {
|
||||
throw error(404, 'Service not found');
|
||||
}
|
||||
// mdsvex
|
||||
const {default: content, metadata: frontmatter} = await import(`$content/${categoryId}/${serviceId}.md`)
|
||||
|
||||
const seoData = {
|
||||
title: service.title, // Assuming title exists in service data
|
||||
description: service.description,
|
||||
id: service.id,
|
||||
image: service.image,
|
||||
price: service.price,
|
||||
duration: service.duration,
|
||||
// ... (populate other optional fields)
|
||||
frontmatter: frontmatter, // Assuming metadata contains date and tags
|
||||
};
|
||||
|
||||
return {
|
||||
category,
|
||||
service: {
|
||||
...service,
|
||||
content,
|
||||
frontmatter
|
||||
},
|
||||
seoData
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue