diff --git a/tutorials/building-a-simple-web-blog-with-vweb.md b/tutorials/building-a-simple-web-blog-with-vweb.md index ee647dc37f..bbed9f10c9 100644 --- a/tutorials/building-a-simple-web-blog-with-vweb.md +++ b/tutorials/building-a-simple-web-blog-with-vweb.md @@ -64,14 +64,13 @@ fn main() { vweb.run(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 } diff --git a/tutorials/code/blog/article.v b/tutorials/code/blog/article.v index 1db3e025f8..66f86520f2 100644 --- a/tutorials/code/blog/article.v +++ b/tutorials/code/blog/article.v @@ -8,6 +8,6 @@ struct Article { pub fn (app &App) find_all_articles() []Article { return sql app.db { - select from Article + select from Article } } diff --git a/tutorials/code/blog/blog.sql b/tutorials/code/blog/blog.sql deleted file mode 100644 index f40fccad10..0000000000 --- a/tutorials/code/blog/blog.sql +++ /dev/null @@ -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?' -); - diff --git a/tutorials/code/blog/blog.sqlite b/tutorials/code/blog/blog.sqlite new file mode 100644 index 0000000000..cebd36acec --- /dev/null +++ b/tutorials/code/blog/blog.sqlite @@ -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?" +); diff --git a/tutorials/code/blog/blog.v b/tutorials/code/blog/blog.v index 2774fcb51d..bd34dcaf51 100644 --- a/tutorials/code/blog/blog.v +++ b/tutorials/code/blog/blog.v @@ -15,26 +15,27 @@ fn main() { vweb.run(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() { diff --git a/tutorials/code/blog/index.html b/tutorials/code/blog/index.html index b2541c94f6..7ed9bcc85f 100644 --- a/tutorials/code/blog/index.html +++ b/tutorials/code/blog/index.html @@ -8,9 +8,8 @@ @article.title
@article.text +
@end -
New article -