2019-12-14 03:31:09 +01:00
|
|
|
module main
|
|
|
|
|
2020-04-26 13:49:31 +02:00
|
|
|
import vweb
|
|
|
|
import time
|
2020-06-30 21:04:00 +02:00
|
|
|
import sqlite
|
2020-04-26 13:49:31 +02:00
|
|
|
import json
|
2019-12-14 03:31:09 +01:00
|
|
|
|
2020-06-30 21:04:00 +02:00
|
|
|
struct App {
|
2020-12-31 17:47:20 +01:00
|
|
|
vweb.Context
|
2021-05-11 12:59:02 +02:00
|
|
|
pub mut:
|
2021-08-03 15:03:16 +02:00
|
|
|
db sqlite.DB
|
2021-04-15 05:27:24 +02:00
|
|
|
user_id string
|
2019-12-14 03:31:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-08-03 15:03:16 +02:00
|
|
|
mut app := App{
|
|
|
|
db: sqlite.connect('blog.db') or { panic(err) }
|
|
|
|
}
|
|
|
|
sql app.db {
|
|
|
|
create table Article
|
|
|
|
}
|
2021-07-21 16:33:16 +02:00
|
|
|
vweb.run(app, 8081)
|
2019-12-14 03:31:09 +01:00
|
|
|
}
|
|
|
|
|
2020-07-04 18:56:18 +02:00
|
|
|
/*
|
2020-07-07 22:01:18 +02:00
|
|
|
pub fn (mut app App) index_text() vweb.Result {
|
2019-12-14 03:31:09 +01:00
|
|
|
app.vweb.text('Hello, world from vweb!')
|
2020-07-04 18:56:18 +02:00
|
|
|
return vweb.Result{}
|
2019-12-14 03:31:09 +01:00
|
|
|
}
|
|
|
|
|
2020-07-07 22:01:18 +02:00
|
|
|
pub fn (app &App) index_html() vweb.Result {
|
2020-07-04 18:56:18 +02:00
|
|
|
message := 'Hello, world from Vweb!'
|
|
|
|
return $vweb.html()
|
2019-12-14 03:31:09 +01:00
|
|
|
}
|
|
|
|
*/
|
2021-07-21 16:33:16 +02:00
|
|
|
['/index']
|
2020-07-07 22:01:18 +02:00
|
|
|
pub fn (app &App) index() vweb.Result {
|
2019-12-14 03:31:09 +01:00
|
|
|
articles := app.find_all_articles()
|
2020-06-30 21:04:00 +02:00
|
|
|
return $vweb.html()
|
2019-12-14 03:31:09 +01:00
|
|
|
}
|
|
|
|
|
2021-04-15 05:27:24 +02:00
|
|
|
pub fn (mut app App) before_request() {
|
|
|
|
app.user_id = app.get_cookie('id') or { '0' }
|
2019-12-14 22:55:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-21 16:33:16 +02:00
|
|
|
['/new']
|
2020-07-04 11:38:47 +02:00
|
|
|
pub fn (mut app App) new() vweb.Result {
|
2020-06-30 21:04:00 +02:00
|
|
|
return $vweb.html()
|
2020-01-15 22:17:40 +01:00
|
|
|
}
|
|
|
|
|
2021-03-11 14:20:58 +01:00
|
|
|
['/new_article'; post]
|
2020-06-30 21:04:00 +02:00
|
|
|
pub fn (mut app App) new_article() vweb.Result {
|
2020-12-31 17:47:20 +01:00
|
|
|
title := app.form['title']
|
|
|
|
text := app.form['text']
|
2020-07-04 11:38:47 +02:00
|
|
|
if title == '' || text == '' {
|
2021-01-08 04:49:13 +01:00
|
|
|
return app.text('Empty text/title')
|
2019-12-14 22:55:05 +01:00
|
|
|
}
|
2020-10-14 23:39:09 +02:00
|
|
|
article := Article{
|
2019-12-14 22:55:05 +01:00
|
|
|
title: title
|
|
|
|
text: text
|
|
|
|
}
|
2021-01-01 20:38:22 +01:00
|
|
|
println('posting article')
|
2019-12-14 22:55:05 +01:00
|
|
|
println(article)
|
2020-06-30 21:04:00 +02:00
|
|
|
sql app.db {
|
|
|
|
insert article into Article
|
|
|
|
}
|
2021-04-27 14:28:57 +02:00
|
|
|
|
2020-12-31 17:47:20 +01:00
|
|
|
return app.redirect('/')
|
2019-12-14 22:55:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-21 16:33:16 +02:00
|
|
|
['/articles'; get]
|
|
|
|
pub fn (mut app App) articles() vweb.Result {
|
2019-12-14 22:55:05 +01:00
|
|
|
articles := app.find_all_articles()
|
2021-07-22 10:02:17 +02:00
|
|
|
json_result := json.encode(articles)
|
|
|
|
return app.json(json_result)
|
2019-12-14 22:55:05 +01:00
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut app App) time() {
|
2020-12-31 17:47:20 +01:00
|
|
|
app.text(time.now().format())
|
2019-12-14 03:31:09 +01:00
|
|
|
}
|