Add types for users

This commit is contained in:
Ethan Girouard 2024-01-28 15:29:23 -05:00
parent ec9b515484
commit 770dd4d2cd
Signed by: eta357
GPG Key ID: 7BCDC36DFD11C146
2 changed files with 52 additions and 0 deletions

View File

@ -3,8 +3,15 @@ pub mod songdata;
pub mod playstatus; pub mod playstatus;
pub mod playbar; pub mod playbar;
pub mod database; pub mod database;
pub mod models;
use cfg_if::cfg_if; use cfg_if::cfg_if;
cfg_if! {
if #[cfg(feature = "ssr")] {
pub mod schema;
}
}
cfg_if! { cfg_if! {
if #[cfg(feature = "hydrate")] { if #[cfg(feature = "hydrate")] {

45
src/models.rs Normal file
View File

@ -0,0 +1,45 @@
use std::time::SystemTime;
use serde::{Deserialize, Serialize};
#[cfg(feature = "ssr")]
use diesel::prelude::*;
// These "models" are used to represent the data in the database
// Diesel uses these models to generate the SQL queries that are used to interact with the database.
// These types are also used for API endpoints, for consistency. Because the file must be compiled
// for both the server and the client, we use the `cfg_attr` attribute to conditionally add
// diesel-specific attributes to the models when compiling for the server
/// Model for a "User", used for querying the database
#[cfg_attr(feature = "ssr", derive(Queryable, Selectable))]
#[cfg_attr(feature = "ssr", diesel(table_name = crate::schema::users))]
#[cfg_attr(feature = "ssr", diesel(check_for_backend(diesel::pg::Pg)))]
#[derive(Serialize, Deserialize)]
pub struct User {
/// A unique id for the user
pub id: i32,
/// The user's username
pub username: String,
/// The user's email
pub email: String,
/// The user's password, stored as a hash
pub password: String,
/// The time the user was created
pub created_at: SystemTime,
}
/// Model for a "New User", used for inserting into the database
/// Note that this model does not have an id or created_at field, as those are automatically
/// generated by the database and we don't want to deal with them ourselves
#[cfg_attr(feature = "ssr", derive(Insertable))]
#[cfg_attr(feature = "ssr", diesel(table_name = crate::schema::users))]
#[cfg_attr(feature = "ssr", diesel(check_for_backend(diesel::pg::Pg)))]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct NewUser {
/// The user's username
pub username: String,
/// The user's email
pub email: String,
/// The user's password, stored as a hash
pub password: String,
}