Skip to content

Commit

Permalink
schedule progress
Browse files Browse the repository at this point in the history
  • Loading branch information
manglemix committed Jan 12, 2025
1 parent 7f4ccfa commit b8cf776
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 54 deletions.
11 changes: 5 additions & 6 deletions bruno/New Order.bru
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ post {

body:json {
{
"name": "Chicken",
"count": 40,
"unit_cost": 1000.43,
"store_in": "MEB 2350",
"team": "Software",
"reason": "Machine learning... maybe"
"name": "Coruscant",
"count": 2,
"unit_cost": 23.2,
"team": "Mechanical",
"reason": "Some good reason"
}
}
9 changes: 7 additions & 2 deletions usr-backend/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
io::{LineWriter, Write}, net::SocketAddr, path::Path, sync::Arc
backtrace::Backtrace, io::{LineWriter, Write}, net::SocketAddr, panic::set_hook, path::Path, sync::Arc
};

use axum::{routing::get, Router};
Expand All @@ -10,7 +10,7 @@ use sea_orm::{Database, DatabaseConnection};
use serde::Deserialize;
use tower::ServiceBuilder;
use tower_http::cors::Any;
use tracing::info;
use tracing::{error, info};
use tracing_subscriber::FmtSubscriber;

mod scheduler;
Expand Down Expand Up @@ -74,6 +74,11 @@ async fn main() -> anyhow::Result<()> {
})
.init();

set_hook(Box::new(|info| {
let backtrace = Backtrace::capture();
error!("{}\n{backtrace}", info);
}));

let db = Database::connect("sqlite://usr-db.sqlite?mode=rwc").await?;
let config: Config = serde_json::from_reader(std::fs::File::open("config.json")?)?;

Expand Down
29 changes: 16 additions & 13 deletions usr-backend/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ struct PendingSchedule {

#[axum::debug_handler]
async fn add_schedule(State(state): State<Arc<UsrState>>, Json(pending_schedule): Json<PendingSchedule>) -> (StatusCode, &'static str) {
if pending_schedule.name.is_empty() {
return (StatusCode::BAD_REQUEST, "");
}
let result = state.db.transaction(|tx| Box::pin(async move {
for time in pending_schedule.times {
let active_model = availability::ActiveModel {
availability::Entity::insert(availability::ActiveModel {
name: ActiveValue::Set(pending_schedule.name.clone()),
time: ActiveValue::Set(time),
};
active_model.insert(tx).await?;
}).on_conflict_do_nothing().exec(tx).await?;
}
Result::<_, sea_orm::DbErr>::Ok(())
})).await;
Expand All @@ -41,13 +43,15 @@ async fn add_schedule(State(state): State<Arc<UsrState>>, Json(pending_schedule)

#[axum::debug_handler]
async fn del_schedule(State(state): State<Arc<UsrState>>, Json(pending_schedule): Json<PendingSchedule>) -> (StatusCode, &'static str) {
if pending_schedule.name.is_empty() {
return (StatusCode::BAD_REQUEST, "");
}
let result = state.db.transaction(|tx| Box::pin(async move {
for time in pending_schedule.times {
let active_model = availability::ActiveModel {
name: ActiveValue::Set(pending_schedule.name.clone()),
time: ActiveValue::Set(time),
};
active_model.delete(tx).await?;
availability::Entity::delete(availability::ActiveModel {
name: ActiveValue::Unchanged(pending_schedule.name.clone()),
time: ActiveValue::Unchanged(time),
}).exec(tx).await?;
}
Result::<_, sea_orm::DbErr>::Ok(())
})).await;
Expand All @@ -68,6 +72,9 @@ struct SetTeam {

#[axum::debug_handler]
async fn set_teams(State(state): State<Arc<UsrState>>, Json(set_team): Json<SetTeam>) -> (StatusCode, &'static str) {
if set_team.name.is_empty() {
return (StatusCode::BAD_REQUEST, "");
}
let result = state.db.transaction(|tx| Box::pin(async move {
team::Entity::delete_many().filter(team::Column::Name.eq(set_team.name.clone())).exec(tx).await?;
for team in set_team.teams {
Expand Down Expand Up @@ -136,11 +143,7 @@ async fn get_schedule(State(state): State<Arc<UsrState>>) -> Response {
availabilities: {
let mut out: Box<[Vec<String>]> = std::iter::from_fn(|| Some(Vec::default())).take(7 * 8 * 4).collect();
for model in availabilities {
let hour_of_day = model.time % 24 * 4;
if hour_of_day < 9 * 4 || hour_of_day >= 17 * 4 {
continue;
}
out[model.time as usize - 9 * 4].push(model.name);
out[model.time as usize].push(model.name);
}
out
},
Expand Down
Loading

0 comments on commit b8cf776

Please sign in to comment.