Skip to content

Commit f4630d2

Browse files
committed
change: problem_list with actix-web
1 parent e7eb907 commit f4630d2

File tree

1 file changed

+130
-103
lines changed

1 file changed

+130
-103
lines changed
Lines changed: 130 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,162 @@
1-
use crate::server::utils::RequestUnpack;
1+
use crate::server::utils::GetAuthId;
22
use crate::server::{AppData, Authentication, CommonResponse};
3+
4+
use actix_web::{error, web, HttpRequest, HttpResponse, Result};
35
use serde::Deserialize;
46
use sql_client::internal::problem_list_manager::ProblemListManager;
5-
use tide::{Request, Response, Result};
6-
7-
pub(crate) async fn get_own_lists<A>(request: Request<AppData<A>>) -> Result<Response>
8-
where
9-
A: Authentication + Clone + Send + Sync + 'static,
10-
{
11-
let user_id = request.get_authorized_id().await?;
12-
let conn = request.state().pg_pool.clone();
13-
let list = conn.get_list(&user_id).await?;
14-
let response = Response::json(&list)?;
7+
8+
pub(crate) async fn get_own_lists<A: Authentication + Clone + Send + Sync + 'static>(
9+
request: HttpRequest,
10+
data: web::Data<AppData<A>>,
11+
) -> Result<HttpResponse> {
12+
let user_id = data.get_authorized_id(request.cookie("token")).await?;
13+
let conn = data.pg_pool.clone();
14+
let list = conn
15+
.get_list(&user_id)
16+
.await
17+
.map_err(error::ErrorInternalServerError)?;
18+
let response = HttpResponse::json(&list)?;
1519
Ok(response)
1620
}
1721

18-
pub(crate) async fn get_single_list<A>(request: Request<AppData<A>>) -> Result<Response>
19-
where
20-
A: Authentication + Clone + Send + Sync + 'static,
21-
{
22-
let list_id = request.param("list_id")?;
23-
let conn = request.state().pg_pool.clone();
24-
let list = conn.get_single_list(list_id).await?;
25-
let response = Response::json(&list)?;
22+
#[derive(Deserialize)]
23+
struct GetListQuery {
24+
list_id: String,
25+
}
26+
27+
pub(crate) async fn get_single_list<A: Authentication + Clone + Send + Sync + 'static>(
28+
request: HttpRequest,
29+
data: web::Data<AppData<A>>,
30+
query: web::Query<GetListQuery>,
31+
) -> Result<HttpResponse> {
32+
let user_id = data.get_authorized_id(request.cookie("token")).await?;
33+
let conn = data.pg_pool.clone();
34+
let list_id = &query.list_id;
35+
let list = conn
36+
.get_single_list(list_id)
37+
.await
38+
.map_err(error::ErrorInternalServerError)?;
39+
let response = HttpResponse::json(&list)?;
2640
Ok(response)
2741
}
2842

29-
pub(crate) async fn create_list<A>(request: Request<AppData<A>>) -> Result<Response>
30-
where
31-
A: Authentication + Clone + Send + Sync + 'static,
32-
{
33-
#[derive(Deserialize)]
34-
struct Query {
35-
list_name: String,
36-
}
37-
let internal_user_id = request.get_authorized_id().await?;
38-
let conn = request.state().pg_pool.clone();
39-
let query = request.parse_body::<Query>().await?;
43+
#[derive(Deserialize)]
44+
struct CreateListQuery {
45+
list_name: String,
46+
}
47+
48+
pub(crate) async fn create_list<A: Authentication + Clone + Send + Sync + 'static>(
49+
request: HttpRequest,
50+
data: web::Data<AppData<A>>,
51+
query: web::Json<CreateListQuery>,
52+
) -> Result<HttpResponse> {
53+
let user_id = data.get_authorized_id(request.cookie("token")).await?;
54+
let conn = data.pg_pool.clone();
4055
let internal_list_id = conn
41-
.create_list(&internal_user_id, &query.list_name)
42-
.await?;
56+
.create_list(&user_id, &query.list_name)
57+
.await
58+
.map_err(error::ErrorInternalServerError)?;
4359
let body = serde_json::json!({ "internal_list_id": internal_list_id });
44-
let response = Response::json(&body)?;
60+
let response = HttpResponse::json(&body)?;
4561
Ok(response)
4662
}
4763

48-
pub(crate) async fn delete_list<A>(request: Request<AppData<A>>) -> Result<Response>
49-
where
50-
A: Authentication + Clone + Send + Sync + 'static,
51-
{
52-
#[derive(Deserialize)]
53-
struct Q {
54-
internal_list_id: String,
55-
}
56-
request.get_authorized_id().await?;
57-
let conn = request.state().pg_pool.clone();
58-
let query = request.parse_body::<Q>().await?;
59-
conn.delete_list(&query.internal_list_id).await?;
60-
let response = Response::empty_json();
64+
#[derive(Deserialize)]
65+
struct DeleteListQuery {
66+
internal_list_id: String,
67+
}
68+
69+
pub(crate) async fn delete_list<A: Authentication + Clone + Send + Sync + 'static>(
70+
request: HttpRequest,
71+
data: web::Data<AppData<A>>,
72+
query: web::Json<DeleteListQuery>,
73+
) -> Result<HttpResponse> {
74+
let user_id = data.get_authorized_id(request.cookie("token")).await?;
75+
let conn = data.pg_pool.clone();
76+
conn.delete_list(&query.internal_list_id)
77+
.await
78+
.map_err(error::ErrorInternalServerError)?;
79+
let response = HttpResponse::empty_json();
6180
Ok(response)
6281
}
6382

64-
pub(crate) async fn update_list<A>(request: Request<AppData<A>>) -> Result<Response>
65-
where
66-
A: Authentication + Clone + Send + Sync + 'static,
67-
{
68-
#[derive(Deserialize)]
69-
struct Q {
70-
internal_list_id: String,
71-
name: String,
72-
}
73-
request.get_authorized_id().await?;
74-
let conn = request.state().pg_pool.clone();
75-
let query = request.parse_body::<Q>().await?;
83+
#[derive(Deserialize)]
84+
struct UpdateListQuery {
85+
internal_list_id: String,
86+
name: String,
87+
}
88+
89+
pub(crate) async fn update_list<A: Authentication + Clone + Send + Sync + 'static>(
90+
request: HttpRequest,
91+
data: web::Data<AppData<A>>,
92+
query: web::Json<UpdateListQuery>,
93+
) -> Result<HttpResponse> {
94+
let user_id = data.get_authorized_id(request.cookie("token")).await?;
95+
let conn = data.pg_pool.clone();
7696
conn.update_list(&query.internal_list_id, &query.name)
77-
.await?;
78-
let response = Response::empty_json();
97+
.await
98+
.map_err(error::ErrorInternalServerError)?;
99+
let response = HttpResponse::empty_json();
79100
Ok(response)
80101
}
81102

82-
pub(crate) async fn add_item<A>(request: Request<AppData<A>>) -> Result<Response>
83-
where
84-
A: Authentication + Clone + Send + Sync + 'static,
85-
{
86-
#[derive(Deserialize)]
87-
struct Q {
88-
internal_list_id: String,
89-
problem_id: String,
90-
}
91-
request.get_authorized_id().await?;
92-
let conn = request.state().pg_pool.clone();
93-
let query = request.parse_body::<Q>().await?;
103+
#[derive(Deserialize)]
104+
struct AddItemQuery {
105+
internal_list_id: String,
106+
problem_id: String,
107+
}
108+
109+
pub(crate) async fn add_item<A: Authentication + Clone + Send + Sync + 'static>(
110+
request: HttpRequest,
111+
data: web::Data<AppData<A>>,
112+
query: web::Json<AddItemQuery>,
113+
) -> Result<HttpResponse> {
114+
let user_id = data.get_authorized_id(request.cookie("token")).await?;
115+
let conn = data.pg_pool.clone();
94116
conn.add_item(&query.internal_list_id, &query.problem_id)
95-
.await?;
96-
let response = Response::empty_json();
117+
.await
118+
.map_err(error::ErrorInternalServerError)?;
119+
let response = HttpResponse::empty_json();
97120
Ok(response)
98121
}
99122

100-
pub(crate) async fn update_item<A>(request: Request<AppData<A>>) -> Result<Response>
101-
where
102-
A: Authentication + Clone + Send + Sync + 'static,
103-
{
104-
#[derive(Deserialize)]
105-
struct Q {
106-
internal_list_id: String,
107-
problem_id: String,
108-
memo: String,
109-
}
110-
request.get_authorized_id().await?;
111-
let conn = request.state().pg_pool.clone();
112-
let query = request.parse_body::<Q>().await?;
123+
#[derive(Deserialize)]
124+
struct UpdateItemQuery {
125+
internal_list_id: String,
126+
problem_id: String,
127+
memo: String,
128+
}
129+
130+
pub(crate) async fn update_item<A: Authentication + Clone + Send + Sync + 'static>(
131+
request: HttpRequest,
132+
data: web::Data<AppData<A>>,
133+
query: web::Json<UpdateItemQuery>,
134+
) -> Result<HttpResponse> {
135+
let user_id = data.get_authorized_id(request.cookie("token")).await?;
136+
let conn = data.pg_pool.clone();
113137
conn.update_item(&query.internal_list_id, &query.problem_id, &query.memo)
114-
.await?;
115-
let response = Response::empty_json();
138+
.await
139+
.map_err(error::ErrorInternalServerError)?;
140+
let response = HttpResponse::empty_json();
116141
Ok(response)
117142
}
118143

119-
pub(crate) async fn delete_item<A>(request: Request<AppData<A>>) -> Result<Response>
120-
where
121-
A: Authentication + Clone + Send + Sync + 'static,
122-
{
123-
#[derive(Deserialize)]
124-
struct Q {
125-
internal_list_id: String,
126-
problem_id: String,
127-
}
128-
request.get_authorized_id().await?;
129-
let conn = request.state().pg_pool.clone();
130-
let query = request.parse_body::<Q>().await?;
144+
#[derive(Deserialize)]
145+
struct DeleteItemQuery {
146+
internal_list_id: String,
147+
problem_id: String,
148+
}
149+
150+
pub(crate) async fn delete_item<A: Authentication + Clone + Send + Sync + 'static>(
151+
request: HttpRequest,
152+
data: web::Data<AppData<A>>,
153+
query: web::Json<DeleteItemQuery>,
154+
) -> Result<HttpResponse> {
155+
let user_id = data.get_authorized_id(request.cookie("token")).await?;
156+
let conn = data.pg_pool.clone();
131157
conn.delete_item(&query.internal_list_id, &query.problem_id)
132-
.await?;
133-
let response = Response::empty_json();
158+
.await
159+
.map_err(error::ErrorInternalServerError)?;
160+
let response = HttpResponse::empty_json();
134161
Ok(response)
135162
}

0 commit comments

Comments
 (0)