diff --git a/tutorials/code/blog/article.v b/tutorials/code/blog/article.v index 42acf00709..faeaf3185e 100644 --- a/tutorials/code/blog/article.v +++ b/tutorials/code/blog/article.v @@ -7,9 +7,9 @@ struct Article { } pub fn (app &App) find_all_articles() []Article { - db := app.db - articles := db.select from Article - return articles + return sql app.db { + select from Article + } } diff --git a/tutorials/code/blog/blog.v b/tutorials/code/blog/blog.v index 2fdb1e72f7..a31273473c 100644 --- a/tutorials/code/blog/blog.v +++ b/tutorials/code/blog/blog.v @@ -2,17 +2,17 @@ module main import vweb import time -import pg +import sqlite import json -pub struct App { -mut: +struct App { +pub mut: vweb vweb.Context - db pg.DB + db sqlite.DB } fn main() { - vweb.run(8080) + vweb.run(8081) } fn (mut app App) index_text() { @@ -26,42 +26,39 @@ fn (app &App) index_html() { } */ -fn (app &App) index() { +fn (app &App) index() vweb.Result { articles := app.find_all_articles() - $vweb.html() + return $vweb.html() } -pub fn (mut app App) init() { - db := pg.connect(pg.Config{ - host: '127.0.0.1' - dbname: 'blog' - user: 'alex' - }) or { panic(err) } +pub fn (mut app App) init_once() { + db := sqlite.connect(':memory:') or { panic(err) } app.db = db } -pub fn (mut app App) new() { - $vweb.html() +pub fn (mut app App) init() { } -pub fn (mut app App) reset() { +pub fn (mut app App) new() vweb.Result{ + return $vweb.html() } -pub fn (mut app App) new_article() { +pub fn (mut app App) new_article() vweb.Result { title := app.vweb.form['title'] text := app.vweb.form['text'] if title == '' || text == '' { app.vweb.text('Empty text/titile') - return + return vweb.Result{} } article := Article{ title: title text: text } println(article) - db := app.db - db.insert(article) - app.vweb.redirect('/article/') + sql app.db { + insert article into Article + } + return app.vweb.redirect('/article/') } pub fn (mut app App) articles() { diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index bff260c56b..e1be618af6 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -92,10 +92,11 @@ pub fn (mut ctx Context) ok(s string) Result { return Result{} } -pub fn (mut ctx Context) redirect(url string) { - if ctx.done { return } +pub fn (mut ctx Context) redirect(url string) Result { + if ctx.done { return Result{} } ctx.done = true - ctx.conn.send_string('HTTP/1.1 302 Found\r\nLocation: ${url}${ctx.headers}\r\n${headers_close}') or { return } + ctx.conn.send_string('HTTP/1.1 302 Found\r\nLocation: ${url}${ctx.headers}\r\n${headers_close}') or { return Result{} } + return Result{} } pub fn (mut ctx Context) not_found() Result {