From 7fe7ddcfac3f5d73d758a1501e57b4ca03181013 Mon Sep 17 00:00:00 2001 From: James Shiffer Date: Sun, 21 Jun 2020 17:22:09 -0700 Subject: [PATCH] [wip] category selector for article composer --- src/routes/cms/create.svelte | 13 ++++++++++++- src/server.js | 24 ++++++++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/routes/cms/create.svelte b/src/routes/cms/create.svelte index 7516faf..806bf13 100644 --- a/src/routes/cms/create.svelte +++ b/src/routes/cms/create.svelte @@ -16,7 +16,7 @@ const { session } = stores(); let editor; - let title = '', image = ''; + let title = '', image = '', category = ''; let actions = [ { @@ -26,6 +26,7 @@ result: function save() { window.localStorage['title'] = title; window.localStorage['image'] = image; + window.localStorage['category'] = category; window.localStorage['html'] = editor.getHtml(true); alert('Successfully saved draft to browser local storage'); } @@ -53,6 +54,7 @@ onMount(function load() { title = window.localStorage['title'] || ''; image = window.localStorage['image'] || ''; + category = window.localStorage['category'] || ''; editor.setHtml(window.localStorage['html'] || '', false); }); @@ -88,6 +90,15 @@

Article Title:

Article Author: {$session.user.realname}

+

Article Category:

Article Header Image URL:

diff --git a/src/server.js b/src/server.js index 1aca7c3..6e618a2 100644 --- a/src/server.js +++ b/src/server.js @@ -205,16 +205,25 @@ express() isAuthor, async function(req, res, next) { try { - const { html, title, image } = req.body; - if (!title || !image || !html) { + const { html, title, image, category } = req.body; + if (!title || !image || !html || !category) { res.writeHead(422, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ - message: `You must supply an article title, image URL, and content.` + message: `You must supply an article title, image URL, category, and content.` })); } - const article = await new Article({ html, title, image, author: req.user._id }); + const cat = await Category.findOne({ name: category }); + if (!cat) { + res.writeHead(404, { + 'Content-Type': 'application/json' + }); + res.end(JSON.stringify({ + message: `That category does not exist.` + })); + } + const article = await new Article({ html, title, image, category: cat, author: req.user._id }); await article.save(); res.writeHead(200, { 'Content-Type': 'application/json' @@ -288,7 +297,14 @@ express() 'Content-Type': 'application/json' }); res.end(JSON.stringify(articles)); + }) + .get('/c', async function (req, res, next) { + let categories = await Category.find(); + res.writeHead(200, { + 'Content-Type': 'application/json' + }); + res.end(JSON.stringify(categories)); }) .get('/me', function(req, res, next) {