Refactor the post service.

This commit is contained in:
Dave Smith-Hayes 2024-08-01 21:07:54 -04:00
parent d7c16c6763
commit e0adc0e684
4 changed files with 24 additions and 23 deletions

View File

@ -2,16 +2,16 @@ import { Hono } from "hono";
import { Home } from "@blog/templates/Pages/Home";
import { PostMeta } from "@blog/models/PostMeta";
import { Post } from '@blog/models/Post';
import { PostFileService } from '@blog/services/post-file';
import { PostService } from '@blog/services/post-file';
type Posts = {
postService: PostFileService
postService: PostService
};
const app = new Hono<{ Variables: Posts }>();
app.get("/", async (c) => {
const postService: PostFileService = c.get('postService');
const postService: PostService = c.get('postService');
const postList: PostMeta[] = Array.from(postService.getPublishedPosts())
.map((p: Post) => p.meta);

View File

@ -2,17 +2,17 @@ import { Hono } from 'hono';
import { PostPage } from '@blog/templates/Pages/PostPage';
import { FourOhFour } from '@blog/templates/Pages/FourOhFour';
import { SiteMeta } from '@blog/models/SiteMeta';
import { PostFileService } from '@blog/services/post-file';
import { PostService } from '@blog/services/post-file';
type Posts = {
postService: PostFileService
postService: PostService
}
const app = new Hono<{ Variables: Posts }>();
app.get('/:slug', async (c) => {
const postSlug: string = c.req.param("slug");
const postService: PostFileService = c.get('postService');
const postService: PostService = c.get('postService');
try {
const post = postService.getPost(postSlug);

View File

@ -1,12 +1,13 @@
import { createFactory } from 'hono/factory';
import { PostFileService } from '@blog/services/post-file';
import { createPostService, PostService } from '@blog/services/post-file';
import { POST_PATH } from '@blog/config';
const factory = createFactory();
let postFileService: PostFileService;
let postFileService: PostService;
export const postFileMiddleware = factory.createMiddleware(async (c, next) => {
if (!postFileService) {
postFileService = await PostFileService.create();
postFileService = await createPostService(POST_PATH);
}
c.set('postService', postFileService);

View File

@ -47,16 +47,9 @@ export async function readPostFile(path: string): Promise<Post> {
};
}
export class PostFileService {
private posts: Map<string, Post>;
public constructor(posts: Map<string, Post>) {
this.posts = posts;
}
public static async create() {
export async function createPostService(path: string): Promise<PostService> {
const posts = new Map<string, Post>();
const postFiles: string[] = await readdir(POST_PATH);
const postFiles: string[] = await readdir(path);
for (const postFile of postFiles) {
const post = await readPostFile(POST_PATH + "/" + postFile);
@ -64,7 +57,14 @@ export class PostFileService {
posts.set(key, post);
}
return new PostFileService(posts);
return new PostService(posts);
}
export class PostService {
private posts: Map<string, Post>;
public constructor(posts: Map<string, Post>) {
this.posts = posts;
}
public getPosts(): Map<string, Post> {