Compare commits

..

5 Commits

Author SHA1 Message Date
25beef5cca fixed compilation errors 2026-01-28 12:47:02 +01:00
e2a3f1a149 added sqlx 2026-01-28 12:25:40 +01:00
f69e2fff56 added bigdecimal type 2026-01-28 12:25:04 +01:00
9d78c98bcb location handler 2026-01-28 11:29:41 +01:00
b3d49869b0 addend models for location 2026-01-28 11:26:15 +01:00
6 changed files with 928 additions and 11 deletions

878
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -7,3 +7,5 @@ edition = "2024"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
actix-web = "4.x.x"
bigdecimal = {version = "0.4.10", features = ["serde"] }
sqlx = "0.8.6"

20
src/handlers/locations.rs Normal file
View 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)
}

View File

@@ -1,12 +1,20 @@
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]
async fn main() -> std::io::Result<()> {
HttpServer::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")?
.run()

View 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
View 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>,
}