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

75 lines
1.1 KiB
V
Raw Normal View History

2019-12-14 05:31:09 +03:00
module main
2020-04-26 14:49:31 +03:00
import vweb
import time
import pg
import json
2019-12-14 05:31:09 +03:00
pub struct App {
2019-12-14 05:31:09 +03:00
mut:
vweb vweb.Context
db pg.DB
}
fn main() {
2019-12-21 05:25:28 +03:00
vweb.run<App>(8080)
2019-12-14 05:31:09 +03:00
}
2020-05-17 14:51:18 +03:00
fn (mut app App) index_text() {
2019-12-14 05:31:09 +03:00
app.vweb.text('Hello, world from vweb!')
}
/*
fn (app &App) index_html() {
message := 'Hello, world from vweb!'
$vweb.html()
}
*/
fn (app &App) index() {
articles := app.find_all_articles()
$vweb.html()
}
2020-05-17 14:51:18 +03:00
pub fn (mut app App) init() {
2019-12-14 05:31:09 +03:00
db := pg.connect(pg.Config{
host: '127.0.0.1'
dbname: 'blog'
user: 'alex'
}) or { panic(err) }
app.db = db
}
2020-05-17 14:51:18 +03:00
pub fn (mut app App) new() {
2019-12-15 00:55:05 +03:00
$vweb.html()
}
2020-05-17 14:51:18 +03:00
pub fn (mut app App) reset() {
}
2020-05-17 14:51:18 +03:00
pub fn (mut app App) new_article() {
2019-12-15 00:55:05 +03:00
title := app.vweb.form['title']
text := app.vweb.form['text']
if title == '' || text == '' {
app.vweb.text('Empty text/titile')
return
}
article := Article{
title: title
text: text
}
println(article)
db := app.db
db.insert(article)
app.vweb.redirect('/article/')
}
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()
app.vweb.json(json.encode(articles))
}
2020-05-17 14:51:18 +03:00
fn (mut app App) time() {
2019-12-14 05:31:09 +03:00
app.vweb.text(time.now().format())
}