mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
tutorials: more blog fixes (#5657)
This commit is contained in:
@ -8,6 +8,6 @@ struct Article {
|
||||
|
||||
pub fn (app &App) find_all_articles() []Article {
|
||||
return sql app.db {
|
||||
select from Article
|
||||
select from Article
|
||||
}
|
||||
}
|
||||
|
@ -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?'
|
||||
);
|
||||
|
17
tutorials/code/blog/blog.sqlite
Normal file
17
tutorials/code/blog/blog.sqlite
Normal file
@ -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?"
|
||||
);
|
@ -15,26 +15,27 @@ fn main() {
|
||||
vweb.run<App>(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() {
|
||||
|
@ -8,9 +8,8 @@
|
||||
<b>@article.title</b> <br>
|
||||
@article.text
|
||||
</div>
|
||||
<br>
|
||||
@end
|
||||
<br>
|
||||
<a href='/new'>New article</a>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
Reference in New Issue
Block a user