1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
use axum::{
extract::{Form, Path, State},
response::{IntoResponse, Redirect},
routing::post,
Router,
};
use sqlx::sqlite::SqlitePool;
use super::repo::{Id as ChannelId, Provider as _};
use crate::{error::InternalError, login::repo::logins::Login};
pub fn router() -> Router<SqlitePool> {
Router::new()
.route("/create", post(on_create))
.route("/join", post(on_join))
.route("/:channel/leave", post(on_leave))
}
#[derive(serde::Deserialize)]
struct CreateRequest {
name: String,
}
async fn on_create(
State(db): State<SqlitePool>,
login: Login,
Form(form): Form<CreateRequest>,
) -> Result<impl IntoResponse, InternalError> {
let mut tx = db.begin().await?;
let channel = tx.channels().create(&form.name).await?;
tx.channels().join(&channel.id, &login.id).await?;
tx.commit().await?;
Ok(Redirect::to("/"))
}
#[derive(serde::Deserialize)]
struct JoinRequest {
channel: ChannelId,
}
async fn on_join(
State(db): State<SqlitePool>,
login: Login,
Form(req): Form<JoinRequest>,
) -> Result<impl IntoResponse, InternalError> {
let mut tx = db.begin().await?;
tx.channels().join(&req.channel, &login.id).await?;
tx.commit().await?;
Ok(Redirect::to("/"))
}
async fn on_leave(
State(db): State<SqlitePool>,
login: Login,
Path(channel): Path<ChannelId>,
) -> Result<impl IntoResponse, InternalError> {
let mut tx = db.begin().await?;
tx.channels().leave(&channel, &login.id).await?;
tx.commit().await?;
Ok(Redirect::to("/"))
}
|