user centric apis

This commit is contained in:
Jeremy Wall 2022-09-03 11:44:21 -04:00
parent 45284ca038
commit 1ce8da0294

View File

@ -80,34 +80,47 @@ async fn ui_static_assets(Path(path): Path<String>) -> impl IntoResponse {
#[instrument] #[instrument]
async fn api_recipes( async fn api_recipes(
Extension(store): Extension<Arc<recipe_store::AsyncFileStore>>, Extension(store): Extension<Arc<recipe_store::AsyncFileStore>>,
Extension(app_store): Extension<Arc<session::SqliteStore>>,
session: session::UserIdFromSession, session: session::UserIdFromSession,
) -> impl IntoResponse { ) -> impl IntoResponse {
// TODO(jwall): Select recipes based on the user-id if it exists. // Select recipes based on the user-id if it exists or serve the default if it does not.
// Or serve the default if it does not. use session::{UserId, UserIdFromSession::*};
let result: Result<axum::Json<Vec<RecipeEntry>>, String> = match store let result = match session {
.get_recipes() NoUserId => store
.await .get_recipes()
.map_err(|e| format!("Error: {:?}", e)) .await
{ .map_err(|e| format!("Error: {:?}", e)),
FoundUserId(UserId(id)) => app_store
.get_recipes_for_user(id.as_str())
.await
.map_err(|e| format!("Error: {:?}", e)),
};
match result {
Ok(Some(recipes)) => Ok(axum::Json::from(recipes)), Ok(Some(recipes)) => Ok(axum::Json::from(recipes)),
Ok(None) => Ok(axum::Json::from(Vec::<RecipeEntry>::new())), Ok(None) => Ok(axum::Json::from(Vec::<RecipeEntry>::new())),
Err(e) => Err(e), Err(e) => Err(e),
}; }
result
} }
#[instrument] #[instrument]
async fn api_categories( async fn api_categories(
Extension(store): Extension<Arc<recipe_store::AsyncFileStore>>, Extension(store): Extension<Arc<recipe_store::AsyncFileStore>>,
Extension(app_store): Extension<Arc<session::SqliteStore>>,
session: session::UserIdFromSession, session: session::UserIdFromSession,
) -> impl IntoResponse { ) -> impl IntoResponse {
// TODO(jwall): Select recipes based on the user-id if it exists. // Select Categories based on the user-id if it exists or serve the default if it does not.
// Or serve the default if it does not. use session::{UserId, UserIdFromSession::*};
let recipe_result = store let categories_result = match session {
.get_categories() NoUserId => store
.await .get_categories()
.map_err(|e| format!("Error: {:?}", e)); .await
let result: Result<axum::Json<String>, String> = match recipe_result { .map_err(|e| format!("Error: {:?}", e)),
FoundUserId(UserId(id)) => app_store
.get_categories_for_user(id.as_str())
.await
.map_err(|e| format!("Error: {:?}", e)),
};
let result: Result<axum::Json<String>, String> = match categories_result {
Ok(Some(categories)) => Ok(axum::Json::from(categories)), Ok(Some(categories)) => Ok(axum::Json::from(categories)),
Ok(None) => Ok(axum::Json::from(String::new())), Ok(None) => Ok(axum::Json::from(String::new())),
Err(e) => Err(e), Err(e) => Err(e),
@ -115,31 +128,27 @@ async fn api_categories(
result result
} }
pub async fn add_user(session_store_path: PathBuf, username: String, password: String) { pub async fn add_user(store_path: PathBuf, username: String, password: String) {
let session_store = session::SqliteStore::new(session_store_path) let app_store = session::SqliteStore::new(store_path)
.await .await
.expect("Unable to create session_store"); .expect("Unable to create app_store");
let user_creds = session::UserCreds { let user_creds = session::UserCreds {
id: session::UserId(username), id: session::UserId(username),
pass: secrecy::Secret::from(password), pass: secrecy::Secret::from(password),
}; };
session_store app_store
.store_user_creds(user_creds) .store_user_creds(user_creds)
.await .await
.expect("Failed to store user creds"); .expect("Failed to store user creds");
} }
#[instrument(fields(recipe_dir=?recipe_dir_path,listen=?listen_socket), skip_all)] #[instrument(fields(recipe_dir=?recipe_dir_path,listen=?listen_socket), skip_all)]
pub async fn ui_main( pub async fn ui_main(recipe_dir_path: PathBuf, store_path: PathBuf, listen_socket: SocketAddr) {
recipe_dir_path: PathBuf,
session_store_path: PathBuf,
listen_socket: SocketAddr,
) {
let store = Arc::new(recipe_store::AsyncFileStore::new(recipe_dir_path.clone())); let store = Arc::new(recipe_store::AsyncFileStore::new(recipe_dir_path.clone()));
//let dir_path = (&dir_path).clone(); //let dir_path = (&dir_path).clone();
let session_store = session::SqliteStore::new(session_store_path) let app_store = session::SqliteStore::new(store_path)
.await .await
.expect("Unable to create session_store"); .expect("Unable to create app_store");
let router = Router::new() let router = Router::new()
.route("/", get(|| async { Redirect::temporary("/ui/plan") })) .route("/", get(|| async { Redirect::temporary("/ui/plan") }))
.route("/ui/*path", get(ui_static_assets)) .route("/ui/*path", get(ui_static_assets))
@ -157,7 +166,7 @@ pub async fn ui_main(
ServiceBuilder::new() ServiceBuilder::new()
.layer(TraceLayer::new_for_http()) .layer(TraceLayer::new_for_http())
.layer(Extension(store)) .layer(Extension(store))
.layer(Extension(session_store)), .layer(Extension(app_store)),
); );
info!( info!(
http = format!("http://{}", listen_socket), http = format!("http://{}", listen_socket),