1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

tutorials: vweb: add an article

This commit is contained in:
Alexander Medvednikov
2019-12-15 00:55:05 +03:00
parent fc33f9d49c
commit 48ea136a9a
5 changed files with 106 additions and 1 deletions

View File

@ -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())
}

View File

@ -9,6 +9,8 @@
@article.text
</div>
@end
<br>
<a href='/new'>New article</a>
</body>
</html>

View File

@ -0,0 +1,13 @@
<html>
<header>
<title>V Blog</title>
</header>
<body>
<form action='/new_article' method='post'>
<input type='text' placeholder='Title' name='title'> <br>
<textarea placeholder='Text' name='text'></textarea>
<input type='submit'>
</form>
</body>
</html>