File size: 4,266 Bytes
ac4facb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | ```rust
use actix_web::{web, App, HttpServer, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
use std::sync::Mutex;
use std::collections::HashMap;
// Data models
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Welder {
id: u32,
name: String,
articles: Vec<Article>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Article {
id: u32,
code: String,
quantity: u32,
completed: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Plan {
id: u32,
article_code: String,
total_quantity: u32,
completed_quantity: u32,
assignments: HashMap<u32, u32>, // welder_id -> quantity
}
#[derive(Debug, Serialize, Deserialize)]
struct Norm {
article_code: String,
time_norm: f32, // hours per unit
}
// App state
struct AppState {
welders: Mutex<Vec<Welder>>,
plans: Mutex<Vec<Plan>>,
norms: Mutex<Vec<Norm>>,
next_welder_id: Mutex<u32>,
next_article_id: Mutex<u32>,
next_plan_id: Mutex<u32>,
}
// REST API handlers
async fn get_welders(data: web::Data<AppState>) -> impl Responder {
let welders = data.welders.lock().unwrap();
HttpResponse::Ok().json(welders.clone())
}
async fn add_welder(data: web::Data<AppState>, welder: web::Json<Welder>) -> impl Responder {
let mut welders = data.welders.lock().unwrap();
let mut next_id = data.next_welder_id.lock().unwrap();
let new_welder = Welder {
id: *next_id,
name: welder.name.clone(),
articles: vec![],
};
*next_id += 1;
welders.push(new_welder.clone());
HttpResponse::Created().json(new_welder)
}
async fn get_plans(data: web::Data<AppState>) -> impl Responder {
let plans = data.plans.lock().unwrap();
HttpResponse::Ok().json(plans.clone())
}
async fn add_plan(data: web::Data<AppState>, plan: web::Json<Plan>) -> impl Responder {
let mut plans = data.plans.lock().unwrap();
let mut next_id = data.next_plan_id.lock().unwrap();
let mut new_plan = plan.into_inner();
new_plan.id = *next_id;
*next_id += 1;
plans.push(new_plan.clone());
HttpResponse::Created().json(new_plan)
}
async fn get_norms(data: web::Data<AppState>) -> impl Responder {
let norms = data.norms.lock().unwrap();
HttpResponse::Ok().json(norms.clone())
}
async fn add_norm(data: web::Data<AppState>, norm: web::Json<Norm>) -> impl Responder {
let mut norms = data.norms.lock().unwrap();
norms.push(norm.into_inner());
HttpResponse::Created().json(norms.last().unwrap().clone())
}
async fn assign_article(
data: web::Data<AppState>,
path: web::Path<(u32,)>,
article: web::Json<Article>,
) -> impl Responder {
let (welder_id,) = path.into_inner();
let mut welders = data.welders.lock().unwrap();
let mut next_article_id = data.next_article_id.lock().unwrap();
if let Some(welder) = welders.iter_mut().find(|w| w.id == welder_id) {
let new_article = Article {
id: *next_article_id,
code: article.code.clone(),
quantity: article.quantity,
completed: 0,
};
*next_article_id += 1;
welder.articles.push(new_article.clone());
HttpResponse::Created().json(new_article)
} else {
HttpResponse::NotFound().json("Welder not found")
}
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
// Initialize app state
let app_state = web::Data::new(AppState {
welders: Mutex::new(vec![]),
plans: Mutex::new(vec![]),
norms: Mutex::new(vec![]),
next_welder_id: Mutex::new(1),
next_article_id: Mutex::new(1),
next_plan_id: Mutex::new(1),
});
// Start HTTP server
HttpServer::new(move || {
App::new()
.app_data(app_state.clone())
.route("/welders", web::get().to(get_welders))
.route("/welders", web::post().to(add_welder))
.route("/welders/{id}/articles", web::post().to(assign_article))
.route("/plans", web::get().to(get_plans))
.route("/plans", web::post().to(add_plan))
.route("/norms", web::get().to(get_norms))
.route("/norms", web::post().to(add_norm))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
``` |