Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit fe035d6a authored by Pierre Jarriges's avatar Pierre Jarriges
Browse files

service::remove_page

parent aa7f6f7b
No related branches found
No related tags found
No related merge requests found
......@@ -65,7 +65,8 @@ async fn main() -> std::io::Result<()> {
.service(service::files::post_files)
.service(service::files::delete_static_file)
.service(service::add_template)
.service(service::update_template),
.service(service::update_template)
.service(service::remove_page),
),
)
.service(service::files::favicon)
......
use crate::website::{Page, PageTemplate, WebSite};
use crate::AppState;
use actix_web::{
get, post, put,
delete, get, post, put,
web::{Data, Json},
HttpResponse,
};
......@@ -160,3 +160,23 @@ pub async fn update_template(
HttpResponse::Ok().json(updated)
}
#[delete("/remove-page/{id}")]
pub async fn remove_page(
id: actix_web::web::Path<usize>,
app_state: Data<RwLock<AppState>>,
website: Data<RwLock<WebSite>>,
) -> HttpResponse {
let mut website = website
.write()
.expect("Couldn't acquire website write lock");
match website.remove_page(id.into_inner()) {
Ok(()) => {
website
.save(&app_state.read().expect("Couldn't read AppState").config)
.expect("Error saving website to static file");
HttpResponse::Accepted().finish()
}
Err(msg) => HttpResponse::BadRequest().body(msg),
}
}
......@@ -190,17 +190,24 @@ impl WebSite {
None
}
fn find_page_parent_mut<'a>(
page: &'a mut Page,
parent_page: Option<&'a mut Page>,
fn find_page_parent_id<'a>(
page: &'a Page,
parent_id: Option<usize>,
match_id: usize,
) -> Result<&'a mut Page, String> {
) -> Result<usize, String> {
let parent_id = {
if parent_id.is_none() {
return Err("Page must have a parent".to_string());
}
parent_id.unwrap()
};
if page.get_id() == match_id {
return Ok(parent_page.expect("Page must have a parent"));
return Ok(parent_id);
}
for sp in page.sub_pages.iter_mut() {
if let Ok(parent) = Self::find_page_parent_mut(sp, Some(page), match_id) {
for sp in page.sub_pages.iter() {
if let Ok(parent) = Self::find_page_parent_id(sp, Some(page.get_id()), match_id) {
return Ok(parent);
};
}
......@@ -216,8 +223,8 @@ impl WebSite {
Self::find_page_mut(&mut self.root_page, id)
}
fn get_page_parent_mut(&mut self, id: usize) -> Result<&mut Page, String> {
Self::find_page_parent_mut(&mut self.root_page, None, id)
fn get_page_parent_id(&self, id: usize) -> Result<usize, String> {
Self::find_page_parent_id(&self.root_page, None, id)
}
pub fn add_page(&mut self, parent_page_id: usize, page: Page) -> Result<(), String> {
......@@ -329,8 +336,8 @@ impl WebSite {
}
}
fn validate_page_to_remove(&mut self, id: usize) -> Result<&mut Page, String> {
let res = self.get_page_parent_mut(id);
fn validate_page_to_remove(&mut self, id: usize) -> Result<usize, String> {
let res = self.get_page_parent_id(id);
if let Err(msg) = res {
return Err(msg);
}
......@@ -340,8 +347,9 @@ impl WebSite {
pub fn remove_page(&mut self, id: usize) -> Result<(), String> {
match self.validate_page_to_remove(id) {
Ok(parent) => {
parent.sub_pages = parent
Ok(parent_id) => {
let mut parent_page = self.get_page_mut(parent_id).unwrap();
parent_page.sub_pages = parent_page
.sub_pages
.iter()
.filter(|sp| sp.get_id() != id)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment