mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
tutorial: minor fixes + move code and images to a separate dir
This commit is contained in:
17
tutorials/code/blog/article.v
Normal file
17
tutorials/code/blog/article.v
Normal file
@ -0,0 +1,17 @@
|
||||
module main
|
||||
|
||||
struct Article {
|
||||
id int
|
||||
title string
|
||||
text string
|
||||
}
|
||||
|
||||
pub fn (app &App) find_all_articles() []Article {
|
||||
db := app.db
|
||||
articles := db.select from Article
|
||||
return articles
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
48
tutorials/code/blog/blog.v
Normal file
48
tutorials/code/blog/blog.v
Normal file
@ -0,0 +1,48 @@
|
||||
module main
|
||||
|
||||
import (
|
||||
vweb
|
||||
time
|
||||
pg
|
||||
)
|
||||
|
||||
struct App {
|
||||
mut:
|
||||
vweb vweb.Context
|
||||
db pg.DB
|
||||
}
|
||||
|
||||
fn main() {
|
||||
app := App{}
|
||||
vweb.run(mut app, 8080)
|
||||
}
|
||||
|
||||
fn (app mut App) index_text() {
|
||||
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()
|
||||
}
|
||||
|
||||
pub fn (app mut App) init() {
|
||||
db := pg.connect(pg.Config{
|
||||
host: '127.0.0.1'
|
||||
dbname: 'blog'
|
||||
user: 'alex'
|
||||
}) or { panic(err) }
|
||||
app.db = db
|
||||
}
|
||||
|
||||
fn (app mut App) time() {
|
||||
app.vweb.text(time.now().format())
|
||||
}
|
||||
|
Reference in New Issue
Block a user