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

tutorials: web blog fixes

This commit is contained in:
Alexander Medvednikov 2020-06-30 21:04:00 +02:00
parent 7df6984261
commit 34ddc9240e
3 changed files with 25 additions and 27 deletions

View File

@ -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
}
}

View File

@ -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<App>(8080)
vweb.run<App>(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() {

View File

@ -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 {