Chart page with a drawn wheel; reuse saved charts for identical birth data
- New /kepletek/[id] page: chart wheel (SVG), big three, elements, modalities, moon phase, positions and an aspects table. /keplet opens the user's own chart; the home page goes there after login. - The profile only shows the birth data, with a link to the chart. - Saving the same birth data again returns the stored chart (200) instead of calculating a new one. - Charts without a birth time (angles null) render without houses. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -106,22 +106,54 @@ pub struct CreateChart {
|
||||
/// Calculates a chart from birth data; the `name` field is dropped so no
|
||||
/// personal name reaches the calculation (or, later, the LLM).
|
||||
pub async fn calculate(state: &AppState, birth: Value) -> Result<(Value, Value, String), ApiError> {
|
||||
let Value::Object(mut birth) = birth else {
|
||||
return Err(ApiError::BadRequest("birth must be an object".into()));
|
||||
};
|
||||
birth.remove("name");
|
||||
let birth = Value::Object(birth);
|
||||
let birth = clean_birth(birth)?;
|
||||
let result = state.horoscope.natal(&birth).await?;
|
||||
let hash = chart_hash(&result);
|
||||
Ok((birth, result, hash))
|
||||
}
|
||||
|
||||
fn clean_birth(birth: Value) -> Result<Value, ApiError> {
|
||||
let Value::Object(mut birth) = birth else {
|
||||
return Err(ApiError::BadRequest("birth must be an object".into()));
|
||||
};
|
||||
birth.remove("name");
|
||||
Ok(Value::Object(birth))
|
||||
}
|
||||
|
||||
/// Saves a new chart. If the user already has a chart with exactly the same
|
||||
/// birth data, that one is returned (and made primary if asked) instead of
|
||||
/// calculating it again.
|
||||
pub async fn create(
|
||||
State(state): St,
|
||||
auth: AuthUser,
|
||||
WithRejection(Json(req), _): Body<CreateChart>,
|
||||
) -> ApiResult<(StatusCode, Json<Value>)> {
|
||||
let label = clean_label(&req.label)?;
|
||||
let birth = clean_birth(req.birth)?;
|
||||
let existing: Option<ChartRow> =
|
||||
sqlx::query_as(&format!("SELECT {CHART_COLUMNS} FROM charts WHERE owner_id = $1 AND birth_data = $2 LIMIT 1"))
|
||||
.bind(auth.id())
|
||||
.bind(&birth)
|
||||
.fetch_optional(&state.db)
|
||||
.await?;
|
||||
if let Some(chart) = existing {
|
||||
if req.is_primary && !chart.is_primary {
|
||||
let mut tx = state.db.begin().await?;
|
||||
sqlx::query("UPDATE charts SET is_primary = false, updated_at = now() WHERE owner_id = $1 AND is_primary")
|
||||
.bind(auth.id())
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
let row: ChartRow = sqlx::query_as(&format!(
|
||||
"UPDATE charts SET is_primary = true, updated_at = now() WHERE id = $1 RETURNING {CHART_COLUMNS}"
|
||||
))
|
||||
.bind(chart.id)
|
||||
.fetch_one(&mut *tx)
|
||||
.await?;
|
||||
tx.commit().await?;
|
||||
return Ok((StatusCode::OK, Json(row.owner_view())));
|
||||
}
|
||||
return Ok((StatusCode::OK, Json(chart.owner_view())));
|
||||
}
|
||||
let (count,): (i64,) =
|
||||
sqlx::query_as("SELECT count(*) FROM charts WHERE owner_id = $1").bind(auth.id()).fetch_one(&state.db).await?;
|
||||
if count >= state.config.max_charts_per_user {
|
||||
@@ -130,7 +162,7 @@ pub async fn create(
|
||||
format!("at most {} charts can be saved", state.config.max_charts_per_user),
|
||||
));
|
||||
}
|
||||
let (birth, result, hash) = calculate(&state, req.birth).await?;
|
||||
let (birth, result, hash) = calculate(&state, birth).await?;
|
||||
|
||||
let mut tx = state.db.begin().await?;
|
||||
if req.is_primary {
|
||||
|
||||
@@ -358,6 +358,25 @@ async fn charts_are_private(pool: PgPool) {
|
||||
assert_eq!(list["charts"][0]["id"], other["id"]); // the new primary first
|
||||
assert_eq!(list["charts"][1]["is_primary"], false);
|
||||
|
||||
// the same birth data again: the saved chart is reused, not calculated again
|
||||
let r = app
|
||||
.post(
|
||||
"/v1/charts",
|
||||
Some(&anna),
|
||||
json!({"label": "Újra", "is_primary": true,
|
||||
"birth": {"date": "1990-03-15", "time": "14:30", "country": "HU", "place": "Szeged"}}),
|
||||
)
|
||||
.await;
|
||||
assert_eq!((r.status, r.body["id"].as_str(), &r.body["is_primary"]), (StatusCode::OK, Some(id), &json!(true)));
|
||||
assert_eq!(app.get("/v1/charts", Some(&anna)).await.body["charts"].as_array().unwrap().len(), 2);
|
||||
app.call(
|
||||
"PATCH",
|
||||
&format!("/v1/charts/{}", other["id"].as_str().unwrap()),
|
||||
Some(&anna),
|
||||
Some(json!({"is_primary": true})),
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(app.get(&format!("/v1/charts/{id}"), Some(&bela)).await.status, StatusCode::NOT_FOUND);
|
||||
assert_eq!(app.call("DELETE", &format!("/v1/charts/{id}"), Some(&bela), None).await.status, StatusCode::NOT_FOUND);
|
||||
assert_eq!(app.get("/v1/charts/not-a-uuid", Some(&anna)).await.status, StatusCode::BAD_REQUEST);
|
||||
|
||||
Reference in New Issue
Block a user