mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
tools/vcreate: fix web app (#18019)
This commit is contained in:
parent
3fb32a866c
commit
cd90bc65b8
@ -461,8 +461,8 @@ pub fn (mut app App) controller_auth(username string, password string) vweb.Resu
|
|||||||
content: 'module main
|
content: 'module main
|
||||||
|
|
||||||
struct AuthRequestDto {
|
struct AuthRequestDto {
|
||||||
username string [required]
|
username string [nonull]
|
||||||
password string [required]
|
password string [nonull]
|
||||||
}
|
}
|
||||||
'
|
'
|
||||||
}
|
}
|
||||||
@ -504,9 +504,10 @@ fn (mut app App) service_auth(username string, password string) !string {
|
|||||||
db.close() or { panic(err) }
|
db.close() or { panic(err) }
|
||||||
}
|
}
|
||||||
|
|
||||||
user := sql db {
|
users := sql db {
|
||||||
select from User where username == username limit 1
|
select from User where username == username
|
||||||
}
|
}!
|
||||||
|
user := users.first()
|
||||||
if user.username != username {
|
if user.username != username {
|
||||||
return error('user not found')
|
return error('user not found')
|
||||||
}
|
}
|
||||||
@ -588,6 +589,9 @@ fn auth_verify(token string) bool {
|
|||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
<script type='text/javascript'>
|
<script type='text/javascript'>
|
||||||
|
// function eraseCookie(name) {
|
||||||
|
// document.cookie = name + '=; Max-Age=0'
|
||||||
|
// }
|
||||||
async function addUser() {
|
async function addUser() {
|
||||||
const form = document.querySelector('#index_form');
|
const form = document.querySelector('#index_form');
|
||||||
const formData = new FormData(form);
|
const formData = new FormData(form);
|
||||||
@ -659,6 +663,7 @@ fn main() {
|
|||||||
|
|
||||||
sql db {
|
sql db {
|
||||||
create table User
|
create table User
|
||||||
|
create table Product
|
||||||
} or { panic('error on create table: \${err}') }
|
} or { panic('error on create table: \${err}') }
|
||||||
|
|
||||||
db.close() or { panic(err) }
|
db.close() or { panic(err) }
|
||||||
@ -752,7 +757,7 @@ pub fn (mut app App) controller_create_product(product_name string) vweb.Result
|
|||||||
struct Product {
|
struct Product {
|
||||||
id int [primary; sql: serial]
|
id int [primary; sql: serial]
|
||||||
user_id int
|
user_id int
|
||||||
name string [required; sql_type: 'TEXT']
|
name string [nonull; sql_type: 'TEXT']
|
||||||
created_at string [default: 'CURRENT_TIMESTAMP']
|
created_at string [default: 'CURRENT_TIMESTAMP']
|
||||||
}
|
}
|
||||||
"
|
"
|
||||||
@ -786,7 +791,7 @@ fn (mut app App) service_add_product(product_name string, user_id int) ! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut app App) service_get_all_products_from(user_id int) ?[]Product {
|
fn (mut app App) service_get_all_products_from(user_id int) ![]Product {
|
||||||
mut db := databases.create_db_connection() or {
|
mut db := databases.create_db_connection() or {
|
||||||
println(err)
|
println(err)
|
||||||
return err
|
return err
|
||||||
@ -798,7 +803,7 @@ fn (mut app App) service_get_all_products_from(user_id int) ?[]Product {
|
|||||||
|
|
||||||
results := sql db {
|
results := sql db {
|
||||||
select from Product where user_id == user_id
|
select from Product where user_id == user_id
|
||||||
}
|
}!
|
||||||
|
|
||||||
return results
|
return results
|
||||||
}
|
}
|
||||||
@ -875,6 +880,7 @@ import json
|
|||||||
|
|
||||||
['/controller/users'; get]
|
['/controller/users'; get]
|
||||||
pub fn (mut app App) controller_get_all_user() vweb.Result {
|
pub fn (mut app App) controller_get_all_user() vweb.Result {
|
||||||
|
// token := app.get_cookie('token') or { '' }
|
||||||
token := app.req.header.get_custom('token') or { '' }
|
token := app.req.header.get_custom('token') or { '' }
|
||||||
|
|
||||||
if !auth_verify(token) {
|
if !auth_verify(token) {
|
||||||
@ -891,6 +897,7 @@ pub fn (mut app App) controller_get_all_user() vweb.Result {
|
|||||||
|
|
||||||
['/controller/user'; get]
|
['/controller/user'; get]
|
||||||
pub fn (mut app App) controller_get_user() vweb.Result {
|
pub fn (mut app App) controller_get_user() vweb.Result {
|
||||||
|
// token := app.get_cookie('token') or { '' }
|
||||||
token := app.req.header.get_custom('token') or { '' }
|
token := app.req.header.get_custom('token') or { '' }
|
||||||
|
|
||||||
if !auth_verify(token) {
|
if !auth_verify(token) {
|
||||||
@ -941,8 +948,8 @@ pub fn (mut app App) controller_create_user(username string, password string) vw
|
|||||||
pub struct User {
|
pub struct User {
|
||||||
mut:
|
mut:
|
||||||
id int [primary; sql: serial]
|
id int [primary; sql: serial]
|
||||||
username string [required; sql_type: 'TEXT'; unique]
|
username string [nonull; sql_type: 'TEXT'; unique]
|
||||||
password string [required; sql_type: 'TEXT']
|
password string [nonull; sql_type: 'TEXT']
|
||||||
active bool
|
active bool
|
||||||
products []Product [fkey: 'user_id']
|
products []Product [fkey: 'user_id']
|
||||||
}
|
}
|
||||||
@ -982,7 +989,7 @@ fn (mut app App) service_add_user(username string, password string) ! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut app App) service_get_all_user() ?[]User {
|
fn (mut app App) service_get_all_user() ![]User {
|
||||||
mut db := databases.create_db_connection() or {
|
mut db := databases.create_db_connection() or {
|
||||||
println(err)
|
println(err)
|
||||||
return err
|
return err
|
||||||
@ -994,12 +1001,12 @@ fn (mut app App) service_get_all_user() ?[]User {
|
|||||||
|
|
||||||
results := sql db {
|
results := sql db {
|
||||||
select from User
|
select from User
|
||||||
}
|
}!
|
||||||
|
|
||||||
return results
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut app App) service_get_user(id int) ?User {
|
fn (mut app App) service_get_user(id int) !User {
|
||||||
mut db := databases.create_db_connection() or {
|
mut db := databases.create_db_connection() or {
|
||||||
println(err)
|
println(err)
|
||||||
return err
|
return err
|
||||||
@ -1011,9 +1018,9 @@ fn (mut app App) service_get_user(id int) ?User {
|
|||||||
|
|
||||||
results := sql db {
|
results := sql db {
|
||||||
select from User where id == id
|
select from User where id == id
|
||||||
}
|
}!
|
||||||
|
|
||||||
return results
|
return results.first()
|
||||||
}
|
}
|
||||||
"
|
"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user