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

72 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
2020-06-30 22:04:00 +03:00
import sqlite
2020-04-26 14:49:31 +03:00
import json
2019-12-14 05:31:09 +03:00
2020-06-30 22:04:00 +03:00
struct App {
pub mut:
2019-12-14 05:31:09 +03:00
vweb vweb.Context
2020-06-30 22:04:00 +03:00
db sqlite.DB
2019-12-14 05:31:09 +03:00
}
fn main() {
2020-06-30 22:04:00 +03:00
vweb.run<App>(8081)
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()
}
*/
2020-06-30 22:04:00 +03:00
fn (app &App) index() vweb.Result {
2019-12-14 05:31:09 +03:00
articles := app.find_all_articles()
2020-06-30 22:04:00 +03:00
return $vweb.html()
2019-12-14 05:31:09 +03:00
}
2020-06-30 22:04:00 +03:00
pub fn (mut app App) init_once() {
db := sqlite.connect(':memory:') or { panic(err) }
2019-12-14 05:31:09 +03:00
app.db = db
}
2020-06-30 22:04:00 +03:00
pub fn (mut app App) init() {
2019-12-15 00:55:05 +03:00
}
2020-06-30 22:04:00 +03:00
pub fn (mut app App) new() vweb.Result{
return $vweb.html()
}
2020-06-30 22:04:00 +03:00
pub fn (mut app App) new_article() vweb.Result {
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')
2020-06-30 22:04:00 +03:00
return vweb.Result{}
2019-12-15 00:55:05 +03:00
}
article := Article{
title: title
text: text
}
println(article)
2020-06-30 22:04:00 +03:00
sql app.db {
insert article into Article
}
return app.vweb.redirect('/article/')
2019-12-15 00:55:05 +03:00
}
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())
}