Make model query modules consistent
This commit is contained in:
parent
012a3175bb
commit
f527e73328
10 changed files with 37 additions and 48 deletions
|
@ -46,7 +46,7 @@ pub enum AdminCommand {
|
|||
async fn handle_create_user(db_url: &str, new_user: NewUser) -> Result<()> {
|
||||
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);
|
||||
|
||||
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<()> {
|
||||
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());
|
||||
for user in users {
|
||||
|
|
|
@ -98,7 +98,7 @@ pub async fn create_document_submit(
|
|||
"".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");
|
||||
|
||||
Ok(Redirect::to("/documents").into_response())
|
||||
|
@ -128,7 +128,7 @@ pub async fn edit_document_page(
|
|||
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 =
|
||||
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()));
|
||||
}
|
||||
|
||||
documents::query::update(
|
||||
documents::q::update(
|
||||
&mut db,
|
||||
&document_id.to_string(),
|
||||
form.title.to_owned(),
|
||||
|
|
|
@ -78,9 +78,9 @@ pub async fn create_project_submit(
|
|||
);
|
||||
// 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)?;
|
||||
|
||||
Ok(Redirect::to("/projects").into_response())
|
||||
|
|
|
@ -3,7 +3,6 @@ use thiserror::Error;
|
|||
pub mod documents;
|
||||
pub mod project_memberships;
|
||||
pub mod projects;
|
||||
pub mod types;
|
||||
pub mod users;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
|
|
|
@ -38,41 +38,41 @@ impl NewDocument {
|
|||
}
|
||||
}
|
||||
|
||||
pub mod query {
|
||||
pub mod q {
|
||||
use super::*;
|
||||
|
||||
pub fn create(
|
||||
db: &mut SqliteConnection,
|
||||
conn: &mut SqliteConnection,
|
||||
new_document: NewDocument,
|
||||
) -> 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)
|
||||
}
|
||||
|
||||
pub fn update(
|
||||
db: &mut SqliteConnection,
|
||||
conn: &mut SqliteConnection,
|
||||
document_id: &str,
|
||||
title: String,
|
||||
content: String,
|
||||
) -> Result<Document, DbError> {
|
||||
diesel::update(dsl::documents.filter(dsl::id.eq(document_id)))
|
||||
.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)
|
||||
}
|
||||
|
||||
pub fn by_id(
|
||||
db: &mut SqliteConnection,
|
||||
conn: &mut SqliteConnection,
|
||||
document_id: &str,
|
||||
) -> Result<Option<Document>, DbError> {
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -65,13 +65,13 @@ pub struct NewProjectMembership {
|
|||
pub role: ProjectRole,
|
||||
}
|
||||
|
||||
pub mod query {
|
||||
pub mod q {
|
||||
use diesel::SqliteConnection;
|
||||
|
||||
use super::*;
|
||||
|
||||
pub fn create(
|
||||
db: &mut SqliteConnection,
|
||||
conn: &mut SqliteConnection,
|
||||
user_id: &str,
|
||||
project_id: &str,
|
||||
role: ProjectRole,
|
||||
|
@ -85,7 +85,7 @@ pub mod query {
|
|||
};
|
||||
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -32,22 +32,22 @@ impl NewProject {
|
|||
}
|
||||
}
|
||||
|
||||
pub mod query {
|
||||
pub mod q {
|
||||
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 =
|
||||
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)
|
||||
}
|
||||
|
||||
pub fn create(
|
||||
db: &mut SqliteConnection,
|
||||
conn: &mut SqliteConnection,
|
||||
new_project: NewProject,
|
||||
) -> Result<Project, diesel::result::Error> {
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
|
|
@ -57,33 +57,28 @@ impl NewUser {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct Query<'a> {
|
||||
db: &'a mut SqliteConnection,
|
||||
}
|
||||
impl<'a> Query<'a> {
|
||||
pub fn new(db: &'a mut SqliteConnection) -> Self {
|
||||
Self { db }
|
||||
}
|
||||
pub mod q {
|
||||
use super::*;
|
||||
|
||||
pub fn all(&mut self) -> Result<Vec<User>, DbError> {
|
||||
let user_list = dsl::users.load::<User>(self.db)?;
|
||||
pub fn all(conn: &mut SqliteConnection) -> Result<Vec<User>, DbError> {
|
||||
let user_list = dsl::users.load::<User>(conn)?;
|
||||
Ok(user_list)
|
||||
}
|
||||
|
||||
pub fn by_id(&mut self, id: &str) -> Result<User, DbError> {
|
||||
let user = dsl::users.filter(dsl::id.eq(id)).first::<User>(self.db)?;
|
||||
pub fn by_id(conn: &mut SqliteConnection, id: &str) -> Result<User, DbError> {
|
||||
let user = dsl::users.filter(dsl::id.eq(id)).first::<User>(conn)?;
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
pub fn by_username(&mut self, username: &str) -> Result<User, DbError> {
|
||||
let user = dsl::users.filter(dsl::username.eq(username)).first::<User>(self.db)?;
|
||||
pub fn by_username(conn: &mut SqliteConnection, username: &str) -> Result<User, DbError> {
|
||||
let user = dsl::users.filter(dsl::username.eq(username)).first::<User>(conn)?;
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
pub fn create(&mut self, new_user: NewUser) -> Result<User, DbError> {
|
||||
let _ = diesel::insert_into(dsl::users).values(&new_user).execute(self.db)?;
|
||||
pub fn create(conn: &mut SqliteConnection, new_user: NewUser) -> Result<User, DbError> {
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -34,9 +34,7 @@ impl AuthnBackend for Provider {
|
|||
creds: Self::Credentials,
|
||||
) -> Result<Option<Self::User>, Self::Error> {
|
||||
let mut db = self.db_pool.get()?;
|
||||
let mut q = users::Query::new(&mut db);
|
||||
|
||||
let user = q.by_username(&creds.username)?;
|
||||
let user = users::q::by_username(&mut db, &creds.username)?;
|
||||
|
||||
if password::verify(&user.password_hash, &creds.password) {
|
||||
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> {
|
||||
let mut db = self.db_pool.get()?;
|
||||
let mut q = users::Query::new(&mut db);
|
||||
|
||||
let user = q.by_id(user_id)?;
|
||||
let user = users::q::by_id(&mut db, user_id)?;
|
||||
|
||||
Ok(Some(user))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue