diff --git a/src/models/book.rs b/src/models/book.rs index e2a33e7..e8a5898 100644 --- a/src/models/book.rs +++ b/src/models/book.rs @@ -149,6 +149,18 @@ impl BookOperator { .context(DBSnafu) } + /// Finds vec of book by its Owner + pub async fn find_all_by_current_holder( + &self, + current_holder_id: i32, + ) -> Result, BookError> { + Entity::find() + .filter(Column::CurrentHolderId.eq(current_holder_id)) + .all(&self.state.db) + .await + .context(DBSnafu) + } + /// Creates a new book from the given form data. pub async fn create(&self, form: BookForm) -> Result { let book = ActiveModel { diff --git a/src/models/user.rs b/src/models/user.rs index 0699e42..bdf2624 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -1,4 +1,5 @@ -use crate::models::book::BookOperator; +use crate::models::book; +use crate::routes::book::BookForm; use crate::routes::user::UserForm; use crate::state::AppState; use crate::state::error::UserSnafu; @@ -38,6 +39,8 @@ pub enum UserError { DB { source: sea_orm::DbErr }, #[snafu(display("User with id {id} not found"))] NotFound { id: i32 }, + #[snafu(display("Book error"))] + Book { source: super::book::BookError }, } #[derive(Debug)] @@ -90,7 +93,46 @@ impl UserOperator { } } + /// Delete user by ID. + /// Before deleting the user, you must search for all the books they own in order to delete them beforehand, + /// then search for all the books they have borrowed in order to update the current holder to None. pub async fn delete(&self, user_id: i32) -> Result { + // get all + let owner_books = book::BookOperator::new(self.state.clone()) + .find_all_by_owner(user_id) + .await + .context(BookSnafu)?; + + // Delete all book with owner_id = current_user + for owner_book in owner_books { + book::BookOperator::new(self.state.clone()) + .delete(owner_book.id) + .await + .context(BookSnafu)?; + } + + let current_holder_books = book::BookOperator::new(self.state.clone()) + .find_all_by_current_holder(user_id) + .await + .context(BookSnafu)?; + + // Update all book with current Holder = current user + for current_holder_book in current_holder_books { + let form = BookForm { + title: current_holder_book.title, + authors: current_holder_book.authors, + owner_id: current_holder_book.owner_id, + description: current_holder_book.description, + comment: current_holder_book.comment, + current_holder_id: None, + }; + + book::BookOperator::new(self.state.clone()) + .update(current_holder_book.id, form) + .await + .context(BookSnafu)?; + } + let user: Option = Entity::find_by_id(user_id) .one(&self.state.db) .await diff --git a/src/state/error.rs b/src/state/error.rs index 9a316be..e3b6a39 100644 --- a/src/state/error.rs +++ b/src/state/error.rs @@ -5,10 +5,7 @@ use log::error; use snafu::prelude::*; use crate::{ - models::{ - book::{BookError, NotFoundSnafu}, - user::UserError, - }, + models::{book::BookError, user::UserError}, state::config::ConfigError, }; diff --git a/templates/base.html b/templates/base.html index 5e2038d..58b2ef0 100644 --- a/templates/base.html +++ b/templates/base.html @@ -28,5 +28,4 @@ - diff --git a/templates/components/dropdown.html b/templates/components/dropdown.html index ecd95a7..37dc290 100644 --- a/templates/components/dropdown.html +++ b/templates/components/dropdown.html @@ -22,10 +22,29 @@ {% endif %}
  • Edit
  • -
    - -
    + Delete
  • + + + {% endmacro %}