summaryrefslogtreecommitdiff
path: root/src/invite/routes
diff options
context:
space:
mode:
Diffstat (limited to 'src/invite/routes')
-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.rs52
-rw-r--r--src/invite/routes/mod.rs16
-rw-r--r--src/invite/routes/post.rs19
5 files changed, 130 insertions, 0 deletions
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..3ca4e6b
--- /dev/null
+++ b/src/invite/routes/invite/post.rs
@@ -0,0 +1,52 @@
+use axum::{
+ extract::{Json, Path, State},
+ http::StatusCode,
+ response::{IntoResponse, Response},
+};
+
+use crate::{
+ app::App,
+ clock::RequestedAt,
+ error::{Internal, NotFound},
+ invite::app,
+ login::{Login, Password},
+ name::Name,
+ token::extract::IdentityCookie,
+};
+
+pub async fn handler(
+ State(app): State<App>,
+ RequestedAt(accepted_at): RequestedAt,
+ identity: IdentityCookie,
+ Path(invite): Path<super::PathInfo>,
+ Json(request): Json<Request>,
+) -> Result<(IdentityCookie, Json<Login>), Error> {
+ let (login, secret) = app
+ .invites()
+ .accept(&invite, &request.name, &request.password, &accepted_at)
+ .await
+ .map_err(Error)?;
+ let identity = identity.set(secret);
+ Ok((identity, Json(login)))
+}
+
+#[derive(serde::Deserialize)]
+pub struct Request {
+ pub name: Name,
+ 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..eb7d706
--- /dev/null
+++ b/src/invite/routes/post.rs
@@ -0,0 +1,19 @@
+use axum::extract::{Json, State};
+
+use crate::{
+ app::App, clock::RequestedAt, error::Internal, invite::Invite, token::extract::Identity,
+};
+
+pub async fn handler(
+ State(app): State<App>,
+ RequestedAt(issued_at): RequestedAt,
+ identity: Identity,
+ _: Json<Request>,
+) -> Result<Json<Invite>, Internal> {
+ let invite = app.invites().create(&identity.login, &issued_at).await?;
+ Ok(Json(invite))
+}
+
+// Require `{}` as the only valid request for this endpoint.
+#[derive(Default, serde::Deserialize)]
+pub struct Request {}