From 48ea136a9a467f7aea1150ddfa51c587b8fce5e7 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 15 Dec 2019 00:55:05 +0300 Subject: [PATCH] tutorials: vweb: add an article --- .../building-a-simple-web-blog-with-vweb.md | 63 +++++++++++++++++++ tutorials/code/blog/blog.v | 27 ++++++++ tutorials/code/blog/index.html | 2 + tutorials/code/blog/new.html | 13 ++++ vlib/compiler/fn.v | 2 +- 5 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 tutorials/code/blog/new.html diff --git a/tutorials/building-a-simple-web-blog-with-vweb.md b/tutorials/building-a-simple-web-blog-with-vweb.md index 58431fd3c3..ffbb2a224c 100644 --- a/tutorials/building-a-simple-web-blog-with-vweb.md +++ b/tutorials/building-a-simple-web-blog-with-vweb.md @@ -317,6 +317,69 @@ article := app.retrieve_article(10) or { > `db := app.db` is a temporary limitation in the V ORM, soon this will not be needed. + +### Adding new articles + +Create `new.html`: + +```html + +
+ V Blog +
+ +
+
+ + +
+ + +``` + +```v +pub fn (app mut App) new_article() { + title := app.vweb.form['title'] + text := app.vweb.form['text'] + if title == '' || text == '' { + app.vweb.text('Empty text/titile') + return + } + article := Article{ + title: title + text: text + } + db := app.db + db.insert(article) + app.vweb.redirect('/article/') +} +``` + +> Untyped `form['key']` is temporary. Very soon Vweb will accept query and form +parameters via function arguments: `new_article(title, text string) {`. + +We need to update `index.html` to add a link to the "new article" page: +New article + + + +### JSON endpoints + +This tutorial used the traditional server side rendering. If you prefer +to render everything on the client or need an API, creating JSON endpoints +in V is very simple: + +```v +pub fn (app mut App) articles() { + articles := app.find_all_articles() + app.vweb.json(json.encode(articles)) +} +``` + + + + + To be continued on Dec 14... For an example of a more sophisticated web app written in V, check out Vorum: https://github.com/vlang/vorum diff --git a/tutorials/code/blog/blog.v b/tutorials/code/blog/blog.v index 6e0c505524..d6c70d63fd 100644 --- a/tutorials/code/blog/blog.v +++ b/tutorials/code/blog/blog.v @@ -4,6 +4,7 @@ import ( vweb time pg + json ) struct App { @@ -42,6 +43,32 @@ pub fn (app mut App) init() { app.db = db } +pub fn (app mut App) new() { + $vweb.html() +} + +pub fn (app mut App) new_article() { + title := app.vweb.form['title'] + text := app.vweb.form['text'] + if title == '' || text == '' { + app.vweb.text('Empty text/titile') + return + } + article := Article{ + title: title + text: text + } + println(article) + db := app.db + db.insert(article) + app.vweb.redirect('/article/') +} + +pub fn (app mut App) articles() { + articles := app.find_all_articles() + app.vweb.json(json.encode(articles)) +} + fn (app mut App) time() { app.vweb.text(time.now().format()) } diff --git a/tutorials/code/blog/index.html b/tutorials/code/blog/index.html index 4ec36c56a6..b2541c94f6 100644 --- a/tutorials/code/blog/index.html +++ b/tutorials/code/blog/index.html @@ -9,6 +9,8 @@ @article.text @end +
+ New article diff --git a/tutorials/code/blog/new.html b/tutorials/code/blog/new.html new file mode 100644 index 0000000000..04ca000688 --- /dev/null +++ b/tutorials/code/blog/new.html @@ -0,0 +1,13 @@ + +
+ V Blog +
+ +
+
+ + +
+ + + diff --git a/vlib/compiler/fn.v b/vlib/compiler/fn.v index ca513e6067..357eb68762 100644 --- a/vlib/compiler/fn.v +++ b/vlib/compiler/fn.v @@ -633,7 +633,7 @@ fn (p mut Parser) check_unused_and_mut_vars() { } if !var.is_changed && var.is_mut && !p.pref.is_repl && !p.pref.translated && var.typ != 'T*' && - p.mod != 'ui' + p.mod != 'ui' && var.typ != 'App*' { p.error_with_token_index('`$var.name` is declared as mutable, but it was never changed', var.token_idx ) }