2019-12-14 05:31:09 +03:00
|
|
|
module main
|
|
|
|
|
2020-04-26 14:49:31 +03:00
|
|
|
import vweb
|
|
|
|
import time
|
2020-06-30 22:04:00 +03:00
|
|
|
import sqlite
|
2020-04-26 14:49:31 +03:00
|
|
|
import json
|
2019-12-14 05:31:09 +03:00
|
|
|
|
2020-06-30 22:04:00 +03:00
|
|
|
struct App {
|
2020-12-31 19:47:20 +03:00
|
|
|
vweb.Context
|
2021-01-01 22:38:22 +03:00
|
|
|
mut:
|
2020-12-31 19:47:20 +03:00
|
|
|
db sqlite.DB
|
2019-12-14 05:31:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2020-06-30 22:04:00 +03:00
|
|
|
vweb.run<App>(8081)
|
2019-12-14 05:31:09 +03:00
|
|
|
}
|
|
|
|
|
2020-07-04 19:56:18 +03:00
|
|
|
/*
|
2020-07-07 23:01:18 +03:00
|
|
|
pub fn (mut app App) index_text() vweb.Result {
|
2019-12-14 05:31:09 +03:00
|
|
|
app.vweb.text('Hello, world from vweb!')
|
2020-07-04 19:56:18 +03:00
|
|
|
return vweb.Result{}
|
2019-12-14 05:31:09 +03:00
|
|
|
}
|
|
|
|
|
2020-07-07 23:01:18 +03:00
|
|
|
pub fn (app &App) index_html() vweb.Result {
|
2020-07-04 19:56:18 +03:00
|
|
|
message := 'Hello, world from Vweb!'
|
|
|
|
return $vweb.html()
|
2019-12-14 05:31:09 +03:00
|
|
|
}
|
|
|
|
*/
|
2020-07-07 23:01:18 +03:00
|
|
|
pub fn (app &App) index() vweb.Result {
|
2019-12-14 05:31:09 +03:00
|
|
|
articles := app.find_all_articles()
|
2020-06-30 22:04:00 +03:00
|
|
|
return $vweb.html()
|
2019-12-14 05:31:09 +03:00
|
|
|
}
|
|
|
|
|
2020-06-30 22:04:00 +03:00
|
|
|
pub fn (mut app App) init_once() {
|
2021-01-01 22:38:22 +03:00
|
|
|
app.db = sqlite.connect('blog.db') or { panic(err) }
|
2021-01-08 06:49:13 +03:00
|
|
|
app.db.exec('create table if not exists article (' + 'id integer primary key, ' + "title text default ''," +
|
|
|
|
"text text default ''" + ');')
|
2019-12-14 05:31:09 +03:00
|
|
|
}
|
|
|
|
|
2020-06-30 22:04:00 +03:00
|
|
|
pub fn (mut app App) init() {
|
2019-12-15 00:55:05 +03:00
|
|
|
}
|
|
|
|
|
2020-07-04 12:38:47 +03:00
|
|
|
pub fn (mut app App) new() vweb.Result {
|
2020-06-30 22:04:00 +03:00
|
|
|
return $vweb.html()
|
2020-01-16 00:17:40 +03:00
|
|
|
}
|
|
|
|
|
2021-01-01 22:38:22 +03:00
|
|
|
[post]
|
|
|
|
['/new_article']
|
2020-06-30 22:04:00 +03:00
|
|
|
pub fn (mut app App) new_article() vweb.Result {
|
2020-12-31 19:47:20 +03:00
|
|
|
title := app.form['title']
|
|
|
|
text := app.form['text']
|
2020-07-04 12:38:47 +03:00
|
|
|
if title == '' || text == '' {
|
2021-01-08 06:49:13 +03:00
|
|
|
return app.text('Empty text/title')
|
2019-12-15 00:55:05 +03:00
|
|
|
}
|
2020-10-15 00:39:09 +03:00
|
|
|
article := Article{
|
2019-12-15 00:55:05 +03:00
|
|
|
title: title
|
|
|
|
text: text
|
|
|
|
}
|
2021-01-01 22:38:22 +03:00
|
|
|
println('posting article')
|
2019-12-15 00:55:05 +03:00
|
|
|
println(article)
|
2020-06-30 22:04:00 +03:00
|
|
|
sql app.db {
|
|
|
|
insert article into Article
|
|
|
|
}
|
2020-12-31 19:47:20 +03:00
|
|
|
return app.redirect('/')
|
2019-12-15 00:55:05 +03:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut app App) articles() {
|
2019-12-15 00:55:05 +03:00
|
|
|
articles := app.find_all_articles()
|
2020-12-31 19:47:20 +03:00
|
|
|
app.json(json.encode(articles))
|
2019-12-15 00:55:05 +03:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut app App) time() {
|
2020-12-31 19:47:20 +03:00
|
|
|
app.text(time.now().format())
|
2019-12-14 05:31:09 +03:00
|
|
|
}
|