diff options
| author | Ellie Huxtable <ellie@elliehuxtable.com> | 2022-04-13 18:29:18 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-13 18:29:18 +0100 |
| commit | f4240aa62b47850020aa8c3e164d6d3544626f53 (patch) | |
| tree | 4a68f85825fe2d1b82e277730f7c270b5049e639 /atuin-server/src/handlers | |
| parent | provide better error messages (#300) (diff) | |
| download | atuin-f4240aa62b47850020aa8c3e164d6d3544626f53.zip | |
Initial implementation of calendar API (#298)
This can be used in the future for sync so that we can be more
intelligent with what we're doing, and only sync up what's needed
I'd like to eventually replace this with something more like a merkle
tree, hence the hash field I've exposed, but that can come later
Although this does include a much larger number of count queries, it
should also be significantly more cache-able. I'll follow up with that
later, and also follow up with using this for sync :)
Diffstat (limited to 'atuin-server/src/handlers')
| -rw-r--r-- | atuin-server/src/handlers/history.rs | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/atuin-server/src/handlers/history.rs b/atuin-server/src/handlers/history.rs index 546e5a29..fde7cf2d 100644 --- a/atuin-server/src/handlers/history.rs +++ b/atuin-server/src/handlers/history.rs @@ -1,11 +1,14 @@ use axum::extract::Query; -use axum::{Extension, Json}; +use axum::{extract::Path, Extension, Json}; use http::StatusCode; +use std::collections::HashMap; use crate::database::{Database, Postgres}; use crate::models::{NewHistory, User}; use atuin_common::api::*; +use crate::calendar::{TimePeriod, TimePeriodInfo}; + pub async fn count( user: User, db: Extension<Postgres>, @@ -79,3 +82,46 @@ pub async fn add( Ok(()) } + +pub async fn calendar( + Path(focus): Path<String>, + Query(params): Query<HashMap<String, u64>>, + user: User, + db: Extension<Postgres>, +) -> Result<Json<HashMap<u64, TimePeriodInfo>>, ErrorResponseStatus<'static>> { + let focus = focus.as_str(); + + let year = params.get("year").unwrap_or(&0); + let month = params.get("month").unwrap_or(&1); + + let focus = match focus { + "year" => db + .calendar(&user, TimePeriod::YEAR, *year, *month) + .await + .map_err(|_| { + ErrorResponse::reply("failed to query calendar") + .with_status(StatusCode::INTERNAL_SERVER_ERROR) + }), + + "month" => db + .calendar(&user, TimePeriod::MONTH, *year, *month) + .await + .map_err(|_| { + ErrorResponse::reply("failed to query calendar") + .with_status(StatusCode::INTERNAL_SERVER_ERROR) + }), + + "day" => db + .calendar(&user, TimePeriod::DAY, *year, *month) + .await + .map_err(|_| { + ErrorResponse::reply("failed to query calendar") + .with_status(StatusCode::INTERNAL_SERVER_ERROR) + }), + + _ => Err(ErrorResponse::reply("invalid focus: use year/month/day") + .with_status(StatusCode::BAD_REQUEST)), + }?; + + Ok(Json(focus)) +} |
