Compare commits

..

No commits in common. "9b876511d967f8363d2f6e468506cafe5975bbb5" and "b81485a3d5690a11bca49685af68cb5daf592ec2" have entirely different histories.

3 changed files with 29 additions and 60 deletions

View file

@ -8,7 +8,10 @@ use crate::error::Error;
use super::error::ApiResult;
#[post("/dump", data = "<sql_dump>")]
pub(crate) async fn sql_dump(db: &State<DatabaseConnection>, sql_dump: &str) -> ApiResult<Status> {
pub(crate) async fn sql_dump(
db: &State<DatabaseConnection>,
sql_dump: &str,
) -> ApiResult<Status> {
for line in sql_dump.lines() {
let line = line.to_ascii_lowercase();
if line.starts_with("insert into")
@ -22,8 +25,11 @@ pub(crate) async fn sql_dump(db: &State<DatabaseConnection>, sql_dump: &str) ->
Ok(Status::Ok)
}
#[post("/", data = "<sqlite_db>")]
pub(crate) async fn db_file(db: &State<DatabaseConnection>, sqlite_db: &[u8]) -> ApiResult<Status> {
#[post("/", data="<sqlite_db>")]
pub(crate) async fn db_file(
db: &State<DatabaseConnection>,
sqlite_db: &[u8],
) -> ApiResult<Status> {
use std::{
io::Write,
process::{Command, Stdio},
@ -45,6 +51,8 @@ pub(crate) async fn db_file(db: &State<DatabaseConnection>, sqlite_db: &[u8]) ->
if result.status.success() {
sql_dump(db, &String::from_utf8(result.stdout).map_err(Error::from)?).await
} else {
Err(Error::SqliteCommandError(String::from_utf8_lossy(&result.stderr).to_string()).into())
Err(Error::SqliteCommandError(String::from_utf8_lossy(
&result.stderr,
).to_string()).into())
}
}

View file

@ -5,6 +5,7 @@ use either::Either::{self, Left, Right};
use rocket::http::Status;
use rocket::{serde::json::Json, State};
use sea_orm::{prelude::*, DatabaseConnection};
use std::default::default;
use tokio::sync::broadcast::Sender;
use super::update::Update;
@ -53,46 +54,37 @@ pub(super) async fn ticks_for_track(
#[post("/", format = "application/json", data = "<track>")]
pub(super) async fn insert_track(
db: &State<DatabaseConnection>,
tx: &State<Sender<Update>>,
track: Json<serde_json::Value>,
) -> ApiResult<Json<tracks::Model>> {
let track = track.0;
let db = db as &DatabaseConnection;
let model = tracks::ActiveModel::from_json(track).map_err(Error::from)?;
let track = model.insert(db).await.map_err(Error::from)?;
tx.send(Update::track_added(track.clone()))
.map_err(Error::from)?;
Ok(Json(track))
let mut model: tracks::ActiveModel = default();
model.set_from_json(track).map_err(Error::from)?;
Ok(Json(model.insert(db).await.map_err(Error::from)?))
}
#[put("/", format = "application/json", data = "<track>")]
pub(super) async fn update_track(
db: &State<DatabaseConnection>,
tx: &State<Sender<Update>>,
track: Json<serde_json::Value>,
) -> ApiResult<Json<tracks::Model>> {
let db = db as &DatabaseConnection;
let track = tracks::ActiveModel::from_json(track.0)
.map_err(Error::from)?
.update(db)
.await
.map_err(Error::from)?;
tx.send(Update::track_changed(track.clone()))
.map_err(Error::from)?;
Ok(Json(track))
Ok(Json(
tracks::ActiveModel::from_json(track.0)
.map_err(Error::from)?
.update(db)
.await
.map_err(Error::from)?,
))
}
#[delete("/<id>")]
pub(super) async fn delete_track(
db: &State<DatabaseConnection>,
tx: &State<Sender<Update>>,
id: i32,
) -> ApiResult<Status> {
pub(super) async fn delete_track(db: &State<DatabaseConnection>, id: i32) -> ApiResult<Status> {
let db = db as &DatabaseConnection;
let Some(track) = Tracks::find_by_id(id).one(db).await.map_err(Error::from)? else {
return Ok(Status::NotFound);
};
tx.send(Update::track_removed(track)).map_err(Error::from)?;
Tracks::delete_by_id(id)
.exec(db)
.await
.map_err(Error::from)?;
Ok(Status::Ok)
}

View file

@ -4,10 +4,7 @@ use serde::{Deserialize, Serialize};
use serde_json::json;
use tokio::sync::broadcast::Sender;
use crate::{
entities::{ticks, tracks},
error::Result,
};
use crate::{entities::ticks, error::Result};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Update {
@ -19,10 +16,6 @@ pub enum Update {
kind: UpdateType,
count: u64,
},
TrackChanged {
kind: UpdateType,
track: tracks::Model,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@ -30,9 +23,6 @@ pub enum Update {
pub enum UpdateType {
TickAdded,
TickDropped,
TrackAdded,
TrackChanged,
TrackDropped,
Error,
}
@ -58,26 +48,6 @@ impl Update {
}
}
pub fn track_added(track: tracks::Model) -> Self {
Self::TrackChanged {
kind: UpdateType::TrackAdded,
track,
}
}
pub fn track_removed(track: tracks::Model) -> Self {
Self::TrackChanged {
kind: UpdateType::TrackDropped,
track,
}
}
pub fn track_changed(track: tracks::Model) -> Self {
Self::TrackChanged {
kind: UpdateType::TrackChanged,
track,
}
}
pub fn to_event(&self) -> Event {
use Update::*;
match self {
@ -86,7 +56,6 @@ impl Update {
Event::json(&json! {{"message": "error: lagged", "count": count}})
.event(format!("{kind:?}"))
}
TrackChanged { kind, track } => Event::json(track).event(format!("{kind:?}")),
}
}