From 27e7c8f17ba6bc7dff9b0d9b1d2136d12ea09601 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Thu, 11 Jul 2024 20:27:55 -0400 Subject: [PATCH] Add some development, services repository. --- posts/contract-development.md | 1 + src/services/post-file.ts | 12 +++++++++--- src/services/post-repository.ts | 4 ++++ 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 src/services/post-repository.ts diff --git a/posts/contract-development.md b/posts/contract-development.md index 68965b3..5f4eb44 100644 --- a/posts/contract-development.md +++ b/posts/contract-development.md @@ -5,6 +5,7 @@ date: 2023-04-09 tags: - development - php +slug: contract-development --- I have been mulling in my head for a while now about how I wanted to talk about this very common design pattern at work. [Designing by contract](https://en.wikipedia.org/wiki/Design_by_contract) is a very old idea in software and I have found myself having to explain it and teach it other developers at work. So hopefully in this post, I can elaborate on why using a contract is beneficial in the design and development of large applications. diff --git a/src/services/post-file.ts b/src/services/post-file.ts index 6e376b4..1a10f77 100644 --- a/src/services/post-file.ts +++ b/src/services/post-file.ts @@ -4,12 +4,18 @@ import type { Post } from '@blog/models/Post'; import * as yamlFront from 'yaml-front-matter'; import { marked } from 'marked'; -export async function readPostFile(fileName: string): Promise { - const file = Bun.file(POST_PATH + '/' + fileName); +export async function readPostFile(path: string): Promise { + const file = Bun.file(path); const fileContent = await file.text(); const parsedData = yamlFront.loadFront(fileContent); const postHtml = await marked.parse(parsedData.__content); + let slug = parsedData.slug; + + if (!slug) { + slug = path.split('/').pop().slice(0, -3); + } + return { meta: { title: parsedData.title, @@ -17,7 +23,7 @@ export async function readPostFile(fileName: string): Promise { date: new Date(parsedData.date), draft: parsedData?.draft || false, tags: parsedData.tags, - slug: POST_ROUTE_PREFIX + '/' + fileName, + slug: slug }, content: parsedData.__content, html: postHtml diff --git a/src/services/post-repository.ts b/src/services/post-repository.ts new file mode 100644 index 0000000..8099153 --- /dev/null +++ b/src/services/post-repository.ts @@ -0,0 +1,4 @@ + +export class PostRepository { + +}