summaryrefslogtreecommitdiff
path: root/src/invite
diff options
context:
space:
mode:
Diffstat (limited to 'src/invite')
-rw-r--r--src/invite/app.rs16
-rw-r--r--src/invite/routes.rs97
-rw-r--r--src/invite/routes/invite/get.rs39
-rw-r--r--src/invite/routes/invite/mod.rs4
-rw-r--r--src/invite/routes/invite/post.rs51
-rw-r--r--src/invite/routes/mod.rs16
-rw-r--r--src/invite/routes/post.rs17
7 files changed, 129 insertions, 111 deletions
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<Summary, Error> {
+ pub async fn get(&self, invite: &Id) -> Result<Option<Summary>, 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)
@@ -92,14 +88,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}")]
NotFound(Id),
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<App> {
- 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<App>,
- RequestedAt(issued_at): RequestedAt,
- login: Login,
- // Require `{}` as the only valid request for this endpoint.
- _: Json<InviteRequest>,
-) -> Result<Json<Invite>, Internal> {
- let invite = app.invites().create(&login, &issued_at).await?;
- Ok(Json(invite))
-}
-
-async fn invite(
- State(app): State<App>,
- Path(invite): Path<Id>,
-) -> Result<Json<Summary>, 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<App>,
- RequestedAt(accepted_at): RequestedAt,
- identity: IdentityToken,
- Path(invite): Path<Id>,
- Json(request): Json<AcceptRequest>,
-) -> 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<App>,
+ Path(invite): Path<super::PathInfo>,
+) -> Result<Json<Summary>, 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<App>,
+ RequestedAt(accepted_at): RequestedAt,
+ identity: IdentityToken,
+ Path(invite): Path<super::PathInfo>,
+ Json(request): Json<Request>,
+) -> 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<App> {
+ 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<App>,
+ RequestedAt(issued_at): RequestedAt,
+ login: Login,
+ // Require `{}` as the only valid request for this endpoint.
+ _: Json<Request>,
+) -> Result<Json<Invite>, Internal> {
+ let invite = app.invites().create(&login, &issued_at).await?;
+ Ok(Json(invite))
+}
+
+#[derive(Default, serde::Deserialize)]
+pub struct Request {}