@@ -2,27 +2,43 @@ use axum::Json;
|
||||
use axum::extract::{Path, Query, State};
|
||||
use axum::http::StatusCode;
|
||||
|
||||
use api_types::mappers::correlation_response;
|
||||
use api_types::requests::{
|
||||
CreateEntryRequest, DateRangeParams, ListEntriesParams, ReplaceActivityRequest,
|
||||
UpdateEntryRequest,
|
||||
};
|
||||
use api_types::responses::{
|
||||
BulkActionResponse, CalendarDayResponse, CorrelationResponse, EntryResponse, MoodStatsResponse,
|
||||
BulkActionResponse, CalendarDayResponse, EntryResponse, MoodStatsResponse,
|
||||
};
|
||||
use application::entry::composition::EntryComposer;
|
||||
use application::entry::queries::{FilterByActivityQuery, FilterByMoodQuery, MoodStatsQuery};
|
||||
use application::entry::use_cases::{
|
||||
create_entry, delete_entries_by_date_range, delete_entry, filter_by_activity, filter_by_mood,
|
||||
get_activity_correlation, get_calendar, get_entry, get_mood_stats, list_entries,
|
||||
replace_activity, update_entry,
|
||||
get_calendar, get_entry, get_mood_stats, list_entries, replace_activity, update_entry,
|
||||
};
|
||||
use domain::activity::ActivityId;
|
||||
use domain::entry::MoodEntry;
|
||||
use domain::entry::{Mood, MoodEntryId};
|
||||
|
||||
use crate::errors::ApiError;
|
||||
use crate::extractors::{AuthenticatedUser, PathId};
|
||||
use crate::state::AppState;
|
||||
|
||||
async fn compose(
|
||||
dimensions: Vec<std::sync::Arc<dyn domain::ports::EntryDimensionPort>>,
|
||||
entries: Vec<MoodEntry>,
|
||||
) -> Result<Vec<EntryResponse>, ApiError> {
|
||||
let composed = EntryComposer::new(dimensions).compose(entries).await?;
|
||||
Ok(composed.into_iter().map(EntryResponse::from).collect())
|
||||
}
|
||||
|
||||
async fn compose_one(
|
||||
dimensions: Vec<std::sync::Arc<dyn domain::ports::EntryDimensionPort>>,
|
||||
entry: MoodEntry,
|
||||
) -> Result<EntryResponse, ApiError> {
|
||||
let mut responses = compose(dimensions, vec![entry]).await?;
|
||||
Ok(responses.remove(0))
|
||||
}
|
||||
|
||||
#[utoipa::path(post, path = "/api/v1/entries", tag = "entries", security(("bearer" = [])),
|
||||
request_body = CreateEntryRequest,
|
||||
responses((status = 201, body = EntryResponse))
|
||||
@@ -33,12 +49,17 @@ pub async fn handle_create(
|
||||
Json(body): Json<CreateEntryRequest>,
|
||||
) -> Result<(StatusCode, Json<EntryResponse>), ApiError> {
|
||||
let cmd = body.into_command(user_id, &state.entry_config)?;
|
||||
let dimensions = state.dimensions.clone();
|
||||
let deps = create_entry::Deps {
|
||||
entries: state.entry_command,
|
||||
dimensions: state.dimensions.clone(),
|
||||
events: state.event_publisher,
|
||||
};
|
||||
let entry = create_entry::execute(cmd, &deps).await?;
|
||||
Ok((StatusCode::CREATED, Json(EntryResponse::from(entry))))
|
||||
Ok((
|
||||
StatusCode::CREATED,
|
||||
Json(compose_one(dimensions, entry).await?),
|
||||
))
|
||||
}
|
||||
|
||||
#[utoipa::path(get, path = "/api/v1/entries/{id}", tag = "entries", security(("bearer" = [])),
|
||||
@@ -50,11 +71,12 @@ pub async fn handle_get(
|
||||
AuthenticatedUser(user_id): AuthenticatedUser,
|
||||
PathId(entry_id): PathId<MoodEntryId>,
|
||||
) -> Result<Json<EntryResponse>, ApiError> {
|
||||
let dimensions = state.dimensions.clone();
|
||||
let deps = get_entry::Deps {
|
||||
query: state.entry_query,
|
||||
};
|
||||
let entry = get_entry::execute(entry_id, user_id, &deps).await?;
|
||||
Ok(Json(EntryResponse::from(entry)))
|
||||
Ok(Json(compose_one(dimensions, entry).await?))
|
||||
}
|
||||
|
||||
#[utoipa::path(get, path = "/api/v1/entries", tag = "entries", security(("bearer" = [])),
|
||||
@@ -67,11 +89,12 @@ pub async fn handle_list(
|
||||
Query(params): Query<ListEntriesParams>,
|
||||
) -> Result<Json<Vec<EntryResponse>>, ApiError> {
|
||||
let query = params.into_query(user_id)?;
|
||||
let dimensions = state.dimensions.clone();
|
||||
let deps = list_entries::Deps {
|
||||
query: state.entry_query,
|
||||
};
|
||||
let entries = list_entries::execute(query, &deps).await?;
|
||||
Ok(Json(entries.into_iter().map(EntryResponse::from).collect()))
|
||||
Ok(Json(compose(dimensions, entries).await?))
|
||||
}
|
||||
|
||||
#[utoipa::path(patch, path = "/api/v1/entries/{id}", tag = "entries", security(("bearer" = [])),
|
||||
@@ -86,14 +109,16 @@ pub async fn handle_update(
|
||||
Json(body): Json<UpdateEntryRequest>,
|
||||
) -> Result<Json<EntryResponse>, ApiError> {
|
||||
let cmd = body.into_command(entry_id, &state.entry_config)?;
|
||||
let dimensions = state.dimensions.clone();
|
||||
let deps = update_entry::Deps {
|
||||
command: state.entry_command,
|
||||
dimensions: state.dimensions.clone(),
|
||||
query: state.entry_query,
|
||||
media_storage: state.media_storage,
|
||||
events: state.event_publisher,
|
||||
};
|
||||
let entry = update_entry::execute(cmd, user_id, &deps).await?;
|
||||
Ok(Json(EntryResponse::from(entry)))
|
||||
Ok(Json(compose_one(dimensions, entry).await?))
|
||||
}
|
||||
|
||||
#[utoipa::path(delete, path = "/api/v1/entries/{id}", tag = "entries", security(("bearer" = [])),
|
||||
@@ -106,6 +131,7 @@ pub async fn handle_delete(
|
||||
PathId(entry_id): PathId<MoodEntryId>,
|
||||
) -> Result<StatusCode, ApiError> {
|
||||
let deps = delete_entry::Deps {
|
||||
dimensions: state.dimensions.clone(),
|
||||
command: state.entry_command,
|
||||
query: state.entry_query,
|
||||
events: state.event_publisher,
|
||||
@@ -126,11 +152,12 @@ pub async fn handle_filter_by_mood(
|
||||
) -> Result<Json<Vec<EntryResponse>>, ApiError> {
|
||||
let mood = Mood::try_from(mood)?;
|
||||
let query = FilterByMoodQuery { user_id, mood };
|
||||
let dimensions = state.dimensions.clone();
|
||||
let deps = filter_by_mood::Deps {
|
||||
query: state.entry_query,
|
||||
};
|
||||
let entries = filter_by_mood::execute(query, &deps).await?;
|
||||
Ok(Json(entries.into_iter().map(EntryResponse::from).collect()))
|
||||
Ok(Json(compose(dimensions, entries).await?))
|
||||
}
|
||||
|
||||
#[utoipa::path(get, path = "/api/v1/entries/filter/activity/{id}", tag = "entries", security(("bearer" = [])),
|
||||
@@ -146,11 +173,12 @@ pub async fn handle_filter_by_activity(
|
||||
user_id,
|
||||
activity_id,
|
||||
};
|
||||
let dimensions = state.dimensions.clone();
|
||||
let deps = filter_by_activity::Deps {
|
||||
query: state.entry_query,
|
||||
};
|
||||
let entries = filter_by_activity::execute(query, &deps).await?;
|
||||
Ok(Json(entries.into_iter().map(EntryResponse::from).collect()))
|
||||
Ok(Json(compose(dimensions, entries).await?))
|
||||
}
|
||||
|
||||
#[utoipa::path(get, path = "/api/v1/entries/stats", tag = "entries", security(("bearer" = [])),
|
||||
@@ -172,6 +200,7 @@ pub async fn handle_stats(
|
||||
};
|
||||
let query = MoodStatsQuery { user_id, range };
|
||||
let deps = get_mood_stats::Deps {
|
||||
users: state.user_query.clone(),
|
||||
query: state.entry_query,
|
||||
};
|
||||
let stats = get_mood_stats::execute(query, &deps).await?;
|
||||
@@ -189,7 +218,11 @@ pub async fn handle_calendar(
|
||||
) -> Result<Json<Vec<CalendarDayResponse>>, ApiError> {
|
||||
let range = params.into_date_range()?;
|
||||
let deps = get_calendar::Deps {
|
||||
users: state.user_query.clone(),
|
||||
query: state.entry_query,
|
||||
dimensions: state.dimensions.clone(),
|
||||
cycles: state.cycle_query,
|
||||
preferences: state.preferences_query,
|
||||
};
|
||||
let days = get_calendar::execute(user_id, range, &deps).await?;
|
||||
Ok(Json(
|
||||
@@ -197,32 +230,6 @@ pub async fn handle_calendar(
|
||||
))
|
||||
}
|
||||
|
||||
#[utoipa::path(get, path = "/api/v1/entries/correlation/{id}", tag = "entries", security(("bearer" = [])),
|
||||
params(("id" = String, Path, description = "Activity ID"), ListEntriesParams),
|
||||
responses((status = 200, body = CorrelationResponse))
|
||||
)]
|
||||
pub async fn handle_activity_correlation(
|
||||
State(state): State<AppState>,
|
||||
AuthenticatedUser(user_id): AuthenticatedUser,
|
||||
PathId(activity_id): PathId<ActivityId>,
|
||||
Query(params): Query<ListEntriesParams>,
|
||||
) -> Result<Json<CorrelationResponse>, ApiError> {
|
||||
let range = match (params.from, params.to) {
|
||||
(Some(from), Some(to)) => {
|
||||
let from = api_types::mappers::shared::parse_datetime(&from)?;
|
||||
let to = api_types::mappers::shared::parse_datetime(&to)?;
|
||||
Some(domain::entry::DateRange::new(from, to)?)
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
let deps = get_activity_correlation::Deps {
|
||||
query: state.entry_query,
|
||||
};
|
||||
let correlation =
|
||||
get_activity_correlation::execute(user_id, activity_id.clone(), range, &deps).await?;
|
||||
Ok(Json(correlation_response(activity_id, correlation)))
|
||||
}
|
||||
|
||||
#[utoipa::path(delete, path = "/api/v1/entries/bulk/delete", tag = "entries", security(("bearer" = [])),
|
||||
params(DateRangeParams),
|
||||
responses((status = 200, body = BulkActionResponse))
|
||||
@@ -234,6 +241,8 @@ pub async fn handle_delete_by_date_range(
|
||||
) -> Result<Json<BulkActionResponse>, ApiError> {
|
||||
let range = params.into_date_range()?;
|
||||
let deps = delete_entries_by_date_range::Deps {
|
||||
query: state.entry_query.clone(),
|
||||
dimensions: state.dimensions.clone(),
|
||||
cascade: state.cascade,
|
||||
media_storage: state.media_storage,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user