tutorials: more blog fixes (#5657)
parent
0626ac2901
commit
fbfd92a899
|
@ -64,14 +64,13 @@ fn main() {
|
||||||
vweb.run<App>(8081)
|
vweb.run<App>(8081)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut app App) index() {
|
fn (mut app App) index() vweb.Result {
|
||||||
app.vweb.text('Hello, world from vweb!')
|
app.vweb.text('Hello, world from vweb!')
|
||||||
|
return vweb.Result{}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (app &App) init() {}
|
pub fn (app &App) init() {}
|
||||||
pub fn (app &App) init_once() {}
|
pub fn (app &App) init_once() {}
|
||||||
pub fn (app &App) reset() {}
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Run it with
|
Run it with
|
||||||
|
@ -115,7 +114,6 @@ text, which isn't frequently used in websites.
|
||||||
|
|
||||||
### HTML View
|
### HTML View
|
||||||
|
|
||||||
|
|
||||||
Let's return an HTML view instead. Create `index.html` in the same directory:
|
Let's return an HTML view instead. Create `index.html` in the same directory:
|
||||||
|
|
||||||
```html
|
```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:
|
and update our `index()` action so that it returns the HTML view we just created:
|
||||||
|
|
||||||
```v
|
```v
|
||||||
fn (mut app App) index() {
|
fn (mut app App) index() vweb.Result {
|
||||||
message := 'Hello, world from Vweb!'
|
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.
|
- All errors in the templates are guaranteed to be caught during compilation.
|
||||||
|
|
||||||
|
|
||||||
### Fetching data with V ORM
|
### Fetching data with V ORM
|
||||||
|
|
||||||
Now let's display some articles!
|
Now let's display some articles!
|
||||||
|
|
||||||
We'll be using V's builtin ORM and a Postgres database. (V ORM will also
|
We'll be using V's builtin ORM and a SQLite database.
|
||||||
support MySQL, SQLite, and SQL Server soon.)
|
(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
|
```sql
|
||||||
create database blog;
|
drop table Article;
|
||||||
|
|
||||||
\c blog
|
create table Article (
|
||||||
|
id integer primary key,
|
||||||
drop table articles;
|
title text default "",
|
||||||
|
text text default ""
|
||||||
create table articles (
|
|
||||||
id serial primary key,
|
|
||||||
title text default '',
|
|
||||||
text text default ''
|
|
||||||
);
|
);
|
||||||
|
|
||||||
insert into articles (title, text) values (
|
insert into Article (title, text) values (
|
||||||
'Hello, world!',
|
"Hello, world!",
|
||||||
'V is great.'
|
"V is great."
|
||||||
);
|
);
|
||||||
|
|
||||||
insert into articles (title, text) values (
|
insert into Article (title, text) values (
|
||||||
'Second post.',
|
"Second post.",
|
||||||
'Hm... what should I write about?'
|
"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
|
```v
|
||||||
|
import sqlite
|
||||||
|
|
||||||
struct App {
|
struct App {
|
||||||
pub mut:
|
pub mut:
|
||||||
vweb vweb.Context
|
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
|
```v
|
||||||
pub fn (mut app App) init_once() {
|
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
|
app.db = db
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -233,7 +233,7 @@ Create a new file `article.v`:
|
||||||
|
|
||||||
|
|
||||||
```v
|
```v
|
||||||
|
// article.v
|
||||||
module main
|
module main
|
||||||
|
|
||||||
struct Article {
|
struct Article {
|
||||||
|
@ -342,6 +342,7 @@ pub fn (mut app App) new_article() vweb.Result {
|
||||||
title: title
|
title: title
|
||||||
text: text
|
text: text
|
||||||
}
|
}
|
||||||
|
println(article)
|
||||||
sql app.db {
|
sql app.db {
|
||||||
insert article into Article
|
insert article into Article
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,6 @@ struct Article {
|
||||||
|
|
||||||
pub fn (app &App) find_all_articles() []Article {
|
pub fn (app &App) find_all_articles() []Article {
|
||||||
return sql app.db {
|
return sql app.db {
|
||||||
select from Article
|
select from Article
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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?'
|
|
||||||
);
|
|
||||||
|
|
|
@ -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?"
|
||||||
|
);
|
|
@ -15,26 +15,27 @@ fn main() {
|
||||||
vweb.run<App>(8081)
|
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!')
|
app.vweb.text('Hello, world from vweb!')
|
||||||
|
return vweb.Result{}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
fn (app &App) index_html() vweb.Result {
|
||||||
fn (app &App) index_html() {
|
message := 'Hello, world from Vweb!'
|
||||||
message := 'Hello, world from vweb!'
|
return $vweb.html()
|
||||||
$vweb.html()
|
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
fn (app &App) index() vweb.Result {
|
fn (app &App) index() vweb.Result {
|
||||||
articles := app.find_all_articles()
|
articles := app.find_all_articles()
|
||||||
return $vweb.html()
|
return $vweb.html()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut app App) init_once() {
|
pub fn (mut app App) init_once() {
|
||||||
db := sqlite.connect(':memory:') or {
|
db := sqlite.connect('blog.db') or {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
db.exec('create table `Article` (id integer primary key, title text default "", text text default "")')
|
|
||||||
app.db = db
|
app.db = db
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,10 +50,10 @@ pub fn (mut app App) new_article() vweb.Result {
|
||||||
title := app.vweb.form['title']
|
title := app.vweb.form['title']
|
||||||
text := app.vweb.form['text']
|
text := app.vweb.form['text']
|
||||||
if title == '' || text == '' {
|
if title == '' || text == '' {
|
||||||
app.vweb.text('Empty text/titile')
|
app.vweb.text('Empty text/title')
|
||||||
return vweb.Result{}
|
return vweb.Result{}
|
||||||
}
|
}
|
||||||
article := Article{
|
article := Article {
|
||||||
title: title
|
title: title
|
||||||
text: text
|
text: text
|
||||||
}
|
}
|
||||||
|
@ -60,7 +61,7 @@ pub fn (mut app App) new_article() vweb.Result {
|
||||||
sql app.db {
|
sql app.db {
|
||||||
insert article into Article
|
insert article into Article
|
||||||
}
|
}
|
||||||
return app.vweb.redirect('/article/')
|
return app.vweb.redirect('/')
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut app App) articles() {
|
pub fn (mut app App) articles() {
|
||||||
|
|
|
@ -8,9 +8,8 @@
|
||||||
<b>@article.title</b> <br>
|
<b>@article.title</b> <br>
|
||||||
@article.text
|
@article.text
|
||||||
</div>
|
</div>
|
||||||
|
<br>
|
||||||
@end
|
@end
|
||||||
<br>
|
|
||||||
<a href='/new'>New article</a>
|
<a href='/new'>New article</a>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue