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

tutorials: rename vweb tutorial

This commit is contained in:
Alexander Medvednikov
2022-06-22 22:48:40 +03:00
parent ae2183043b
commit fe673e7963
14 changed files with 93 additions and 0 deletions

View File

@@ -0,0 +1 @@
blog.db

View File

@@ -0,0 +1,13 @@
module main
struct Article {
id int [primary; sql: serial]
title string
text string
}
pub fn (app &App) find_all_articles() []Article {
return sql app.db {
select from Article
}
}

View File

@@ -0,0 +1,13 @@
module main
struct Article {
id int [primary; sql: serial]
title string
text string
}
pub fn (app &App) find_all_articles() []Article {
return sql app.db {
select from Article
}
}

View File

@@ -0,0 +1,17 @@
drop table if exists Article;
create table Article (
id integer primary key,
title text default "",
text text default ""
);
insert into Article (title, text) values (
"Hello, world!",
"V is great."
);
insert into Article (title, text) values (
"Second post.",
"Hm... what should I write about?"
);

View File

@@ -0,0 +1,80 @@
module main
import vweb
import time
import sqlite
import json
struct App {
vweb.Context
pub mut:
db sqlite.DB
user_id string
}
fn main() {
mut app := App{
db: sqlite.connect('blog.db') or { panic(err) }
}
sql app.db {
create table Article
}
vweb.run(app, 8081)
}
/*
pub fn (mut app App) index_text() vweb.Result {
app.vweb.text('Hello, world from vweb!')
return vweb.Result{}
}
pub fn (app &App) index_html() vweb.Result {
message := 'Hello, world from Vweb!'
return $vweb.html()
}
*/
['/index']
pub fn (app &App) index() vweb.Result {
articles := app.find_all_articles()
return $vweb.html()
}
pub fn (mut app App) before_request() {
app.user_id = app.get_cookie('id') or { '0' }
}
['/new']
pub fn (mut app App) new() vweb.Result {
return $vweb.html()
}
['/new_article'; post]
pub fn (mut app App) new_article() vweb.Result {
title := app.form['title']
text := app.form['text']
if title == '' || text == '' {
return app.text('Empty text/title')
}
article := Article{
title: title
text: text
}
println('posting article')
println(article)
sql app.db {
insert article into Article
}
return app.redirect('/')
}
['/articles'; get]
pub fn (mut app App) articles() vweb.Result {
articles := app.find_all_articles()
json_result := json.encode(articles)
return app.json(json_result)
}
fn (mut app App) time() {
app.text(time.now().format())
}

View File

@@ -0,0 +1,80 @@
module main
import vweb
import time
import sqlite
import json
struct App {
vweb.Context
pub mut:
db sqlite.DB
user_id string
}
fn main() {
mut app := App{
db: sqlite.connect('blog.db') or { panic(err) }
}
sql app.db {
create table Article
}
vweb.run(app, 8081)
}
/*
pub fn (mut app App) index_text() vweb.Result {
app.vweb.text('Hello, world from vweb!')
return vweb.Result{}
}
pub fn (app &App) index_html() vweb.Result {
message := 'Hello, world from Vweb!'
return $vweb.html()
}
*/
['/index']
pub fn (app &App) index() vweb.Result {
articles := app.find_all_articles()
return $vweb.html()
}
pub fn (mut app App) before_request() {
app.user_id = app.get_cookie('id') or { '0' }
}
['/new']
pub fn (mut app App) new() vweb.Result {
return $vweb.html()
}
['/new_article'; post]
pub fn (mut app App) new_article() vweb.Result {
title := app.form['title']
text := app.form['text']
if title == '' || text == '' {
return app.text('Empty text/title')
}
article := Article{
title: title
text: text
}
println('posting article')
println(article)
sql app.db {
insert article into Article
}
return app.redirect('/')
}
['/articles'; get]
pub fn (mut app App) articles() vweb.Result {
articles := app.find_all_articles()
json_result := json.encode(articles)
return app.json(json_result)
}
fn (mut app App) time() {
app.text(time.now().format())
}

View File

@@ -0,0 +1,16 @@
<html>
<header>
<title>V Blog</title>
</header>
<body>
user id = @app.user_id <br>
@for article in articles
<div>
<b>@article.title</b> <br>
@article.text
</div>
<br>
@end
<a href='/new'>New article</a>
</body>
</html>

View File

@@ -0,0 +1,13 @@
<html>
<header>
<title>V Blog</title>
</header>
<body>
<form action='/new_article' method='post'>
<input type='text' placeholder='Title' name='title'> <br>
<textarea placeholder='Text' name='text'></textarea>
<input type='submit'>
</form>
</body>
</html>