From fe673e79630283c19f7148e0d729c90d13210338 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 22 Jun 2022 22:48:40 +0300 Subject: [PATCH] tutorials: rename vweb tutorial --- .../README.md | 0 .../code/blog/.gitignore | 0 .../code/blog/article.v | 0 .../code/blog/article.ve | 13 +++ .../code/blog/blog.sqlite | 0 .../code/blog/blog.v | 0 .../code/blog/blog.ve | 80 ++++++++++++++++++ .../code/blog/index.html | 0 .../code/blog/new.html | 0 .../img/articles1.png | Bin .../img/articles_json.png | Bin .../img/hello.png | Bin .../img/hello_html.png | Bin .../img/time.png | Bin 14 files changed, 93 insertions(+) rename tutorials/{building_a_simple_web_blog_with_vweb => Building a simple web blog with vweb}/README.md (100%) rename tutorials/{building_a_simple_web_blog_with_vweb => Building a simple web blog with vweb}/code/blog/.gitignore (100%) rename tutorials/{building_a_simple_web_blog_with_vweb => Building a simple web blog with vweb}/code/blog/article.v (100%) create mode 100644 tutorials/Building a simple web blog with vweb/code/blog/article.ve rename tutorials/{building_a_simple_web_blog_with_vweb => Building a simple web blog with vweb}/code/blog/blog.sqlite (100%) rename tutorials/{building_a_simple_web_blog_with_vweb => Building a simple web blog with vweb}/code/blog/blog.v (100%) create mode 100644 tutorials/Building a simple web blog with vweb/code/blog/blog.ve rename tutorials/{building_a_simple_web_blog_with_vweb => Building a simple web blog with vweb}/code/blog/index.html (100%) rename tutorials/{building_a_simple_web_blog_with_vweb => Building a simple web blog with vweb}/code/blog/new.html (100%) rename tutorials/{building_a_simple_web_blog_with_vweb => Building a simple web blog with vweb}/img/articles1.png (100%) rename tutorials/{building_a_simple_web_blog_with_vweb => Building a simple web blog with vweb}/img/articles_json.png (100%) rename tutorials/{building_a_simple_web_blog_with_vweb => Building a simple web blog with vweb}/img/hello.png (100%) rename tutorials/{building_a_simple_web_blog_with_vweb => Building a simple web blog with vweb}/img/hello_html.png (100%) rename tutorials/{building_a_simple_web_blog_with_vweb => Building a simple web blog with vweb}/img/time.png (100%) 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