# Repositories

This crate is a list of repositories. Currently, we have one repository, the users repository. It has simple CRUD functions, which allows you to manage the users of your app.

We've also added other functions, like add_stripe_customer_id_to_user_by_email which adds, at the end of a Stripe session, the customer's ID to the corresponding user in the users table.

# Use a repository

To use a repository, you should first add the crate to the crate you want to use the repository in (as explained in Getting Started). Then you should be able to use it this way:

use repositories::user;
use repositories::user::BodyUser;

fn main() {
  let user = user::UserRepository::create(&state.db, BodyUser {
    first_name: form.first_name.unwrap(),
    last_name: form.last_name.unwrap(),
    email: form.email.unwrap(),
    password: hash_password(&form.password.unwrap()).await.unwrap(),
  })
    .await?;
}

# Crate content

# Structs

#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct User {
  pub id: i32,
  pub first_name: String,
  pub last_name: String,
  pub email: String,
  pub password: String,
  #[serde(with = "time::serde::rfc3339")]
  pub created_at: OffsetDateTime,
  pub stripe_customer_id: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct BodyUser {
  pub first_name: String,
  pub last_name: String,
  pub email: String,
  pub password: String,
}

# Methods

pub async fn fetch_one(conn: &PgPool, user_id: &i32) -> Result<User, RustSaaSError>
pub async fn fetch_one_by_email(conn: &PgPool, email: &String) -> Result<User, RustSaaSError>
pub async fn create(conn: &PgPool, user: BodyUser) -> Result<User, RustSaaSError>
pub async fn add_stripe_customer_id_to_user_by_email(conn: &PgPool, email: &String, stripe_customer_id: &String) -> Result<User, RustSaaSError>
pub async fn delete(conn: &PgPool, user_id: &i32) -> Result<PgQueryResult, RustSaaSError>
pub async fn fetch_all(conn: &PgPool) -> Result<Vec<User>, RustSaaSError>