tutorials: more blog fixes (#5657)

pull/5667/head
Lukas Neubert 2020-07-04 18:56:18 +02:00 committed by GitHub
parent 0626ac2901
commit fbfd92a899
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 60 additions and 64 deletions

View File

@ -64,14 +64,13 @@ fn main() {
vweb.run<App>(8081)
}
fn (mut app App) index() {
fn (mut app App) index() vweb.Result {
app.vweb.text('Hello, world from vweb!')
return vweb.Result{}
}
pub fn (app &App) init() {}
pub fn (app &App) init_once() {}
pub fn (app &App) reset() {}
```
Run it with
@ -115,7 +114,6 @@ text, which isn't frequently used in websites.
### HTML View
Let's return an HTML view instead. Create `index.html` in the same directory:
```html
@ -134,9 +132,9 @@ Let's return an HTML view instead. Create `index.html` in the same directory:
and update our `index()` action so that it returns the HTML view we just created:
```v
fn (mut app App) index() {
fn (mut app App) index() vweb.Result {
message := 'Hello, world from Vweb!'
$vweb.html()
return $vweb.html()
}
```
@ -170,48 +168,47 @@ into a single binary file together with the web application itself.
- All errors in the templates are guaranteed to be caught during compilation.
### Fetching data with V ORM
Now let's display some articles!
We'll be using V's builtin ORM and a Postgres database. (V ORM will also
support MySQL, SQLite, and SQL Server soon.)
We'll be using V's builtin ORM and a SQLite database.
(V ORM will also support MySQL, Postgre, and SQL Server soon.)
Create a SQL file with the schema:
Create a SQLite file with the schema:
```sql
create database blog;
drop table Article;
\c blog
drop table articles;
create table articles (
id serial primary key,
title text default '',
text text default ''
create table Article (
id integer primary key,
title text default "",
text text default ""
);
insert into articles (title, text) values (
'Hello, world!',
'V is great.'
insert into Article (title, text) values (
"Hello, world!",
"V is great."
);
insert into articles (title, text) values (
'Second post.',
'Hm... what should I write about?'
insert into Article (title, text) values (
"Second post.",
"Hm... what should I write about?"
);
```
Run the file with `psql -f blog.sql`.
Run the file with `sqlite3 blog.db < blog.sqlite`.
Add a Postgres DB handle to `App`:
Add a SQLite handle to `App`:
```v
import sqlite
struct App {
pub mut:
vweb vweb.Context
db pg.DB
db sqlite.DB
}
```
@ -221,7 +218,10 @@ Modify the `init_once()` method we created earlier to connect to a database:
```v
pub fn (mut app App) init_once() {
db := sqlite.connect(':memory:') or { panic(err) }
db := sqlite.connect(':memory:') or { panic(err) }
db.exec('create table `Article` (id integer primary key, title text default "", text text default "")')
db.exec('insert into Article (title, text) values ("Hello, world!", "V is great.")')
db.exec('insert into Article (title, text) values ("Second post.", "Hm... what should I write about?")')
app.db = db
}
```
@ -233,7 +233,7 @@ Create a new file `article.v`:
```v
// article.v
module main
struct Article {
@ -342,6 +342,7 @@ pub fn (mut app App) new_article() vweb.Result {
title: title
text: text
}
println(article)
sql app.db {
insert article into Article
}

View File

@ -8,6 +8,6 @@ struct Article {
pub fn (app &App) find_all_articles() []Article {
return sql app.db {
select from Article
select from Article
}
}

View File

@ -1,22 +0,0 @@
create database blog;
\c blog
drop table articles;
create table articles (
id serial primary key,
title text default '',
text text default ''
);
insert into articles (title, text) values (
'Hello, world!',
'V is great.'
);
insert into articles (title, text) values (
'Second post.',
'Hm... what should I write about?'
);

View File

@ -0,0 +1,17 @@
drop table 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

@ -15,26 +15,27 @@ fn main() {
vweb.run<App>(8081)
}
fn (mut app App) index_text() {
/*
fn (mut app App) index_text() vweb.Result {
app.vweb.text('Hello, world from vweb!')
return vweb.Result{}
}
/*
fn (app &App) index_html() {
message := 'Hello, world from vweb!'
$vweb.html()
fn (app &App) index_html() vweb.Result {
message := 'Hello, world from Vweb!'
return $vweb.html()
}
*/
fn (app &App) index() vweb.Result {
articles := app.find_all_articles()
return $vweb.html()
}
pub fn (mut app App) init_once() {
db := sqlite.connect(':memory:') or {
db := sqlite.connect('blog.db') or {
panic(err)
}
db.exec('create table `Article` (id integer primary key, title text default "", text text default "")')
app.db = db
}
@ -49,10 +50,10 @@ 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')
app.vweb.text('Empty text/title')
return vweb.Result{}
}
article := Article{
article := Article {
title: title
text: text
}
@ -60,7 +61,7 @@ pub fn (mut app App) new_article() vweb.Result {
sql app.db {
insert article into Article
}
return app.vweb.redirect('/article/')
return app.vweb.redirect('/')
}
pub fn (mut app App) articles() {

View File

@ -8,9 +8,8 @@
<b>@article.title</b> <br>
@article.text
</div>
<br>
@end
<br>
<a href='/new'>New article</a>
</body>
</html>