summaryrefslogtreecommitdiff
path: root/src/channel/routes.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/channel/routes.rs')
-rw-r--r--src/channel/routes.rs19
1 files changed, 12 insertions, 7 deletions
diff --git a/src/channel/routes.rs b/src/channel/routes.rs
index 383ec58..674c876 100644
--- a/src/channel/routes.rs
+++ b/src/channel/routes.rs
@@ -17,14 +17,17 @@ use crate::{
},
};
+#[cfg(test)]
+mod test;
+
pub fn router() -> Router<App> {
Router::new()
- .route("/api/channels", get(list_channels))
+ .route("/api/channels", get(list))
.route("/api/channels", post(on_create))
.route("/api/channels/:channel", post(on_send))
}
-async fn list_channels(State(app): State<App>, _: Login) -> Result<Channels, InternalError> {
+async fn list(State(app): State<App>, _: Login) -> Result<Channels, InternalError> {
let channels = app.channels().all().await?;
let response = Channels(channels);
@@ -40,7 +43,7 @@ impl IntoResponse for Channels {
}
}
-#[derive(serde::Deserialize)]
+#[derive(Clone, serde::Deserialize)]
struct CreateRequest {
name: String,
}
@@ -59,6 +62,7 @@ async fn on_create(
Ok(Json(channel))
}
+#[derive(Debug)]
struct CreateError(app::CreateError);
impl IntoResponse for CreateError {
@@ -73,20 +77,20 @@ impl IntoResponse for CreateError {
}
}
-#[derive(serde::Deserialize)]
+#[derive(Clone, serde::Deserialize)]
struct SendRequest {
message: String,
}
async fn on_send(
+ State(app): State<App>,
Path(channel): Path<channel::Id>,
RequestedAt(sent_at): RequestedAt,
- State(app): State<App>,
login: Login,
- Json(form): Json<SendRequest>,
+ Json(request): Json<SendRequest>,
) -> Result<StatusCode, ErrorResponse> {
app.channels()
- .send(&login, &channel, &form.message, &sent_at)
+ .send(&login, &channel, &request.message, &sent_at)
.await
// Could impl `From` here, but it's more code and this is used once.
.map_err(ErrorResponse)?;
@@ -94,6 +98,7 @@ async fn on_send(
Ok(StatusCode::ACCEPTED)
}
+#[derive(Debug)]
struct ErrorResponse(EventsError);
impl IntoResponse for ErrorResponse {