aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/vlib/vweb/vweb_app_test.v
blob: b2f9145bbe17f3525929e80bf7aaea0a3dbf1ca1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module main

import vweb
import time
import sqlite

struct App {
	vweb.Context
pub mut:
	db      sqlite.DB
	user_id string
}

struct Article {
	id    int
	title string
	text  string
}

fn test_a_vweb_application_compiles() {
	go fn () {
		time.sleep(2 * time.second)
		exit(0)
	}()
	vweb.run(&App{}, 18081)
}

/*
/TODO
pub fn (mut app App) init_server_old() {
	app.db = sqlite.connect('blog.db') or { panic(err) }
	app.db.create_table('article', [
		'id integer primary key',
		"title text default ''",
		"text text default ''",
	])
}
*/

pub fn (mut app App) before_request() {
	app.user_id = app.get_cookie('id') or { '0' }
}

['/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('/')
}

fn (mut app App) time() {
	app.text(time.now().format())
}