Skip to content

Commit b45da86

Browse files
authored
examples: refactor vweb_orm_jwt (#15389)
1 parent 9c96b13 commit b45da86

File tree

8 files changed

+31
-48
lines changed

8 files changed

+31
-48
lines changed

examples/vweb_orm_jwt/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ v
77
*.dylib
88
*.dll
99
vls.log
10-
.env
10+
.env
11+
*.db

examples/vweb_orm_jwt/src/auth_services.v

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ struct JwtHeader {
1515
}
1616

1717
struct JwtPayload {
18-
sub string // (subject) = Entidade à quem o token pertence, normalmente o ID do usuário;
19-
iss string // (issuer) = Emissor do token;
20-
exp string // (expiration) = Timestamp de quando o token irá expirar;
21-
iat time.Time // (issued at) = Timestamp de quando o token foi criado;
22-
aud string // (audience) = Destinatário do token, representa a aplicação que irá usá-lo.
18+
sub string // (subject) = Entity to whom the token belongs, usually the user ID;
19+
iss string // (issuer) = Token issuer;
20+
exp string // (expiration) = Timestamp of when the token will expire;
21+
iat time.Time // (issued at) = Timestamp of when the token was created;
22+
aud string // (audience) = Token recipient, represents the application that will use it.
2323
name string
2424
roles string
2525
permissions string
@@ -42,7 +42,7 @@ fn (mut app App) service_auth(username string, password string) ?string {
4242
return error('user is not active')
4343
}
4444

45-
db.close()
45+
db.close()?
4646

4747
bcrypt.compare_hash_and_password(password.bytes(), user.password.bytes()) or {
4848
return error('Failed to auth user, $err')
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module databases
2+
3+
import sqlite
4+
5+
pub fn create_db_connection() ?sqlite.DB {
6+
mut db := sqlite.connect('database.db')?
7+
return db
8+
}

examples/vweb_orm_jwt/src/databases/config_datases_mysql.v

Lines changed: 0 additions & 18 deletions
This file was deleted.

examples/vweb_orm_jwt/src/main.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn main() {
1818
create table User
1919
}
2020

21-
db.close()
21+
db.close() or { panic(err) }
2222

2323
vweb.run(new_app(), http_port)
2424
}

examples/vweb_orm_jwt/src/user_controllers.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ pub fn (mut app App) delete() vweb.Result {
6161
}
6262

6363
defer {
64-
db.close()
64+
db.close() or { panic(err) }
6565
}
6666

6767
sql db {
6868
drop table User
6969
}
7070

71-
return app.text('Tabela deletada com sucesso')
71+
return app.text('Successfully deleted table')
7272
}
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
module main
22

3-
import time
4-
5-
[table: 'usersxqa']
3+
[table: 'users']
64
struct User {
75
mut:
8-
id int [primary; sql: serial]
9-
username string [required; sql_type: 'varchar(191)']
10-
password string [required; sql_type: 'longtext']
11-
name string [sql_type: 'varchar(191)']
12-
created_at time.Time [sql_type: 'datetime(3)']
13-
updated_at time.Time [sql_type: 'datetime(3)']
14-
deleted_at time.Time [sql_type: 'datetime(3)']
6+
id int [primary; sql: serial]
7+
username string [required; sql_type: 'TEXT']
8+
password string [required; sql_type: 'TEXT']
9+
created_at string [default: 'CURRENT_TIMESTAMP']
10+
updated_at string [default: 'CURRENT_TIMESTAMP']
11+
deleted_at string [default: 'CURRENT_TIMESTAMP']
1512
active bool
1613
}

examples/vweb_orm_jwt/src/user_services.v

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module main
22

33
import crypto.bcrypt
44
import databases
5-
import time
65

76
fn (mut app App) service_add_user(username string, password string) ?User {
87
mut db := databases.create_db_connection() or {
@@ -11,7 +10,7 @@ fn (mut app App) service_add_user(username string, password string) ?User {
1110
}
1211

1312
defer {
14-
db.close()
13+
db.close() or { panic(err) }
1514
}
1615

1716
hashed_password := bcrypt.generate_from_password(password.bytes(), bcrypt.min_cost) or {
@@ -21,11 +20,7 @@ fn (mut app App) service_add_user(username string, password string) ?User {
2120

2221
user_model := User{
2322
username: username
24-
name: password
2523
password: hashed_password
26-
created_at: time.now()
27-
updated_at: time.now()
28-
deleted_at: time.now()
2924
active: true
3025
}
3126

@@ -47,7 +42,7 @@ fn (mut app App) service_get_user_by_id(user_id int) ?User {
4742
}
4843

4944
defer {
50-
db.close()
45+
db.close() or { panic(err) }
5146
}
5247

5348
results := sql db {
@@ -64,7 +59,7 @@ fn (mut app App) service_get_all_user() ?[]User {
6459
}
6560

6661
defer {
67-
db.close()
62+
db.close() or { panic(err) }
6863
}
6964

7065
results := sql db {
@@ -81,15 +76,15 @@ fn (mut app App) service_get_by_username(username string) ?User {
8176
}
8277

8378
defer {
84-
db.close()
79+
db.close() or { panic(err) }
8580
}
8681

8782
results := sql db {
8883
select from User where username == username
8984
}
9085

9186
if results.len == 0 {
92-
return error('Usuário não encontrado')
87+
return error('User not found')
9388
}
9489

9590
return results[0]

0 commit comments

Comments
 (0)