From 570c607e1282d5528a98dc6c9447f786cbb27f75 Mon Sep 17 00:00:00 2001 From: James Shiffer <2191476+scoliono@users.noreply.github.com> Date: Thu, 11 Jun 2020 16:02:31 -0700 Subject: [PATCH] loads articles correctly, deleting is WIP --- src/models/article.js | 12 +++++++++-- src/routes/a/[slug].json.js | 16 +++++--------- src/routes/a/[slug].svelte | 2 +- src/routes/a/_articles.js | 9 -------- src/routes/cms/create.svelte | 8 +++---- src/routes/cms/delete.svelte | 41 ++++++++++++++++++++++++++++++++++++ src/routes/index.svelte | 9 ++++---- src/server.js | 41 ++++++++++++++++++++++++++++++------ 8 files changed, 100 insertions(+), 38 deletions(-) delete mode 100644 src/routes/a/_articles.js create mode 100644 src/routes/cms/delete.svelte diff --git a/src/models/article.js b/src/models/article.js index 10e6a9b..a6d0aed 100644 --- a/src/models/article.js +++ b/src/models/article.js @@ -7,7 +7,8 @@ const ArticleSchema = new Schema({ type: mongoose.Schema.Types.ObjectId, ref: 'User' }, - slug: { type: String, required: true, index: { unique: true } }, + slug: { type: String, index: { unique: true } }, + image: { type: String, required: true }, created_at: { type: Date, default: Date.now }, html: { type: String, required: true }, comments: [{ @@ -21,7 +22,14 @@ const ArticleSchema = new Schema({ }); -ArticleSchema.methods.genSlug = () => this.title.toLowerCase().replace(/\W+/g, '-'); +ArticleSchema.virtual('author_user', { + ref: 'User', + localField: 'author', + foreignField: '_id', + justOne: true +}); + +ArticleSchema.methods.genSlug = title => title.toLowerCase().replace(/\W+/g, '-'); ArticleSchema.pre('findOne', function (next) { var article = this; diff --git a/src/routes/a/[slug].json.js b/src/routes/a/[slug].json.js index cc36288..d795bdf 100644 --- a/src/routes/a/[slug].json.js +++ b/src/routes/a/[slug].json.js @@ -1,26 +1,20 @@ -import articles from './_articles.js'; +import Article from '../../models/article.js'; -const lookup = new Map(); -articles.forEach(article => { - lookup.set(article.slug, JSON.stringify(article)); -}); - -export function get(req, res, next) { +export async function get(req, res, next) { // the `slug` parameter is available because // this file is called [slug].json.js const { slug } = req.params; + const article = await Article.findOne({ slug }).populate('author_user'); - if (lookup.has(slug)) { + if (article) { res.writeHead(200, { 'Content-Type': 'application/json' }); - - res.end(lookup.get(slug)); + res.end(JSON.stringify(article)); } else { res.writeHead(404, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ message: `Not found` })); diff --git a/src/routes/a/[slug].svelte b/src/routes/a/[slug].svelte index 31b3357..c3d3b68 100644 --- a/src/routes/a/[slug].svelte +++ b/src/routes/a/[slug].svelte @@ -75,7 +75,7 @@
- {article.title} + {article.title}

{article.title}

diff --git a/src/routes/a/_articles.js b/src/routes/a/_articles.js deleted file mode 100644 index 21ca03c..0000000 --- a/src/routes/a/_articles.js +++ /dev/null @@ -1,9 +0,0 @@ -const articles = [ -]; - -articles.forEach(article => { - article.slug = article.title.toLowerCase().replace(/\W+/g, '-'); - article.html = article.html.replace(/^\t{3}/gm, ''); -}); - -export default articles; diff --git a/src/routes/cms/create.svelte b/src/routes/cms/create.svelte index 4103d2e..3a21f2d 100644 --- a/src/routes/cms/create.svelte +++ b/src/routes/cms/create.svelte @@ -1,8 +1,8 @@ + + + Delete Article | HOWFEED.BIZ + + + + +
+ < Back to Dashboard +

Delete an Article

+
    + {#each articles as article} +
  • + {article.title} by {article.author_user} + +
  • + {:else} +
  • There are no published articles.
  • + {/each} +
+
diff --git a/src/routes/index.svelte b/src/routes/index.svelte index ad9dc5f..0883500 100644 --- a/src/routes/index.svelte +++ b/src/routes/index.svelte @@ -1,7 +1,8 @@ @@ -94,14 +95,14 @@

Welcome

Find an Article

- {#each articles as {title, slug, date}} + {#each articles as {title, slug, image, created_at}}
- {title} + {title}
{:else} diff --git a/src/server.js b/src/server.js index 51c08ec..75b3947 100644 --- a/src/server.js +++ b/src/server.js @@ -199,16 +199,43 @@ express() })); } }, - function(req, res, next) { - res.writeHead(200, { - 'Content-Type': 'application/json' - }); - res.end(JSON.stringify({ - message: `ur a faget lol` - })); + async function(req, res, next) { + try { + const { html, title, image } = req.body; + if (!title || !image || !html) { + res.writeHead(422, { + 'Content-Type': 'application/json' + }); + res.end(JSON.stringify({ + message: `You must supply an article title, image URL, and content.` + })); + } + const article = await new Article({ html, title, image, author: req.user._id }); + await article.save(); + res.redirect(`/a/${article.slug}`); + } catch (err) { + res.writeHead(500, { + 'Content-Type': 'application/json' + }); + res.end(JSON.stringify({ + message: `Failed to add article: ${err}` + })); + } } ) + .get('/a/all', async function (req, res, next) { + let articles = await Article.find().populate('author_user'); + articles.forEach(article => { + article.slug = article.title.toLowerCase().replace(/\W+/g, '-'); + article.html = article.html.replace(/^\t{3}/gm, ''); + }); + res.writeHead(200, { + 'Content-Type': 'application/json' + }); + res.end(JSON.stringify(articles)); + }) + .use(compression({ threshold: 0 })) .use(sirv('static', { dev })) .use(sapper.middleware({