1
0
mirror of https://github.com/ellmau/adf-obdd.git synced 2025-12-20 09:39:38 +01:00

Add Delete and Index endpoints for ADFs

This commit is contained in:
monsterkrampe 2023-04-03 11:00:42 +02:00
parent 7e18b6194f
commit 46ad9eb3fa
No known key found for this signature in database
GPG Key ID: B8ADC1F5A5CE5057
2 changed files with 97 additions and 12 deletions

View File

@ -7,12 +7,13 @@ use actix_identity::Identity;
use actix_web::rt::spawn; use actix_web::rt::spawn;
use actix_web::rt::task::spawn_blocking; use actix_web::rt::task::spawn_blocking;
use actix_web::rt::time::timeout; use actix_web::rt::time::timeout;
use actix_web::{get, post, put, web, HttpMessage, HttpRequest, HttpResponse, Responder}; use actix_web::{delete, get, post, put, web, HttpMessage, HttpRequest, HttpResponse, Responder};
use adf_bdd::datatypes::adf::VarContainer; use adf_bdd::datatypes::adf::VarContainer;
use adf_bdd::datatypes::{BddNode, Term, Var}; use adf_bdd::datatypes::{BddNode, Term, Var};
use futures_util::FutureExt; use futures_util::{FutureExt, TryStreamExt};
use mongodb::bson::doc; use mongodb::bson::doc;
use mongodb::bson::{to_bson, Bson}; use mongodb::bson::{to_bson, Bson};
use mongodb::results::DeleteResult;
use names::{Generator, Name}; use names::{Generator, Name};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -372,17 +373,17 @@ async fn add_adf_problem(
} }
}; };
let ac_and_graph = AcAndGraph {
ac: lib_adf.ac.iter().map(|t| t.0.to_string()).collect(),
graph: lib_adf.into_double_labeled_graph(None),
};
app_state app_state
.currently_running .currently_running
.lock() .lock()
.unwrap() .unwrap()
.remove(&running_info); .remove(&running_info);
let ac_and_graph = AcAndGraph {
ac: lib_adf.ac.iter().map(|t| t.0.to_string()).collect(),
graph: lib_adf.into_double_labeled_graph(None),
};
Ok::<_, &str>((SimplifiedAdf::from_lib_adf(lib_adf), ac_and_graph)) Ok::<_, &str>((SimplifiedAdf::from_lib_adf(lib_adf), ac_and_graph))
}), }),
); );
@ -466,9 +467,11 @@ async fn solve_adf_problem(
SimplifiedAdfOpt::None => { SimplifiedAdfOpt::None => {
return HttpResponse::BadRequest().body("The ADF problem has not been parsed yet.") return HttpResponse::BadRequest().body("The ADF problem has not been parsed yet.")
} }
SimplifiedAdfOpt::Error(err) => return HttpResponse::BadRequest().body(format!( SimplifiedAdfOpt::Error(err) => {
"The ADF problem could not be parsed. Update it and try parsing it again. Error: {err}" return HttpResponse::BadRequest().body(format!(
)), "The ADF problem could not be parsed. Update it and try again. Error: {err}"
))
}
SimplifiedAdfOpt::Some(adf) => adf, SimplifiedAdfOpt::Some(adf) => adf,
}; };
@ -611,3 +614,80 @@ async fn get_adf_problem(
&app_state.currently_running.lock().unwrap(), &app_state.currently_running.lock().unwrap(),
)) ))
} }
#[delete("/{problem_name}")]
async fn delete_adf_problem(
app_state: web::Data<AppState>,
identity: Option<Identity>,
path: web::Path<String>,
) -> impl Responder {
let problem_name = path.into_inner();
let adf_coll: mongodb::Collection<AdfProblem> = app_state
.mongodb_client
.database(DB_NAME)
.collection(ADF_COLL);
let username = match identity.map(|id| id.id()) {
None => {
return HttpResponse::Unauthorized().body("You need to login to get an ADF problem.")
}
Some(Err(err)) => return HttpResponse::InternalServerError().body(err.to_string()),
Some(Ok(username)) => username,
};
match adf_coll
.delete_one(doc! { "name": &problem_name, "username": &username }, None)
.await
{
Ok(DeleteResult {
deleted_count: 0, ..
}) => HttpResponse::InternalServerError().body("Adf Problem could not be deleted."),
Ok(DeleteResult {
deleted_count: 1, ..
}) => HttpResponse::Ok().body("Adf Problem deleted."),
Ok(_) => {
unreachable!("delete_one removes at most one entry so all cases are covered already")
}
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
}
}
#[get("/")]
async fn get_adf_problems_for_user(
app_state: web::Data<AppState>,
identity: Option<Identity>,
) -> impl Responder {
let adf_coll: mongodb::Collection<AdfProblem> = app_state
.mongodb_client
.database(DB_NAME)
.collection(ADF_COLL);
let username = match identity.map(|id| id.id()) {
None => {
return HttpResponse::Unauthorized().body("You need to login to get an ADF problem.")
}
Some(Err(err)) => return HttpResponse::InternalServerError().body(err.to_string()),
Some(Ok(username)) => username,
};
let adf_problem_cursor = match adf_coll.find(doc! { "username": &username }, None).await {
Err(err) => return HttpResponse::InternalServerError().body(err.to_string()),
Ok(cursor) => cursor,
};
let adf_problems: Vec<AdfProblemInfo> = match adf_problem_cursor
.map_ok(|adf_problem| {
AdfProblemInfo::from_adf_prob_and_tasks(
adf_problem,
&app_state.currently_running.lock().unwrap(),
)
})
.try_collect()
.await
{
Err(err) => return HttpResponse::InternalServerError().body(err.to_string()),
Ok(probs) => probs,
};
HttpResponse::Ok().json(adf_problems)
}

View File

@ -17,7 +17,10 @@ mod adf;
mod config; mod config;
mod user; mod user;
use adf::{add_adf_problem, get_adf_problem, solve_adf_problem}; use adf::{
add_adf_problem, delete_adf_problem, get_adf_problem, get_adf_problems_for_user,
solve_adf_problem,
};
use config::{AppState, ASSET_DIRECTORY, COOKIE_DURATION}; use config::{AppState, ASSET_DIRECTORY, COOKIE_DURATION};
use user::{ use user::{
create_username_index, delete_account, login, logout, register, update_user, user_info, create_username_index, delete_account, login, logout, register, update_user, user_info,
@ -83,7 +86,9 @@ async fn main() -> std::io::Result<()> {
web::scope("/adf") web::scope("/adf")
.service(add_adf_problem) .service(add_adf_problem)
.service(solve_adf_problem) .service(solve_adf_problem)
.service(get_adf_problem), .service(get_adf_problem)
.service(delete_adf_problem)
.service(get_adf_problems_for_user),
) )
// this mus be last to not override anything // this mus be last to not override anything
.service(fs::Files::new("/", ASSET_DIRECTORY).index_file("index.html")) .service(fs::Files::new("/", ASSET_DIRECTORY).index_file("index.html"))