From ea74daca4809e4008dd8d01039db9fff3be659d9 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Wed, 16 Oct 2024 20:14:33 -0400 Subject: Organizational pass on endpoints and routes. --- src/invite/app.rs | 16 +------ src/invite/routes.rs | 97 ---------------------------------------- src/invite/routes/invite/get.rs | 39 ++++++++++++++++ src/invite/routes/invite/mod.rs | 4 ++ src/invite/routes/invite/post.rs | 51 +++++++++++++++++++++ src/invite/routes/mod.rs | 16 +++++++ src/invite/routes/post.rs | 17 +++++++ 7 files changed, 129 insertions(+), 111 deletions(-) delete mode 100644 src/invite/routes.rs create mode 100644 src/invite/routes/invite/get.rs create mode 100644 src/invite/routes/invite/mod.rs create mode 100644 src/invite/routes/invite/post.rs create mode 100644 src/invite/routes/mod.rs create mode 100644 src/invite/routes/post.rs (limited to 'src/invite') diff --git a/src/invite/app.rs b/src/invite/app.rs index 6800d72..4162470 100644 --- a/src/invite/app.rs +++ b/src/invite/app.rs @@ -31,13 +31,9 @@ impl<'a> Invites<'a> { Ok(invite) } - pub async fn get(&self, invite: &Id) -> Result { + pub async fn get(&self, invite: &Id) -> Result, sqlx::Error> { let mut tx = self.db.begin().await?; - let invite = tx - .invites() - .summary(invite) - .await - .not_found(|| Error::NotFound(invite.clone()))?; + let invite = tx.invites().summary(invite).await.optional()?; tx.commit().await?; Ok(invite) @@ -91,14 +87,6 @@ impl<'a> Invites<'a> { } } -#[derive(Debug, thiserror::Error)] -pub enum Error { - #[error("invite not found: {0}")] - NotFound(Id), - #[error(transparent)] - Database(#[from] sqlx::Error), -} - #[derive(Debug, thiserror::Error)] pub enum AcceptError { #[error("invite not found: {0}")] diff --git a/src/invite/routes.rs b/src/invite/routes.rs deleted file mode 100644 index 977fe9b..0000000 --- a/src/invite/routes.rs +++ /dev/null @@ -1,97 +0,0 @@ -use axum::{ - extract::{Json, Path, State}, - http::StatusCode, - response::{IntoResponse, Response}, - routing::{get, post}, - Router, -}; - -use super::{app, Id, Invite, Summary}; -use crate::{ - app::App, - clock::RequestedAt, - error::{Internal, NotFound}, - login::{Login, Password}, - token::extract::IdentityToken, -}; - -pub fn router() -> Router { - Router::new() - .route("/api/invite", post(on_invite)) - .route("/api/invite/:invite", get(invite)) - .route("/api/invite/:invite", post(on_accept)) -} - -#[derive(serde::Deserialize)] -struct InviteRequest {} - -async fn on_invite( - State(app): State, - RequestedAt(issued_at): RequestedAt, - login: Login, - // Require `{}` as the only valid request for this endpoint. - _: Json, -) -> Result, Internal> { - let invite = app.invites().create(&login, &issued_at).await?; - Ok(Json(invite)) -} - -async fn invite( - State(app): State, - Path(invite): Path, -) -> Result, InviteError> { - app.invites() - .get(&invite) - .await - .map(Json) - .map_err(InviteError) -} - -struct InviteError(app::Error); - -impl IntoResponse for InviteError { - fn into_response(self) -> Response { - let Self(error) = self; - match error { - error @ app::Error::NotFound(_) => NotFound(error).into_response(), - other => Internal::from(other).into_response(), - } - } -} - -#[derive(serde::Deserialize)] -struct AcceptRequest { - name: String, - password: Password, -} - -async fn on_accept( - State(app): State, - RequestedAt(accepted_at): RequestedAt, - identity: IdentityToken, - Path(invite): Path, - Json(request): Json, -) -> Result<(IdentityToken, StatusCode), AcceptError> { - let secret = app - .invites() - .accept(&invite, &request.name, &request.password, &accepted_at) - .await - .map_err(AcceptError)?; - let identity = identity.set(secret); - Ok((identity, StatusCode::NO_CONTENT)) -} - -struct AcceptError(app::AcceptError); - -impl IntoResponse for AcceptError { - fn into_response(self) -> Response { - let Self(error) = self; - match error { - error @ app::AcceptError::NotFound(_) => NotFound(error).into_response(), - error @ app::AcceptError::DuplicateLogin(_) => { - (StatusCode::CONFLICT, error.to_string()).into_response() - } - other => Internal::from(other).into_response(), - } - } -} diff --git a/src/invite/routes/invite/get.rs b/src/invite/routes/invite/get.rs new file mode 100644 index 0000000..c8b52f1 --- /dev/null +++ b/src/invite/routes/invite/get.rs @@ -0,0 +1,39 @@ +use axum::{ + extract::{Json, Path, State}, + response::{IntoResponse, Response}, +}; + +use crate::{ + app::App, + error::{Internal, NotFound}, + invite::{Id, Summary}, +}; + +pub async fn handler( + State(app): State, + Path(invite): Path, +) -> Result, Error> { + app.invites() + .get(&invite) + .await? + .map(Json) + .ok_or_else(move || Error::NotFound(invite)) +} + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("invite not found: {0}")] + NotFound(Id), + #[error(transparent)] + Database(#[from] sqlx::Error), +} + +impl IntoResponse for Error { + fn into_response(self) -> Response { + #[allow(clippy::match_wildcard_for_single_variants)] + match self { + Self::NotFound(_) => NotFound(self).into_response(), + other => Internal::from(other).into_response(), + } + } +} diff --git a/src/invite/routes/invite/mod.rs b/src/invite/routes/invite/mod.rs new file mode 100644 index 0000000..04593fd --- /dev/null +++ b/src/invite/routes/invite/mod.rs @@ -0,0 +1,4 @@ +pub mod get; +pub mod post; + +type PathInfo = crate::invite::Id; diff --git a/src/invite/routes/invite/post.rs b/src/invite/routes/invite/post.rs new file mode 100644 index 0000000..12c2e21 --- /dev/null +++ b/src/invite/routes/invite/post.rs @@ -0,0 +1,51 @@ +use axum::{ + extract::{Json, Path, State}, + http::StatusCode, + response::{IntoResponse, Response}, +}; + +use crate::{ + app::App, + clock::RequestedAt, + error::{Internal, NotFound}, + invite::app, + login::Password, + token::extract::IdentityToken, +}; + +pub async fn handler( + State(app): State, + RequestedAt(accepted_at): RequestedAt, + identity: IdentityToken, + Path(invite): Path, + Json(request): Json, +) -> Result<(IdentityToken, StatusCode), Error> { + let secret = app + .invites() + .accept(&invite, &request.name, &request.password, &accepted_at) + .await + .map_err(Error)?; + let identity = identity.set(secret); + Ok((identity, StatusCode::NO_CONTENT)) +} + +#[derive(serde::Deserialize)] +pub struct Request { + pub name: String, + pub password: Password, +} + +pub struct Error(app::AcceptError); + +impl IntoResponse for Error { + fn into_response(self) -> Response { + let Self(error) = self; + match error { + app::AcceptError::NotFound(_) => NotFound(error).into_response(), + app::AcceptError::DuplicateLogin(_) => { + (StatusCode::CONFLICT, error.to_string()).into_response() + } + other => Internal::from(other).into_response(), + } + } +} diff --git a/src/invite/routes/mod.rs b/src/invite/routes/mod.rs new file mode 100644 index 0000000..dae20ba --- /dev/null +++ b/src/invite/routes/mod.rs @@ -0,0 +1,16 @@ +use axum::{ + routing::{get, post}, + Router, +}; + +use crate::app::App; + +mod invite; +mod post; + +pub fn router() -> Router { + Router::new() + .route("/api/invite", post(post::handler)) + .route("/api/invite/:invite", get(invite::get::handler)) + .route("/api/invite/:invite", post(invite::post::handler)) +} diff --git a/src/invite/routes/post.rs b/src/invite/routes/post.rs new file mode 100644 index 0000000..80b1c27 --- /dev/null +++ b/src/invite/routes/post.rs @@ -0,0 +1,17 @@ +use axum::extract::{Json, State}; + +use crate::{app::App, clock::RequestedAt, error::Internal, invite::Invite, login::Login}; + +pub async fn handler( + State(app): State, + RequestedAt(issued_at): RequestedAt, + login: Login, + // Require `{}` as the only valid request for this endpoint. + _: Json, +) -> Result, Internal> { + let invite = app.invites().create(&login, &issued_at).await?; + Ok(Json(invite)) +} + +#[derive(Default, serde::Deserialize)] +pub struct Request {} -- cgit v1.2.3