Skip to content

Commit

Permalink
Merge pull request #52 from krivahtoo/feat/service-logs
Browse files Browse the repository at this point in the history
  • Loading branch information
krivahtoo authored Sep 12, 2024
2 parents e41619b + 69a2346 commit 9712180
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 10 deletions.
2 changes: 1 addition & 1 deletion crates/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ reqwest = { version = "0.12.4", features = ["json"] }
tower = "0.4.13"
chrono-tz = { version = "0.9.0", features = ["serde"] }
rand_core = { version = "0.6.4", features = ["std"] }
serde_repr = "0.1.19"
serde_repr = "0.1.19"
37 changes: 33 additions & 4 deletions crates/server/src/models/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,30 @@ impl Log {
Ok(result.rows_affected())
}

pub async fn list(pool: &SqlitePool, limit: Option<u32>) -> sqlx::Result<Vec<Log>> {
pub async fn list_all(pool: &SqlitePool, limit: Option<u32>) -> sqlx::Result<Vec<Log>> {
let logs = sqlx::query_as::<_, Log>(r#"SELECT * FROM logs ORDER BY id DESC LIMIT ?"#)
.bind(limit.unwrap_or(100))
.fetch_all(pool)
.await?;

Ok(logs)
}

pub async fn list(
pool: &SqlitePool,
service_id: u32,
limit: Option<u32>,
) -> sqlx::Result<Vec<Log>> {
let logs = sqlx::query_as::<_, Log>(
r#"SELECT * FROM logs WHERE service_id = ? ORDER BY id DESC LIMIT ?"#,
)
.bind(service_id)
.bind(limit.unwrap_or(100))
.fetch_all(pool)
.await?;

Ok(logs)
}
}

impl std::fmt::Display for LogForCreate {
Expand Down Expand Up @@ -116,7 +132,7 @@ mod tests {

#[sqlx::test(fixtures("users", "services", "logs"))]
async fn list_logs(pool: SqlitePool) -> sqlx::Result<()> {
let logs = Log::list(&pool, None).await?;
let logs = Log::list_all(&pool, None).await?;

dbg!(&logs);

Expand All @@ -127,7 +143,7 @@ mod tests {

#[sqlx::test(fixtures("users", "services", "logs"))]
async fn list_logs_limit(pool: SqlitePool) -> sqlx::Result<()> {
let logs = Log::list(&pool, Some(2)).await?;
let logs = Log::list_all(&pool, Some(2)).await?;

dbg!(&logs);

Expand All @@ -138,7 +154,20 @@ mod tests {

#[sqlx::test(fixtures("users", "services", "logs"))]
async fn list_logs_order(pool: SqlitePool) -> sqlx::Result<()> {
let logs = Log::list(&pool, Some(2)).await?;
let logs = Log::list_all(&pool, Some(2)).await?;

dbg!(&logs);

assert!(logs.first().unwrap().id > logs.last().unwrap().id);

assert_eq!(logs.len(), 2);

Ok(())
}

#[sqlx::test(fixtures("users", "services", "logs"))]
async fn list_service_logs(pool: SqlitePool) -> sqlx::Result<()> {
let logs = Log::list(&pool, 2, Some(2)).await?;

dbg!(&logs);

Expand Down
2 changes: 1 addition & 1 deletion crates/server/src/routes/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async fn list_logs(
State(state): State<AppState>,
pagination: Query<Pagination>,
) -> Response {
match Log::list(&state.pool, pagination.limit).await {
match Log::list_all(&state.pool, pagination.limit).await {
Ok(logs) => Response::builder()
.header("Content-Type", "application/json")
.body(json!({ "logs": logs }).to_string())
Expand Down
43 changes: 39 additions & 4 deletions crates/server/src/routes/service.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
use axum::{
extract::{Path, State},
extract::{Path, Query, State},
response::{IntoResponse, Response},
routing::{get, put},
Json, Router,
};
use axum_macros::debug_handler;
use serde::Deserialize;
use serde_json::json;
use tracing::error;

use crate::{
auth::Claims,
models::service::{Service, ServiceForCreate, ServiceForUpdate},
models::{
log::Log,
service::{Service, ServiceForCreate, ServiceForUpdate},
},
AppState,
};

#[derive(Deserialize)]
struct Pagination {
limit: Option<u32>,
}

#[debug_handler]
async fn add_service(
_: Claims,
Expand Down Expand Up @@ -109,9 +118,35 @@ async fn list_services(State(state): State<AppState>) -> Response {
.into_response()
}

#[debug_handler]
async fn list_service_logs(
//_: Claims,
State(state): State<AppState>,
Path(service_id): Path<u32>,
pagination: Query<Pagination>,
) -> Response {
match Log::list(&state.pool, service_id, pagination.limit).await {
Ok(logs) => Response::builder()
.header("Content-Type", "application/json")
.body(json!({ "logs": logs }).to_string())
.unwrap()
.into_response(),
Err(e) => {
error!("{e}");
Response::builder()
.header("Content-Type", "application/json")
.status(500)
.body(json!({ "message": "Internal server error" }).to_string())
.unwrap()
.into_response()
}
}
}

pub fn routes(state: AppState) -> Router {
Router::new()
.route("/service", get(list_services).post(add_service))
.route("/service/:id", put(update_service).get(get_service))
.route("/services", get(list_services).post(add_service))
.route("/services/:id", put(update_service).get(get_service))
.route("/services/:id/logs", get(list_service_logs))
.with_state(state)
}

0 comments on commit 9712180

Please sign in to comment.