diff --git a/tutorials/building_a_simple_web_blog_with_vweb/README.md b/tutorials/Building a simple web blog with vweb/README.md similarity index 100% rename from tutorials/building_a_simple_web_blog_with_vweb/README.md rename to tutorials/Building a simple web blog with vweb/README.md diff --git a/tutorials/building_a_simple_web_blog_with_vweb/code/blog/.gitignore b/tutorials/Building a simple web blog with vweb/code/blog/.gitignore similarity index 100% rename from tutorials/building_a_simple_web_blog_with_vweb/code/blog/.gitignore rename to tutorials/Building a simple web blog with vweb/code/blog/.gitignore diff --git a/tutorials/building_a_simple_web_blog_with_vweb/code/blog/article.v b/tutorials/Building a simple web blog with vweb/code/blog/article.v similarity index 100% rename from tutorials/building_a_simple_web_blog_with_vweb/code/blog/article.v rename to tutorials/Building a simple web blog with vweb/code/blog/article.v diff --git a/tutorials/Building a simple web blog with vweb/code/blog/article.ve b/tutorials/Building a simple web blog with vweb/code/blog/article.ve new file mode 100644 index 0000000000..f12753241f --- /dev/null +++ b/tutorials/Building a simple web blog with vweb/code/blog/article.ve @@ -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 + } +} diff --git a/tutorials/building_a_simple_web_blog_with_vweb/code/blog/blog.sqlite b/tutorials/Building a simple web blog with vweb/code/blog/blog.sqlite similarity index 100% rename from tutorials/building_a_simple_web_blog_with_vweb/code/blog/blog.sqlite rename to tutorials/Building a simple web blog with vweb/code/blog/blog.sqlite diff --git a/tutorials/building_a_simple_web_blog_with_vweb/code/blog/blog.v b/tutorials/Building a simple web blog with vweb/code/blog/blog.v similarity index 100% rename from tutorials/building_a_simple_web_blog_with_vweb/code/blog/blog.v rename to tutorials/Building a simple web blog with vweb/code/blog/blog.v diff --git a/tutorials/Building a simple web blog with vweb/code/blog/blog.ve b/tutorials/Building a simple web blog with vweb/code/blog/blog.ve new file mode 100644 index 0000000000..d6284ce55e --- /dev/null +++ b/tutorials/Building a simple web blog with vweb/code/blog/blog.ve @@ -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()) +} diff --git a/tutorials/building_a_simple_web_blog_with_vweb/code/blog/index.html b/tutorials/Building a simple web blog with vweb/code/blog/index.html similarity index 100% rename from tutorials/building_a_simple_web_blog_with_vweb/code/blog/index.html rename to tutorials/Building a simple web blog with vweb/code/blog/index.html diff --git a/tutorials/building_a_simple_web_blog_with_vweb/code/blog/new.html b/tutorials/Building a simple web blog with vweb/code/blog/new.html similarity index 100% rename from tutorials/building_a_simple_web_blog_with_vweb/code/blog/new.html rename to tutorials/Building a simple web blog with vweb/code/blog/new.html diff --git a/tutorials/building_a_simple_web_blog_with_vweb/img/articles1.png b/tutorials/Building a simple web blog with vweb/img/articles1.png similarity index 100% rename from tutorials/building_a_simple_web_blog_with_vweb/img/articles1.png rename to tutorials/Building a simple web blog with vweb/img/articles1.png diff --git a/tutorials/building_a_simple_web_blog_with_vweb/img/articles_json.png b/tutorials/Building a simple web blog with vweb/img/articles_json.png similarity index 100% rename from tutorials/building_a_simple_web_blog_with_vweb/img/articles_json.png rename to tutorials/Building a simple web blog with vweb/img/articles_json.png diff --git a/tutorials/building_a_simple_web_blog_with_vweb/img/hello.png b/tutorials/Building a simple web blog with vweb/img/hello.png similarity index 100% rename from tutorials/building_a_simple_web_blog_with_vweb/img/hello.png rename to tutorials/Building a simple web blog with vweb/img/hello.png diff --git a/tutorials/building_a_simple_web_blog_with_vweb/img/hello_html.png b/tutorials/Building a simple web blog with vweb/img/hello_html.png similarity index 100% rename from tutorials/building_a_simple_web_blog_with_vweb/img/hello_html.png rename to tutorials/Building a simple web blog with vweb/img/hello_html.png diff --git a/tutorials/building_a_simple_web_blog_with_vweb/img/time.png b/tutorials/Building a simple web blog with vweb/img/time.png similarity index 100% rename from tutorials/building_a_simple_web_blog_with_vweb/img/time.png rename to tutorials/Building a simple web blog with vweb/img/time.png