API sprejme publishedAt pri uvozu, da uvozeni clanki ne nosijo danasnjega datuma
This commit is contained in:
parent
78ef1848dc
commit
0b7cf9e5f5
@ -268,6 +268,12 @@ export async function registerRoutes(
|
|||||||
res.json({ ...article, views: article.views + (isNew ? 1 : 0) });
|
res.json({ ...article, views: article.views + (isNew ? 1 : 0) });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function _parseDate(v: any): Date | null {
|
||||||
|
if (!v) return null;
|
||||||
|
const d = new Date(v);
|
||||||
|
return isNaN(d.getTime()) ? null : d;
|
||||||
|
}
|
||||||
|
|
||||||
app.post("/api/articles", requireApiKey, async (req, res) => {
|
app.post("/api/articles", requireApiKey, async (req, res) => {
|
||||||
const parsed = insertArticleSchema.safeParse(req.body);
|
const parsed = insertArticleSchema.safeParse(req.body);
|
||||||
if (!parsed.success) {
|
if (!parsed.success) {
|
||||||
@ -277,7 +283,12 @@ export async function registerRoutes(
|
|||||||
if (existing) {
|
if (existing) {
|
||||||
return res.status(409).json({ message: `Artikel "${existing.title}" existiert bereits (ID: ${existing.id})` });
|
return res.status(409).json({ message: `Artikel "${existing.title}" existiert bereits (ID: ${existing.id})` });
|
||||||
}
|
}
|
||||||
const article = await storage.createArticle(parsed.data);
|
// publishedAt iz zunanjega vira (uvoz z ONE): shema ga sicer izpusti, zato
|
||||||
|
// ga sprejmemo posebej - drugace bi vsi uvozeni clanki nosili danasnji datum.
|
||||||
|
const data: any = { ...parsed.data };
|
||||||
|
const pub = _parseDate(req.body?.publishedAt);
|
||||||
|
if (pub) data.publishedAt = pub;
|
||||||
|
const article = await storage.createArticle(data);
|
||||||
res.status(201).json(article);
|
res.status(201).json(article);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -287,7 +298,10 @@ export async function registerRoutes(
|
|||||||
if (!partial.success) {
|
if (!partial.success) {
|
||||||
return res.status(400).json({ message: "Invalid article data", errors: partial.error.flatten() });
|
return res.status(400).json({ message: "Invalid article data", errors: partial.error.flatten() });
|
||||||
}
|
}
|
||||||
const article = await storage.updateArticle(id, partial.data);
|
const patch: any = { ...partial.data };
|
||||||
|
const pubP = _parseDate(req.body?.publishedAt);
|
||||||
|
if (pubP) patch.publishedAt = pubP;
|
||||||
|
const article = await storage.updateArticle(id, patch);
|
||||||
if (!article) {
|
if (!article) {
|
||||||
return res.status(404).json({ message: "Article not found" });
|
return res.status(404).json({ message: "Article not found" });
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user