Skip to content

Commit

Permalink
Added PostgreSQL tables for users, roles, and users_roles, including …
Browse files Browse the repository at this point in the history
…their schemas and models. Lessons 1-12 completed.
  • Loading branch information
juliog922 committed Apr 17, 2024
1 parent 40b660d commit c18f657
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 1 deletion.
1 change: 1 addition & 0 deletions migrations/2024-04-17-191620_create_users/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE TABLE users
6 changes: 6 additions & 0 deletions migrations/2024-04-17-191620_create_users/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(64) NOT NULL UNIQUE,
password VARCHAR(128) NOT NULL,
created_at TIMESTAMP DEFAULT NOW() NOT NULL
)
1 change: 1 addition & 0 deletions migrations/2024-04-17-191631_create_roles/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE TABLE roles
6 changes: 6 additions & 0 deletions migrations/2024-04-17-191631_create_roles/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE roles (
id SERIAL PRIMARY KEY,
code VARCHAR(64) NOT NULL UNIQUE,
name VARCHAR(128) NOT NULL,
created_at TIMESTAMP DEFAULT NOW() NOT NULL
)
1 change: 1 addition & 0 deletions migrations/2024-04-17-191807_create_users_roles/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE TABLE users_roles
5 changes: 5 additions & 0 deletions migrations/2024-04-17-191807_create_users_roles/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE users_roles (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id),
role_id INTEGER NOT NULL REFERENCES roles(id)
)
54 changes: 53 additions & 1 deletion src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,56 @@ pub struct NewCrate {
pub name: String,
pub version: String,
pub description: Option<String>
}
}

#[derive(Queryable, Serialize, Deserialize)]
pub struct User {
#[serde(skip_deserializing)]
pub id: i32,
pub username: String,
pub password: String,
#[serde(skip_deserializing)]
pub created_at: NaiveDateTime
}

#[derive(Insertable, Deserialize)]
#[diesel(table_name=users)]
pub struct NewUser {
pub username: String,
pub password: String
}

#[derive(Queryable, Serialize, Deserialize)]
pub struct Role {
#[serde(skip_deserializing)]
pub id: i32,
pub code: String,
pub name: String,
#[serde(skip_deserializing)]
pub created_at: NaiveDateTime
}

#[derive(Insertable, Deserialize)]
#[diesel(table_name=roles)]
pub struct NewRole {
pub code: String,
pub name: String
}

#[derive(Queryable, Serialize, Deserialize)]
#[diesel(belongs_to(User))]
#[diesel(belongs_to(Role))]
pub struct UserRole {
#[serde(skip_deserializing)]
pub id: i32,
pub user_id: i32,
pub role_id: i32
}

#[derive(Insertable, Deserialize)]
#[diesel(table_name=users_roles)]
pub struct NewUserRole {
pub user_id: i32,
pub role_id: i32
}

35 changes: 35 additions & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ diesel::table! {
}
}

diesel::table! {
roles (id) {
id -> Int4,
#[max_length = 64]
code -> Varchar,
#[max_length = 128]
name -> Varchar,
created_at -> Timestamp,
}
}

diesel::table! {
rustaceans (id) {
id -> Int4,
Expand All @@ -24,9 +35,33 @@ diesel::table! {
}
}

diesel::table! {
users (id) {
id -> Int4,
#[max_length = 64]
username -> Varchar,
#[max_length = 128]
password -> Varchar,
created_at -> Timestamp,
}
}

diesel::table! {
users_roles (id) {
id -> Int4,
user_id -> Int4,
role_id -> Int4,
}
}

diesel::joinable!(crates -> rustaceans (rustacean_id));
diesel::joinable!(users_roles -> roles (role_id));
diesel::joinable!(users_roles -> users (user_id));

diesel::allow_tables_to_appear_in_same_query!(
crates,
roles,
rustaceans,
users,
users_roles,
);

0 comments on commit c18f657

Please sign in to comment.