Compare commits
5 Commits
dd95d8ca28
...
25beef5cca
| Author | SHA1 | Date | |
|---|---|---|---|
| 25beef5cca | |||
| e2a3f1a149 | |||
| f69e2fff56 | |||
| 9d78c98bcb | |||
| b3d49869b0 |
878
Cargo.lock
generated
878
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -7,3 +7,5 @@ edition = "2024"
|
|||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
actix-web = "4.x.x"
|
actix-web = "4.x.x"
|
||||||
|
bigdecimal = {version = "0.4.10", features = ["serde"] }
|
||||||
|
sqlx = "0.8.6"
|
||||||
|
|||||||
20
src/handlers/locations.rs
Normal file
20
src/handlers/locations.rs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
use actix_web::web::Json;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
include!("../models/location.rs");
|
||||||
|
|
||||||
|
async fn get_locations() -> impl Responder {
|
||||||
|
let locations: Vec<Location> = vec![];
|
||||||
|
Json(locations)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create_location(Json(location): Json<Location>) -> impl Responder {
|
||||||
|
Json(location)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update_location(Json(location): Json<Location>) -> impl Responder {
|
||||||
|
Json(location)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete_location(timestamp: i64) -> impl Responder {
|
||||||
|
format!("Deleted location with timestamp: {}", timestamp)
|
||||||
|
}
|
||||||
12
src/main.rs
12
src/main.rs
@@ -1,12 +1,20 @@
|
|||||||
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
|
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
|
||||||
|
include!("handlers/locations.rs");
|
||||||
|
|
||||||
async fn index() -> impl Responder { "Connection successful" }
|
async fn index(req: HttpRequest) -> impl Responder { "Connection successful" }
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
.service(web::resource("/").to(index))
|
.route("/", web::get().to(index))
|
||||||
|
.service(
|
||||||
|
web::scope("/api/v1")
|
||||||
|
.service(web::resource("/locations").to(|| {
|
||||||
|
get_locations()
|
||||||
|
}))
|
||||||
|
.service(web::resource("/locations").route(web::post().to(create_location)))
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8080")?
|
.bind("127.0.0.1:8080")?
|
||||||
.run()
|
.run()
|
||||||
|
|||||||
10
src/models/battery_status.rs
Normal file
10
src/models/battery_status.rs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
use sqlx::Type;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Type)]
|
||||||
|
#[sqlx(type_name = "battery_status", rename_all = "lowercase")]
|
||||||
|
pub enum BatteryStatus {
|
||||||
|
Unknown,
|
||||||
|
Unplugged,
|
||||||
|
Charging,
|
||||||
|
Full,
|
||||||
|
}
|
||||||
15
src/models/location.rs
Normal file
15
src/models/location.rs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
use bigdecimal::BigDecimal;
|
||||||
|
include!("battery_status.rs");
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize)]
|
||||||
|
struct Location {
|
||||||
|
latitude: BigDecimal,
|
||||||
|
longitude: BigDecimal,
|
||||||
|
accuracy: Option<BigDecimal>,
|
||||||
|
altitude: Option<BigDecimal>,
|
||||||
|
velocity: Option<BigDecimal>,
|
||||||
|
battery_level: i16,
|
||||||
|
battery_status: BatteryStatus,
|
||||||
|
timestamp: i64,
|
||||||
|
bearing: Option<BigDecimal>,
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user