// message.repository.ts
import {readFile, writeFile} from 'fs/promises'
// readFile, writeFile은 파일시스템에 접근하기 위해 import 한다.
export class MessagesRepository {
async findOne(id : string) {
const contents = await readFile("messages.json", 'utf-8')
const messages = JSON.parse(contents)
return messages[id]
}
async findAll() {
const contents = await readFile("messages.json", 'utf-8')
const messages = JSON.parse(contents)
return messages
}
async create(content : string) {
const contents = await readFile("messages.json", 'utf-8')
const messages = JSON.parse(contents)
const id = Math.floor(Math.random() * 999)
messages[id] = {id, content}
await writeFile('messages.json', JSON.stringify(messages))
}
}