snapshot i dunno what this is
This commit is contained in:
parent
924e91354c
commit
03aa8ffea9
1168
Cargo.lock
generated
1168
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -39,8 +39,9 @@ anyhow = "1.0"
|
||||
bcrypt = "0.15.1"
|
||||
|
||||
itertools = "0.13"
|
||||
indoc = "1.0.3"
|
||||
|
||||
indoc = "2.0"
|
||||
rocket = "0.5.0-rc.1"
|
||||
rocket_contrib = "0.4.10"
|
||||
|
||||
[dev-dependencies]
|
||||
tokio-core = "*"
|
||||
|
109
src/api.rs
109
src/api.rs
@ -1,118 +1,29 @@
|
||||
#![feature(proc_macro_hygiene, decl_macro)]
|
||||
|
||||
#[macro_use] extern crate rocket;
|
||||
#[macro_use] extern crate rocket_contrib;
|
||||
#[macro_use] extern crate serde_derive;
|
||||
|
||||
use rocket::http::Status;
|
||||
use rocket::{post, routes, Rocket, State};
|
||||
use rocket_contrib::json::{Json, JsonValue};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::SqlitePool;
|
||||
mod auth;
|
||||
|
||||
use crate::auth::login;
|
||||
|
||||
|
||||
// Define data models
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct User {
|
||||
id: String,
|
||||
username: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Authorization {
|
||||
to_date: String,
|
||||
from_date: String,
|
||||
user: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Election {
|
||||
id: String,
|
||||
username: String,
|
||||
name: String,
|
||||
description: String,
|
||||
start_date: String,
|
||||
end_date: String,
|
||||
items: Vec<ElectionItem>,
|
||||
}
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct ElectionItem {
|
||||
id: String,
|
||||
name: String,
|
||||
}
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct VoteItem {
|
||||
item: ElectionItem,
|
||||
value: f64,
|
||||
}
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Vote {
|
||||
authorization: Authorization,
|
||||
userid: String,
|
||||
data: Vec<VoteItem>,
|
||||
}
|
||||
|
||||
#[post("/auth/login", format = "application/json", data = "<credentials>")]
|
||||
async fn handle_login(credentials: Json<User>, db: Db) -> JsonValue {
|
||||
match login(credentials.email, credentials.password, db).await {
|
||||
pub async fn handle_login(credentials: Json<User>, db: State<SqlitePool>) -> JsonValue {
|
||||
match crate::auth::login(&credentials.username, &credentials.password, db.inner()).await {
|
||||
Ok(token) => json!({
|
||||
"token": token
|
||||
}),
|
||||
Err(error) => json!({
|
||||
"error": error
|
||||
"error": error.to_string()
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[post("/auth/token", format = "application/json", data = "<token>")]
|
||||
fn generate_token(token: Json<Authorization>) -> JsonValue {
|
||||
// Token generation logic here
|
||||
json!({
|
||||
"token": "generated_token"
|
||||
})
|
||||
}
|
||||
|
||||
#[post("/elections/create", format = "application/json", data = "<election>")]
|
||||
fn create_election(election: Json<Election>) -> Result<JsonValue, Status> {
|
||||
// Election creation logic here
|
||||
Ok(json!(election))
|
||||
}
|
||||
|
||||
#[get("/elections/all")]
|
||||
fn get_all_elections() -> JsonValue {
|
||||
// Retrieve all elections logic here
|
||||
json!([
|
||||
// List of all existing elections
|
||||
])
|
||||
}
|
||||
|
||||
#[get("/elections/<id>")]
|
||||
fn get_election(id: String) -> JsonValue {
|
||||
// Retrieve single election logic here
|
||||
json!({
|
||||
"id": id,
|
||||
// Other election details
|
||||
})
|
||||
}
|
||||
|
||||
#[post("/elections/<id>", format = "application/json", data = "<vote>")]
|
||||
fn vote_in_election(id: String, vote: Json<Vote>) -> Result<JsonValue, Status> {
|
||||
// Voting logic here
|
||||
Ok(json!(vote))
|
||||
}
|
||||
|
||||
// Rocket fairings to set up CORS and other middlewares can be added here
|
||||
|
||||
fn main() {
|
||||
rocket::ignite()
|
||||
.mount("/api", routes![
|
||||
login,
|
||||
generate_token,
|
||||
create_election,
|
||||
get_all_elections,
|
||||
get_election,
|
||||
vote_in_election,
|
||||
])
|
||||
.launch();
|
||||
pub fn create_rocket() -> Rocket<Build> {
|
||||
rocket::build()
|
||||
.mount("/", routes![handle_login])
|
||||
}
|
||||
|
27
src/main.rs
27
src/main.rs
@ -1,6 +1,11 @@
|
||||
// main.rs
|
||||
#[macro_use] extern crate rocket;
|
||||
use rocket::http::Status;
|
||||
use rocket_contrib::json::{Json, JsonValue};
|
||||
use serde_json::json;
|
||||
//idk the tests works without rocket
|
||||
|
||||
mod db;
|
||||
mod api;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
@ -183,4 +188,24 @@ mod tests {
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
println!("This is the main function. Run `cargo test` to execute tests.");
|
||||
|
||||
let _ = rocket::build()
|
||||
.mount("/", routes![
|
||||
api::handle_login
|
||||
])
|
||||
.launch()
|
||||
.await;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user