diff --git a/.gitignore b/.gitignore index 568c08d..a146ffa 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,6 @@ node_modules/ coverage/ screenshots/* docs/ -.vscode/ data/ coverage/ diff --git a/index.js b/index.js index 203637e..9fdf5f5 100644 --- a/index.js +++ b/index.js @@ -37,16 +37,14 @@ const dbName = 'database.db' router.get('/', async ctx => { try { const article = await new Article(dbName) - let data = await article.getReleased() + const data = await article.search(ctx.query) + const numOfResults = data.length if(ctx.session.authorised === true) { + const loggedIn = true const user = await new User(dbName) const userData = await user.getUser(ctx.session.user) - await ctx.render('homepage', {articles: data, user: userData}) - } else await ctx.render('homepage', {articles: data}) - await ctx.render('homepage', {articles: data}) - data = await article.search(ctx.query) - const numOfResults = data.length - await ctx.render('homepage', {articles: data, query: ctx.query.q, num: numOfResults}) + await ctx.render('homepage', {articles: data, user: userData, query: ctx.query.q, num: numOfResults, loggedIn: loggedIn}) + } else await ctx.render('homepage', {articles: data, query: ctx.query.q, num: numOfResults}) } catch(err) { console.error(err.message) await ctx.render('error', {message: err.message}) @@ -58,7 +56,9 @@ router.get('/articles/:id', async ctx => { const id = ctx.params.id const article = await new Article(dbName) const data = await article.get(id) - await ctx.render('articlepage', data) + const loggedIn = true + if(ctx.session.authorised) await ctx.render('articlepage', {article: data, loggedIn: loggedIn}) + else await ctx.render('articlepage', data) } catch(err) { ctx.body = err.message console.log(err) @@ -200,7 +200,7 @@ router.post('/upload', koaBody, async ctx => { const body = ctx.request.body console.log(body) const article = await new Article(dbName) - const id = await article.upload(body.title, body.summary, body.content, body.tag) + const id = await article.upload(body.title, body.summary, body.content, body.tag, ctx.session.user) console.log(id) const {path, type} = ctx.request.files.image const image = await new Image(dbName) diff --git a/modules/article.js b/modules/article.js index 2de0767..a8ef2b4 100644 --- a/modules/article.js +++ b/modules/article.js @@ -23,15 +23,15 @@ class Article { })() } - async upload(title, summary, content, tag) { + async upload(title, summary, content, tag, username) { try{ if(title === null) throw new Error('Please enter a title for your article') else if(summary === null) throw new Error('Please enter a summary for your article') else if(content === null) throw new Error('Where is the content? Please Enter some text in the content box') const date = await new Date() const fullDate = `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}` - let sql = `INSERT INTO Articles(title, summary, content, written_date, tag, released) - VALUES("${title}", "${summary}", "${content}", "${fullDate}", "${tag}", 0)` + let sql = `INSERT INTO Articles(title, summary, content, written_date, tag, released, author) + VALUES("${title}", "${summary}", "${content}", "${fullDate}", "${tag}", 0, "${username}")` await this.db.run(sql) sql = 'SELECT last_insert_rowid() as id' const result = await this.db.get(sql) diff --git a/views/articlepage.handlebars b/views/articlepage.handlebars index 5075a00..437f82c 100644 --- a/views/articlepage.handlebars +++ b/views/articlepage.handlebars @@ -11,7 +11,7 @@
-

{{title}}

+

{{article.title}}

-
{{written_date}}
- -

{{summary}}

+
{{article.written_date}}
+ +

{{article.summary}}



-

{{content}}

-
by {{author}}
-
tag: {{tag}}
+

{{article.content}}

+
by {{article.author}}
+
tag: {{article.tag}}

rating: @@ -48,7 +48,7 @@

- +
- UPLOAD NEWS + {{#if loggedIn}} UPLOAD NEWS {{/if}} Logout Login Account