setup mongodb

This commit is contained in:
James Shiffer 2020-06-09 01:01:18 -07:00
parent 519246973c
commit 9d1a95aa0a
7 changed files with 31 additions and 23 deletions

View File

@ -9,17 +9,26 @@ const ArticleSchema = new Schema({
},
slug: { type: String, required: true, index: { unique: true } },
created_at: { type: Date, default: Date.now },
html: { type: Blob, required: true },
html: { type: String, required: true },
comments: [{
content: { type: String, required: true },
author: { type: String, required: true },
created_at: { type: Date, default: Date.now },
votes: { type: Number, default: 0 }
}],
views: { type: Number, default: 0 },
votes: { type: Number, default: 0 }
});
ArticleSchema.methods.genSlug = () => this.title.toLowerCase().replace(/\W+/g, '-');
ArticleSchema.pre('findOne', next => {
var article = this;
article.views++;
next();
});
ArticleSchema.pre('save', next => {
var article = this;
// only gen the slug if title has been modified (or is new)
@ -29,6 +38,4 @@ ArticleSchema.pre('save', next => {
next();
});
ArticleSchema.methods.genSlug = () => this.title.toLowerCase().replace(/\W+/g, '-');
export default mongoose.model('Article', ArticleSchema);

View File

@ -0,0 +1,18 @@
<script context="module">
export async function preload(page, session)
{
if (!session.user) {
return this.redirect(302, '/cms/login');
}
return { user: session.user };
}
</script>
<div class="content">
<h1>HowFeed Publisher</h1>
<form method="POST" action="/cms/article">
<textarea name="content"></textarea>
<button type="submit">Submit</button>
</form>
</div>

View File

@ -1,7 +1,3 @@
<script>
let username, password;
</script>
<style>
input {
display: block;
@ -26,8 +22,8 @@
<div class="content">
<h1>Login</h1>
<form method="POST" action="/cms/login">
<input required type="text" name="username" bind:value={username} placeholder="Username">
<input required type="password" name="password" bind:value={password} placeholder="Password">
<input required type="text" name="username" placeholder="Username">
<input required type="password" name="password" placeholder="Password">
<button type="submit">Submit</button>
</form>
</div>

View File

@ -84,5 +84,5 @@ express()
.listen(PORT, err => {
if (err) console.log('error', err);
console.log('Express server listening');
console.log(`Express server listening on port ${PORT}`);
});

1
storage/.gitignore vendored
View File

@ -1 +0,0 @@
*.db

View File

@ -1,12 +0,0 @@
PRAGMA foreign_keys = ON;
CREATE TABLE IF NOT EXISTS users (
username TEXT PRIMARY KEY,
password TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS articles (
title TEXT PRIMARY KEY,
created_at TEXT NOT NULL,
html BLOB NOT NULL,
author INTEGER NOT NULL,
FOREIGN KEY (author) REFERENCES users (username)
);