69 lines
1.8 KiB
69 lines
1.8 KiB
use u_lib::contracts::*; |
|
use warp::{ |
|
Rejection, |
|
Reply, |
|
}; |
|
|
|
pub async fn add_client( |
|
msg: Message<'_, ClientInfo>, |
|
db: Storage) -> Result<impl Reply, Rejection> |
|
{ |
|
let new_cli = msg.item; |
|
let mut clients = db.lock().await; |
|
if clients.contains_key(&new_cli.id) { |
|
Ok(warp::reply::json( |
|
&RawMsg("Already exist".to_string()).as_message() |
|
)) |
|
} else { |
|
clients.insert( |
|
new_cli.id.clone(), |
|
UClient::new(new_cli.into_owned()) |
|
); |
|
Ok(warp::reply::json( |
|
&RawMsg("Added".to_string()).as_message() |
|
)) |
|
} |
|
} |
|
|
|
pub async fn get_jobs( |
|
db: Storage) -> Result<impl Reply, Rejection> |
|
{ |
|
let mut clients = db.lock().await; |
|
let cli = clients.get_mut(&msg.id).unwrap(); |
|
cli.jobs.iter_mut().for_each(|job: &mut JobMeta| { |
|
if job.state == JobState::Queued { |
|
job.state = JobState::Pending |
|
} |
|
}); |
|
Ok(warp::reply::json( |
|
&Message::new(cli.jobs.clone()) |
|
)) |
|
} |
|
|
|
pub async fn set_jobs( |
|
uid: Option<Uuid>, |
|
msg: Message<'_, CollectionWrapper<JobStorage>>, |
|
db: Storage) -> Result<impl Reply, Rejection> |
|
{ |
|
let mut clients = db.lock().await; |
|
let cli = clients.get_mut(&uid.unwrap_or(msg.id)).unwrap(); |
|
msg.item.0.into_iter().for_each(|(uuid, job)| { |
|
match cli.jobs.get_mut(&uuid) { |
|
Some(cli_job) => *cli_job = job, |
|
None => cli.jobs.push(job) |
|
}; |
|
}); |
|
Ok(()) |
|
|
|
} |
|
|
|
pub async fn listing(db: Storage) -> Result<impl Reply, Rejection> { |
|
let clients = db.lock().await; |
|
let mut result: Vec<ClientInfo> = Vec::with_capacity(clients.len()); |
|
for cli in clients.values() { |
|
result.push(cli.client_info.clone()); |
|
} |
|
Ok(warp::reply::json( |
|
&CollectionWrapper(result).as_message() |
|
)) |
|
} |