diff --git a/examples/vweb_orm_jwt/.gitignore b/examples/vweb_orm_jwt/.gitignore index f98f5f46b8..cb0237da4a 100644 --- a/examples/vweb_orm_jwt/.gitignore +++ b/examples/vweb_orm_jwt/.gitignore @@ -7,4 +7,5 @@ v *.dylib *.dll vls.log -.env \ No newline at end of file +.env +*.db diff --git a/examples/vweb_orm_jwt/src/auth_services.v b/examples/vweb_orm_jwt/src/auth_services.v index de5e9ee36f..5536b1dac0 100644 --- a/examples/vweb_orm_jwt/src/auth_services.v +++ b/examples/vweb_orm_jwt/src/auth_services.v @@ -15,11 +15,11 @@ struct JwtHeader { } struct JwtPayload { - sub string // (subject) = Entidade à quem o token pertence, normalmente o ID do usuário; - iss string // (issuer) = Emissor do token; - exp string // (expiration) = Timestamp de quando o token irá expirar; - iat time.Time // (issued at) = Timestamp de quando o token foi criado; - aud string // (audience) = Destinatário do token, representa a aplicação que irá usá-lo. + sub string // (subject) = Entity to whom the token belongs, usually the user ID; + iss string // (issuer) = Token issuer; + exp string // (expiration) = Timestamp of when the token will expire; + iat time.Time // (issued at) = Timestamp of when the token was created; + aud string // (audience) = Token recipient, represents the application that will use it. name string roles string permissions string @@ -42,7 +42,7 @@ fn (mut app App) service_auth(username string, password string) ?string { return error('user is not active') } - db.close() + db.close()? bcrypt.compare_hash_and_password(password.bytes(), user.password.bytes()) or { return error('Failed to auth user, $err') diff --git a/examples/vweb_orm_jwt/src/databases/config_databases_sqlite.v b/examples/vweb_orm_jwt/src/databases/config_databases_sqlite.v new file mode 100644 index 0000000000..8cc608053d --- /dev/null +++ b/examples/vweb_orm_jwt/src/databases/config_databases_sqlite.v @@ -0,0 +1,8 @@ +module databases + +import sqlite + +pub fn create_db_connection() ?sqlite.DB { + mut db := sqlite.connect('database.db')? + return db +} diff --git a/examples/vweb_orm_jwt/src/databases/config_datases_mysql.v b/examples/vweb_orm_jwt/src/databases/config_datases_mysql.v deleted file mode 100644 index 1280c6bf18..0000000000 --- a/examples/vweb_orm_jwt/src/databases/config_datases_mysql.v +++ /dev/null @@ -1,18 +0,0 @@ -module databases - -import mysql -import os - -pub fn create_db_connection() ?mysql.Connection { - mut db := mysql.Connection{ - host: os.getenv('DB_HOST') - port: os.getenv('DB_PORT').u32() - username: os.getenv('DB_USERNAME') - password: os.getenv('DB_PASSWORD') - dbname: os.getenv('DB_NAME') - } - - db.connect() or { println(err) } - - return db -} diff --git a/examples/vweb_orm_jwt/src/main.v b/examples/vweb_orm_jwt/src/main.v index 16dbe04059..96159faccd 100644 --- a/examples/vweb_orm_jwt/src/main.v +++ b/examples/vweb_orm_jwt/src/main.v @@ -18,7 +18,7 @@ fn main() { create table User } - db.close() + db.close() or { panic(err) } vweb.run(new_app(), http_port) } diff --git a/examples/vweb_orm_jwt/src/user_controllers.v b/examples/vweb_orm_jwt/src/user_controllers.v index ba38cb97e9..ef93f198b0 100644 --- a/examples/vweb_orm_jwt/src/user_controllers.v +++ b/examples/vweb_orm_jwt/src/user_controllers.v @@ -61,12 +61,12 @@ pub fn (mut app App) delete() vweb.Result { } defer { - db.close() + db.close() or { panic(err) } } sql db { drop table User } - return app.text('Tabela deletada com sucesso') + return app.text('Successfully deleted table') } diff --git a/examples/vweb_orm_jwt/src/user_entities.v b/examples/vweb_orm_jwt/src/user_entities.v index 18bfb9dda9..f22bd4461e 100644 --- a/examples/vweb_orm_jwt/src/user_entities.v +++ b/examples/vweb_orm_jwt/src/user_entities.v @@ -1,16 +1,13 @@ module main -import time - -[table: 'usersxqa'] +[table: 'users'] struct User { mut: - id int [primary; sql: serial] - username string [required; sql_type: 'varchar(191)'] - password string [required; sql_type: 'longtext'] - name string [sql_type: 'varchar(191)'] - created_at time.Time [sql_type: 'datetime(3)'] - updated_at time.Time [sql_type: 'datetime(3)'] - deleted_at time.Time [sql_type: 'datetime(3)'] + id int [primary; sql: serial] + username string [required; sql_type: 'TEXT'] + password string [required; sql_type: 'TEXT'] + created_at string [default: 'CURRENT_TIMESTAMP'] + updated_at string [default: 'CURRENT_TIMESTAMP'] + deleted_at string [default: 'CURRENT_TIMESTAMP'] active bool } diff --git a/examples/vweb_orm_jwt/src/user_services.v b/examples/vweb_orm_jwt/src/user_services.v index b71ac56cc4..c83e2cb1b9 100644 --- a/examples/vweb_orm_jwt/src/user_services.v +++ b/examples/vweb_orm_jwt/src/user_services.v @@ -2,7 +2,6 @@ module main import crypto.bcrypt import databases -import time fn (mut app App) service_add_user(username string, password string) ?User { mut db := databases.create_db_connection() or { @@ -11,7 +10,7 @@ fn (mut app App) service_add_user(username string, password string) ?User { } defer { - db.close() + db.close() or { panic(err) } } 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 { user_model := User{ username: username - name: password password: hashed_password - created_at: time.now() - updated_at: time.now() - deleted_at: time.now() active: true } @@ -47,7 +42,7 @@ fn (mut app App) service_get_user_by_id(user_id int) ?User { } defer { - db.close() + db.close() or { panic(err) } } results := sql db { @@ -64,7 +59,7 @@ fn (mut app App) service_get_all_user() ?[]User { } defer { - db.close() + db.close() or { panic(err) } } results := sql db { @@ -81,7 +76,7 @@ fn (mut app App) service_get_by_username(username string) ?User { } defer { - db.close() + db.close() or { panic(err) } } results := sql db { @@ -89,7 +84,7 @@ fn (mut app App) service_get_by_username(username string) ?User { } if results.len == 0 { - return error('Usuário não encontrado') + return error('User not found') } return results[0]