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