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

View File

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

View File

@ -1,12 +1,13 @@
import { createFactory } from 'hono/factory'; 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(); const factory = createFactory();
let postFileService: PostFileService; let postFileService: PostService;
export const postFileMiddleware = factory.createMiddleware(async (c, next) => { export const postFileMiddleware = factory.createMiddleware(async (c, next) => {
if (!postFileService) { if (!postFileService) {
postFileService = await PostFileService.create(); postFileService = await createPostService(POST_PATH);
} }
c.set('postService', postFileService); c.set('postService', postFileService);

View File

@ -47,26 +47,26 @@ export async function readPostFile(path: string): Promise<Post> {
}; };
} }
export class PostFileService { export async function createPostService(path: string): Promise<PostService> {
const posts = new Map<string, Post>();
const postFiles: string[] = await readdir(path);
for (const postFile of postFiles) {
const post = await readPostFile(POST_PATH + "/" + postFile);
const key = post.meta?.slug || postFile.slice(0, -3);
posts.set(key, post);
}
return new PostService(posts);
}
export class PostService {
private posts: Map<string, Post>; private posts: Map<string, Post>;
public constructor(posts: Map<string, Post>) { public constructor(posts: Map<string, Post>) {
this.posts = posts; this.posts = posts;
} }
public static async create() {
const posts = new Map<string, Post>();
const postFiles: string[] = await readdir(POST_PATH);
for (const postFile of postFiles) {
const post = await readPostFile(POST_PATH + "/" + postFile);
const key = post.meta?.slug || postFile.slice(0, -3);
posts.set(key, post);
}
return new PostFileService(posts);
}
public getPosts(): Map<string, Post> { public getPosts(): Map<string, Post> {
return this.posts; return this.posts;
} }