1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vweb: more updates

This commit is contained in:
Alexander Medvednikov
2020-12-31 17:47:20 +01:00
parent fa00f157ad
commit 2bc9ee4d88
4 changed files with 17 additions and 23 deletions

View File

@ -6,9 +6,8 @@ import sqlite
import json
struct App {
pub mut:
vweb vweb.Context
db sqlite.DB
vweb.Context
db sqlite.DB
}
fn main() {
@ -32,9 +31,7 @@ pub fn (app &App) index() vweb.Result {
}
pub fn (mut app App) init_once() {
db := sqlite.connect('blog.db') or {
panic(err)
}
db := sqlite.connect('blog.db') or { panic(err) }
app.db = db
}
@ -46,10 +43,10 @@ pub fn (mut app App) new() vweb.Result {
}
pub fn (mut app App) new_article() vweb.Result {
title := app.vweb.form['title']
text := app.vweb.form['text']
title := app.form['title']
text := app.form['text']
if title == '' || text == '' {
app.vweb.text('Empty text/title')
app.text('Empty text/title')
return vweb.Result{}
}
article := Article{
@ -60,14 +57,14 @@ pub fn (mut app App) new_article() vweb.Result {
sql app.db {
insert article into Article
}
return app.vweb.redirect('/')
return app.redirect('/')
}
pub fn (mut app App) articles() {
articles := app.find_all_articles()
app.vweb.json(json.encode(articles))
app.json(json.encode(articles))
}
fn (mut app App) time() {
app.vweb.text(time.now().format())
app.text(time.now().format())
}