Make model query modules consistent

This commit is contained in:
Nicole Tietz-Sokolskaya 2024-06-02 11:29:39 -04:00
parent 012a3175bb
commit f527e73328
10 changed files with 37 additions and 48 deletions

View file

@ -46,7 +46,7 @@ pub enum AdminCommand {
async fn handle_create_user(db_url: &str, new_user: NewUser) -> Result<()> { async fn handle_create_user(db_url: &str, new_user: NewUser) -> Result<()> {
let mut db = establish_connection(db_url); let mut db = establish_connection(db_url);
let user = users::Query::new(&mut db).create(new_user)?; let user = users::q::create(&mut db, new_user)?;
println!("User created successfully with id = {}", user.id); println!("User created successfully with id = {}", user.id);
Ok(()) Ok(())
@ -55,7 +55,7 @@ async fn handle_create_user(db_url: &str, new_user: NewUser) -> Result<()> {
async fn handle_list_users(db_url: &str) -> Result<()> { async fn handle_list_users(db_url: &str) -> Result<()> {
let mut db = establish_connection(db_url); let mut db = establish_connection(db_url);
let users = users::Query::new(&mut db).all()?; let users = users::q::all(&mut db)?;
println!("Found {} users.", users.len()); println!("Found {} users.", users.len());
for user in users { for user in users {

View file

@ -98,7 +98,7 @@ pub async fn create_document_submit(
"".to_owned(), "".to_owned(),
); );
let document = documents::query::create(&mut db, new_document).map_err(internal_error)?; let document = documents::q::create(&mut db, new_document).map_err(internal_error)?;
info!(?document, "document created"); info!(?document, "document created");
Ok(Redirect::to("/documents").into_response()) Ok(Redirect::to("/documents").into_response())
@ -128,7 +128,7 @@ pub async fn edit_document_page(
return Err((StatusCode::FORBIDDEN, "permission denied".to_owned())); return Err((StatusCode::FORBIDDEN, "permission denied".to_owned()));
} }
let document = documents::query::by_id(&mut db, &id.to_string()).map_err(internal_error)?; let document = documents::q::by_id(&mut db, &id.to_string()).map_err(internal_error)?;
let projects = let projects =
permissions::query::accessible_projects(&mut db, &user.id).map_err(internal_error)?; permissions::query::accessible_projects(&mut db, &user.id).map_err(internal_error)?;
@ -172,7 +172,7 @@ pub async fn edit_document_submit(
return Err((StatusCode::FORBIDDEN, "permission denied".to_owned())); return Err((StatusCode::FORBIDDEN, "permission denied".to_owned()));
} }
documents::query::update( documents::q::update(
&mut db, &mut db,
&document_id.to_string(), &document_id.to_string(),
form.title.to_owned(), form.title.to_owned(),

View file

@ -78,9 +78,9 @@ pub async fn create_project_submit(
); );
// TODO: validation // TODO: validation
let project = projects::query::create(&mut db, new_project).map_err(internal_error)?; let project = projects::q::create(&mut db, new_project).map_err(internal_error)?;
let _ = project_memberships::query::create(&mut db, &user.id, &project.id, ProjectRole::Admin) let _ = project_memberships::q::create(&mut db, &user.id, &project.id, ProjectRole::Admin)
.map_err(internal_error)?; .map_err(internal_error)?;
Ok(Redirect::to("/projects").into_response()) Ok(Redirect::to("/projects").into_response())

View file

@ -3,7 +3,6 @@ use thiserror::Error;
pub mod documents; pub mod documents;
pub mod project_memberships; pub mod project_memberships;
pub mod projects; pub mod projects;
pub mod types;
pub mod users; pub mod users;
#[derive(Error, Debug)] #[derive(Error, Debug)]

View file

@ -38,41 +38,41 @@ impl NewDocument {
} }
} }
pub mod query { pub mod q {
use super::*; use super::*;
pub fn create( pub fn create(
db: &mut SqliteConnection, conn: &mut SqliteConnection,
new_document: NewDocument, new_document: NewDocument,
) -> Result<Document, DbError> { ) -> Result<Document, DbError> {
diesel::insert_into(dsl::documents).values(&new_document).execute(db)?; diesel::insert_into(dsl::documents).values(&new_document).execute(conn)?;
let document = dsl::documents.filter(dsl::id.eq(&new_document.id)).first(db)?; let document = dsl::documents.filter(dsl::id.eq(&new_document.id)).first(conn)?;
Ok(document) Ok(document)
} }
pub fn update( pub fn update(
db: &mut SqliteConnection, conn: &mut SqliteConnection,
document_id: &str, document_id: &str,
title: String, title: String,
content: String, content: String,
) -> Result<Document, DbError> { ) -> Result<Document, DbError> {
diesel::update(dsl::documents.filter(dsl::id.eq(document_id))) diesel::update(dsl::documents.filter(dsl::id.eq(document_id)))
.set((dsl::title.eq(title), dsl::content.eq(content))) .set((dsl::title.eq(title), dsl::content.eq(content)))
.execute(db)?; .execute(conn)?;
let document = dsl::documents.filter(dsl::id.eq(document_id)).first(db)?; let document = dsl::documents.filter(dsl::id.eq(document_id)).first(conn)?;
Ok(document) Ok(document)
} }
pub fn by_id( pub fn by_id(
db: &mut SqliteConnection, conn: &mut SqliteConnection,
document_id: &str, document_id: &str,
) -> Result<Option<Document>, DbError> { ) -> Result<Option<Document>, DbError> {
let document = let document =
dsl::documents.filter(dsl::id.eq(document_id)).first::<Document>(db).optional()?; dsl::documents.filter(dsl::id.eq(document_id)).first::<Document>(conn).optional()?;
Ok(document) Ok(document)
} }

View file

@ -65,13 +65,13 @@ pub struct NewProjectMembership {
pub role: ProjectRole, pub role: ProjectRole,
} }
pub mod query { pub mod q {
use diesel::SqliteConnection; use diesel::SqliteConnection;
use super::*; use super::*;
pub fn create( pub fn create(
db: &mut SqliteConnection, conn: &mut SqliteConnection,
user_id: &str, user_id: &str,
project_id: &str, project_id: &str,
role: ProjectRole, role: ProjectRole,
@ -85,7 +85,7 @@ pub mod query {
}; };
let membership = let membership =
diesel::insert_into(pm::project_memberships).values(new_membership).get_result(db)?; diesel::insert_into(pm::project_memberships).values(new_membership).get_result(conn)?;
Ok(membership) Ok(membership)
} }

View file

@ -32,22 +32,22 @@ impl NewProject {
} }
} }
pub mod query { pub mod q {
use super::*; use super::*;
pub fn for_user(db: &mut SqliteConnection, user_id: String) -> Result<Vec<Project>, DbError> { pub fn for_user(conn: &mut SqliteConnection, user_id: String) -> Result<Vec<Project>, DbError> {
let projects = let projects =
dsl::projects.filter(dsl::creator_id.eq(user_id.to_string())).load::<Project>(db)?; dsl::projects.filter(dsl::creator_id.eq(user_id.to_string())).load::<Project>(conn)?;
Ok(projects) Ok(projects)
} }
pub fn create( pub fn create(
db: &mut SqliteConnection, conn: &mut SqliteConnection,
new_project: NewProject, new_project: NewProject,
) -> Result<Project, diesel::result::Error> { ) -> Result<Project, diesel::result::Error> {
use crate::schema::projects::dsl as p; use crate::schema::projects::dsl as p;
let project = diesel::insert_into(p::projects).values(new_project).get_result(db)?; let project = diesel::insert_into(p::projects).values(new_project).get_result(conn)?;
Ok(project) Ok(project)
} }

View file

@ -1 +0,0 @@

View file

@ -57,33 +57,28 @@ impl NewUser {
} }
} }
pub struct Query<'a> { pub mod q {
db: &'a mut SqliteConnection, use super::*;
}
impl<'a> Query<'a> {
pub fn new(db: &'a mut SqliteConnection) -> Self {
Self { db }
}
pub fn all(&mut self) -> Result<Vec<User>, DbError> { pub fn all(conn: &mut SqliteConnection) -> Result<Vec<User>, DbError> {
let user_list = dsl::users.load::<User>(self.db)?; let user_list = dsl::users.load::<User>(conn)?;
Ok(user_list) Ok(user_list)
} }
pub fn by_id(&mut self, id: &str) -> Result<User, DbError> { pub fn by_id(conn: &mut SqliteConnection, id: &str) -> Result<User, DbError> {
let user = dsl::users.filter(dsl::id.eq(id)).first::<User>(self.db)?; let user = dsl::users.filter(dsl::id.eq(id)).first::<User>(conn)?;
Ok(user) Ok(user)
} }
pub fn by_username(&mut self, username: &str) -> Result<User, DbError> { pub fn by_username(conn: &mut SqliteConnection, username: &str) -> Result<User, DbError> {
let user = dsl::users.filter(dsl::username.eq(username)).first::<User>(self.db)?; let user = dsl::users.filter(dsl::username.eq(username)).first::<User>(conn)?;
Ok(user) Ok(user)
} }
pub fn create(&mut self, new_user: NewUser) -> Result<User, DbError> { pub fn create(conn: &mut SqliteConnection, new_user: NewUser) -> Result<User, DbError> {
let _ = diesel::insert_into(dsl::users).values(&new_user).execute(self.db)?; let _ = diesel::insert_into(dsl::users).values(&new_user).execute(conn)?;
let new_user = dsl::users.filter(dsl::id.eq(&new_user.id)).first::<User>(self.db)?; let new_user = dsl::users.filter(dsl::id.eq(&new_user.id)).first::<User>(conn)?;
Ok(new_user) Ok(new_user)
} }

View file

@ -34,9 +34,7 @@ impl AuthnBackend for Provider {
creds: Self::Credentials, creds: Self::Credentials,
) -> Result<Option<Self::User>, Self::Error> { ) -> Result<Option<Self::User>, Self::Error> {
let mut db = self.db_pool.get()?; let mut db = self.db_pool.get()?;
let mut q = users::Query::new(&mut db); let user = users::q::by_username(&mut db, &creds.username)?;
let user = q.by_username(&creds.username)?;
if password::verify(&user.password_hash, &creds.password) { if password::verify(&user.password_hash, &creds.password) {
Ok(Some(user)) Ok(Some(user))
@ -47,9 +45,7 @@ impl AuthnBackend for Provider {
async fn get_user(&self, user_id: &UserId<Self>) -> Result<Option<Self::User>, Self::Error> { async fn get_user(&self, user_id: &UserId<Self>) -> Result<Option<Self::User>, Self::Error> {
let mut db = self.db_pool.get()?; let mut db = self.db_pool.get()?;
let mut q = users::Query::new(&mut db); let user = users::q::by_id(&mut db, user_id)?;
let user = q.by_id(user_id)?;
Ok(Some(user)) Ok(Some(user))
} }