From 9f2b80a92415a6040a0428b454f98ad24dd84328 Mon Sep 17 00:00:00 2001 From: James Shiffer <2191476+scoliono@users.noreply.github.com> Date: Mon, 22 Jun 2020 11:59:54 -0700 Subject: [PATCH] completed categories feature --- src/models/category.js | 2 +- src/routes/c/[slug].json.js | 12 ++++--- src/routes/c/[slug].svelte | 70 ++++++++++++++++++++++++++++++++++++ src/routes/cms/create.svelte | 3 +- src/routes/index.svelte | 48 +++---------------------- src/server.js | 2 +- static/global.css | 37 +++++++++++++++++++ 7 files changed, 123 insertions(+), 51 deletions(-) diff --git a/src/models/category.js b/src/models/category.js index fbec101..05f5f77 100644 --- a/src/models/category.js +++ b/src/models/category.js @@ -3,7 +3,7 @@ import mongoose from 'mongoose'; const { Schema } = mongoose; const CategorySchema = new Schema({ name: { type: String, required: true, index: { unique: true } }, - slug: { type: String, required: true, index: { unique: true } } + slug: { type: String, index: { unique: true } } }); diff --git a/src/routes/c/[slug].json.js b/src/routes/c/[slug].json.js index 2cef4c2..1b74acd 100644 --- a/src/routes/c/[slug].json.js +++ b/src/routes/c/[slug].json.js @@ -4,11 +4,11 @@ import Category from '../../models/category.js'; export async function get(req, res) { let { slug } = req.params; - let articles; + let articles, cat; if (slug === 'all') { - articles = await Article.find().sort({ created_at: 'desc' }); + articles = await Article.find().sort({ created_at: 'desc' }).populate({ path: 'category' }); } else { - let cat = await Category.findOne({ slug }); + cat = await Category.findOne({ slug }); if (!cat) { res.writeHead(404, { 'Content-Type': 'application/json' @@ -18,11 +18,13 @@ export async function get(req, res) })); return; } else { - articles = await Article.find({ category: cat.id }).sort({ created_at: 'desc' }); + articles = await Article.find({ category: cat.id }) + .sort({ created_at: 'desc' }) + .populate({ path: 'category' }); } } res.writeHead(200, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify(articles)); + res.end(JSON.stringify({ category: cat, articles })); } diff --git a/src/routes/c/[slug].svelte b/src/routes/c/[slug].svelte index e69de29..88cfa8f 100644 --- a/src/routes/c/[slug].svelte +++ b/src/routes/c/[slug].svelte @@ -0,0 +1,70 @@ + + + + {category.name} Articles | HOWFEED.BIZ + + + + + + +
+
+

{category.name}

+
+ {#each articles as {title, slug, image, created_at}} + +
+ {title} +
+ +
+ {:else} +

No articles are in this category :(

+

Check back soon!

+ {/each} +
+
diff --git a/src/routes/cms/create.svelte b/src/routes/cms/create.svelte index a0d9914..42eb0a7 100644 --- a/src/routes/cms/create.svelte +++ b/src/routes/cms/create.svelte @@ -70,7 +70,7 @@ 'Accept': 'application/json', 'Content-Type': 'application/json' }, - body: JSON.stringify({ html, image, title }) + body: JSON.stringify({ html, image, title, category }) }); const json = await res.json(); goto(`/a/${json.slug}`); @@ -89,6 +89,7 @@ body: JSON.stringify({ name }) }); categories = await res.json(); + category = categories.filter(c => c.name === name)[0].slug; } } diff --git a/src/routes/index.svelte b/src/routes/index.svelte index a32b765..60d1417 100644 --- a/src/routes/index.svelte +++ b/src/routes/index.svelte @@ -2,7 +2,7 @@ export async function preload() { const res = await this.fetch(`/c/all.json`); - const articles = await res.json(); + const { articles } = await res.json(); return { articles }; } @@ -19,22 +19,7 @@ h1, h2, p { margin: 0 auto; } - h1 { - font-size: 3rem; - text-transform: uppercase; - } - figure { - margin: 0; - } - img { - object-fit: contain; - max-width: 100%; - margin: 1rem; - } @media (min-width: 800px) { - div.homepage { - padding-top: 5rem !important; - } h1.welcome { font-size: 8rem !important; } @@ -50,42 +35,19 @@ font-size: 3.5rem !important; } } - div.background { - background: url('/cityscape.jpg') no-repeat center; - background-size: cover; - position: fixed; - height: 24rem; - width: 100%; - z-index: 0; - filter: blur(5px); - } - div.homepage { - padding-top: 8rem; - padding-bottom: 4rem; - position: absolute; - z-index: 1; - margin: 0 auto; - width: 100%; - } h1.welcome, h2.desc { color: whitesmoke; } h1.welcome { margin-top: 1rem; font-size: 3.75rem; - } - h2 { + font-size: 3rem; text-transform: uppercase; } h2.desc { margin-bottom: 1rem; font-size: 1.5rem; - } - div.article-list { - box-shadow: 0 0 5px #000; - } - div.article-meta { - font-weight: bold; + text-transform: uppercase; } @@ -94,7 +56,7 @@
-
+

Welcome

Find an Article

@@ -110,7 +72,7 @@ {:else}

No articles have been published yet :(

-

Come back soon!

+

Chcek back soon!

{/each}
diff --git a/src/server.js b/src/server.js index 264cfb9..1961d03 100644 --- a/src/server.js +++ b/src/server.js @@ -257,10 +257,10 @@ express() } const cat = await new Category({ name }); await cat.save(); + const categories = await Category.find(); res.writeHead(200, { 'Content-Type': 'application/json' }); - const categories = await Category.find(); res.end(JSON.stringify(categories)); } catch (err) { res.writeHead(500, { diff --git a/static/global.css b/static/global.css index d624332..6ef090f 100644 --- a/static/global.css +++ b/static/global.css @@ -80,10 +80,14 @@ figure.article-image { align-self: center; justify-content: center; width: 33.33%; + margin: 0; } figure.article-image img { height: 6rem; width: auto; + object-fit: contain; + max-width: 100%; + margin: 1rem; } div.article-meta { margin: 1rem; @@ -95,9 +99,11 @@ div.article-meta { } .article-title { font-size: 2rem; + margin: 0 auto; } .article-date { font-size: 1rem; + margin: 0 auto; } .content { @@ -105,3 +111,34 @@ div.article-meta { margin: 8rem 0; padding: 2rem; } + +div.background { + background: url('/cityscape.jpg') no-repeat center; + background-size: cover; + position: fixed; + height: 24rem; + width: 100%; + z-index: 0; + filter: blur(5px); +} + +div.article-list { + box-shadow: 0 0 5px #000; +} +div.article-meta { + font-weight: bold; +} + +div.floaty { + padding-top: 8rem; + padding-bottom: 4rem; + position: absolute; + z-index: 1; + margin: 0 auto; + width: 100%; +} +@media (min-width: 800px) { + div.floaty { + padding-top: 5rem !important; + } +}