Woolfich's picture
теперь логику на rust
ac4facb verified
```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
}
```