Delete user script
This commit is contained in:
parent
81a9e11774
commit
1f5bc0c14a
@ -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<Vec<Model>, 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<Model, BookError> {
|
||||
let book = ActiveModel {
|
||||
|
||||
@ -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<DeleteResult, UserError> {
|
||||
// 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<Model> = Entity::find_by_id(user_id)
|
||||
.one(&self.state.db)
|
||||
.await
|
||||
|
||||
@ -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,
|
||||
};
|
||||
|
||||
|
||||
@ -28,5 +28,4 @@
|
||||
<script src="/assets/js/bootstrap.min.js"></script>
|
||||
<script src="/assets/js/script.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
||||
@ -22,10 +22,29 @@
|
||||
{% endif %}
|
||||
<li><a class="dropdown-item" href="/{{ sub_path }}/{{ book.id }}/edit">Edit</a></li>
|
||||
<li>
|
||||
<form method="post" action="/{{ sub_path }}/{{ book.id }}/delete" class="mb-0">
|
||||
<input class="dropdown-item" type="submit" value="Delete">
|
||||
</form>
|
||||
<a class="dropdown-item" href="#" data-bs-toggle="modal" data-bs-target="#deleteUserModal{{ book.id }}">Delete</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<!-- Modal -->
|
||||
<div class="modal fade" id="deleteUserModal{{ book.id }}" tabindex="-1" aria-labelledby="deleteUserModal{{ book.id }}Label" aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h1 class="modal-title fs-5" id="exampleModalLabel">Confirmation</h1>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p>Are you sure ?</p>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
||||
<form method="post" action="/{{ sub_path }}/{{ book.id }}/delete" class="m-0">
|
||||
<input class="btn btn-danger" type="submit" value="Delete">
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endmacro %}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user