Colonne projects.repo_url (migration 0003), exposée par l'API (repo_url à la création, "" ou null pour l'effacer) et éditable sur la page clients ; une icône ouvre le repo dans un nouvel onglet. Seules les URL http(s) sont acceptées. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
194 lines
5.1 KiB
Rust
194 lines
5.1 KiB
Rust
use axum::extract::{Path, Query, State};
|
|
use axum::http::StatusCode;
|
|
use axum::routing::{get, patch, post};
|
|
use axum::{Json, Router};
|
|
use serde::Deserialize;
|
|
use sqlx::SqlitePool;
|
|
|
|
use crate::clock;
|
|
use crate::error::AppResult;
|
|
use crate::models::{
|
|
Client, Entry, EntryFilter, NameInput, NewEntry, Project, Report, ReportQuery, StartTimer,
|
|
UpdateEntry, UpdateInput,
|
|
};
|
|
use crate::repo::{clients, entries, projects, reports};
|
|
|
|
pub fn router() -> Router<SqlitePool> {
|
|
Router::new()
|
|
.route("/clients", get(list_clients).post(create_client))
|
|
.route("/clients/{id}", patch(update_client).delete(delete_client))
|
|
.route(
|
|
"/clients/{id}/projects",
|
|
get(list_projects).post(create_project),
|
|
)
|
|
.route(
|
|
"/projects/{id}",
|
|
patch(update_project).delete(delete_project),
|
|
)
|
|
.route("/timer", get(timer))
|
|
.route("/timer/start", post(start_timer))
|
|
.route("/timer/stop", post(stop_timer))
|
|
.route("/entries", get(list_entries).post(create_entry))
|
|
.route("/entries/{id}", patch(update_entry).delete(delete_entry))
|
|
.route("/reports", get(report))
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct ArchivedQuery {
|
|
#[serde(default)]
|
|
archived: bool,
|
|
}
|
|
|
|
async fn list_clients(
|
|
State(pool): State<SqlitePool>,
|
|
Query(q): Query<ArchivedQuery>,
|
|
) -> AppResult<Json<Vec<Client>>> {
|
|
Ok(Json(clients::list(&pool, q.archived).await?))
|
|
}
|
|
|
|
async fn create_client(
|
|
State(pool): State<SqlitePool>,
|
|
Json(input): Json<NameInput>,
|
|
) -> AppResult<(StatusCode, Json<Client>)> {
|
|
Ok((
|
|
StatusCode::CREATED,
|
|
Json(clients::create(&pool, &input.name, input.color.as_deref()).await?),
|
|
))
|
|
}
|
|
|
|
async fn update_client(
|
|
State(pool): State<SqlitePool>,
|
|
Path(id): Path<i64>,
|
|
Json(input): Json<UpdateInput>,
|
|
) -> AppResult<Json<Client>> {
|
|
Ok(Json(clients::update(&pool, id, &input).await?))
|
|
}
|
|
|
|
async fn delete_client(
|
|
State(pool): State<SqlitePool>,
|
|
Path(id): Path<i64>,
|
|
) -> AppResult<StatusCode> {
|
|
clients::delete(&pool, id).await?;
|
|
Ok(StatusCode::NO_CONTENT)
|
|
}
|
|
|
|
async fn list_projects(
|
|
State(pool): State<SqlitePool>,
|
|
Path(client_id): Path<i64>,
|
|
Query(q): Query<ArchivedQuery>,
|
|
) -> AppResult<Json<Vec<Project>>> {
|
|
clients::get(&pool, client_id).await?;
|
|
Ok(Json(
|
|
projects::list(&pool, Some(client_id), q.archived).await?,
|
|
))
|
|
}
|
|
|
|
async fn create_project(
|
|
State(pool): State<SqlitePool>,
|
|
Path(client_id): Path<i64>,
|
|
Json(input): Json<NameInput>,
|
|
) -> AppResult<(StatusCode, Json<Project>)> {
|
|
Ok((
|
|
StatusCode::CREATED,
|
|
Json(
|
|
projects::create(
|
|
&pool,
|
|
client_id,
|
|
&input.name,
|
|
input.color.as_deref(),
|
|
input.repo_url.as_deref(),
|
|
)
|
|
.await?,
|
|
),
|
|
))
|
|
}
|
|
|
|
async fn update_project(
|
|
State(pool): State<SqlitePool>,
|
|
Path(id): Path<i64>,
|
|
Json(input): Json<UpdateInput>,
|
|
) -> AppResult<Json<Project>> {
|
|
Ok(Json(projects::update(&pool, id, &input).await?))
|
|
}
|
|
|
|
async fn delete_project(
|
|
State(pool): State<SqlitePool>,
|
|
Path(id): Path<i64>,
|
|
) -> AppResult<StatusCode> {
|
|
projects::delete(&pool, id).await?;
|
|
Ok(StatusCode::NO_CONTENT)
|
|
}
|
|
|
|
async fn timer(State(pool): State<SqlitePool>) -> AppResult<Json<Option<Entry>>> {
|
|
Ok(Json(entries::running(&pool).await?))
|
|
}
|
|
|
|
async fn start_timer(
|
|
State(pool): State<SqlitePool>,
|
|
Json(input): Json<StartTimer>,
|
|
) -> AppResult<(StatusCode, Json<Entry>)> {
|
|
Ok((
|
|
StatusCode::CREATED,
|
|
Json(entries::start(&pool, input.project_id, &input.note).await?),
|
|
))
|
|
}
|
|
|
|
async fn stop_timer(State(pool): State<SqlitePool>) -> AppResult<Json<Entry>> {
|
|
Ok(Json(entries::stop(&pool).await?))
|
|
}
|
|
|
|
async fn list_entries(
|
|
State(pool): State<SqlitePool>,
|
|
Query(filter): Query<EntryFilter>,
|
|
) -> AppResult<Json<Vec<Entry>>> {
|
|
Ok(Json(entries::list(&pool, &filter, clock::now()).await?))
|
|
}
|
|
|
|
async fn create_entry(
|
|
State(pool): State<SqlitePool>,
|
|
Json(input): Json<NewEntry>,
|
|
) -> AppResult<(StatusCode, Json<Entry>)> {
|
|
Ok((
|
|
StatusCode::CREATED,
|
|
Json(entries::create(&pool, &input).await?),
|
|
))
|
|
}
|
|
|
|
async fn update_entry(
|
|
State(pool): State<SqlitePool>,
|
|
Path(id): Path<i64>,
|
|
Json(input): Json<UpdateEntry>,
|
|
) -> AppResult<Json<Entry>> {
|
|
Ok(Json(entries::update(&pool, id, &input).await?))
|
|
}
|
|
|
|
async fn delete_entry(
|
|
State(pool): State<SqlitePool>,
|
|
Path(id): Path<i64>,
|
|
) -> AppResult<StatusCode> {
|
|
entries::delete(&pool, id).await?;
|
|
Ok(StatusCode::NO_CONTENT)
|
|
}
|
|
|
|
/// Defaults to the current week.
|
|
async fn report(
|
|
State(pool): State<SqlitePool>,
|
|
Query(q): Query<ReportQuery>,
|
|
) -> AppResult<Json<Report>> {
|
|
let today = clock::today();
|
|
let from = q.from.unwrap_or_else(|| clock::week_start(today));
|
|
let to = q.to.unwrap_or(today.max(from));
|
|
Ok(Json(
|
|
reports::build(
|
|
&pool,
|
|
from,
|
|
to,
|
|
q.group_by,
|
|
q.client_id,
|
|
q.project_id,
|
|
clock::now(),
|
|
)
|
|
.await?,
|
|
))
|
|
}
|