18 lines
589 B
JavaScript
18 lines
589 B
JavaScript
import { httpError } from '../../shared/http/httpError.mjs';
|
|
import { sendJson } from '../../shared/http/sendJson.mjs';
|
|
import { readArticles, writeArticles } from './articleRepository.mjs';
|
|
|
|
export async function handleDeleteArticle(_request, response, slug) {
|
|
const articles = await readArticles();
|
|
const index = articles.findIndex((article) => article.slug === slug);
|
|
|
|
if (index === -1) {
|
|
throw httpError(404, `Article "${slug}" was not found`);
|
|
}
|
|
|
|
const [article] = articles.splice(index, 1);
|
|
await writeArticles(articles);
|
|
|
|
return sendJson(response, { article });
|
|
}
|