Skip to content

Commit 20240d4

Browse files
committed
feat: add new endpoint
1 parent d0b0afe commit 20240d4

File tree

7 files changed

+124
-28
lines changed

7 files changed

+124
-28
lines changed

src/app/auth/auth_controller.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub async fn post_login(Json(payload): Json<AuthDto>) -> impl IntoResponse {
2222
#[utoipa::path(
2323
post,
2424
path = "/api/auth/refresh",
25-
request_body(content = String, description = "Refresh token"),
25+
request_body(content = AuthRefreshDto, description = "Refresh token"),
2626
responses(
2727
(status = 200, description = "Token refreshed", body = AuthResponse),
2828
(status = 400, description = "Invalid refresh token", body = MessageResponse)

src/app/auth/auth_dto.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ pub struct AuthData {
2020
pub refresh_token: String,
2121
}
2222

23+
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
24+
pub struct AuthRefreshDto {
25+
pub refresh_token: String,
26+
}
27+
2328
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
2429
pub struct AuthResponse {
2530
pub data: AuthData,

src/app/auth/auth_service.rs

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
use super::auth_dto::AuthDto;
2-
use axum::Json;
1+
use axum::{
2+
http::StatusCode,
3+
response::{IntoResponse, Response},
4+
Json,
5+
};
36
use serde_json::json;
47
use std::env;
58
use utils::{
69
format_error, format_success, jwt::decode_refresh_token,
710
jwt::encode_access_token, jwt::encode_refresh_token,
811
};
912

13+
use super::auth_dto::AuthDto;
14+
1015
#[derive(serde::Serialize)]
1116
pub struct ErrorResponse {
1217
pub message: String,
@@ -18,33 +23,43 @@ pub struct LoginResponse {
1823
pub refresh_token: String,
1924
}
2025

21-
pub async fn login(Json(credentials): Json<AuthDto>) -> Json<serde_json::Value> {
26+
pub async fn login(Json(credentials): Json<AuthDto>) -> Response {
2227
let email = env::var("USER_EMAIL").expect("USER_EMAIL must be set");
2328
let password = env::var("USER_PASSWORD").expect("USER_PASSWORD must be set");
2429

2530
if credentials.email != email {
26-
return Json(format_error("Email or password is incorrect"));
31+
return (
32+
StatusCode::UNAUTHORIZED,
33+
Json(format_error("Email or password is incorrect")),
34+
)
35+
.into_response();
2736
}
2837

2938
if credentials.password != password {
30-
return Json(format_error("Email or password is incorrect"));
39+
return (
40+
StatusCode::UNAUTHORIZED,
41+
Json(format_error("Email or password is incorrect")),
42+
)
43+
.into_response();
3144
}
3245

3346
let access_token = encode_access_token(email.clone()).unwrap();
3447
let refresh_token = encode_refresh_token(email.clone()).unwrap();
3548

36-
Json(format_success(
37-
json!({
38-
"access_token": access_token,
39-
"refresh_token": refresh_token,
40-
}),
41-
None,
42-
))
49+
(
50+
StatusCode::OK,
51+
Json(format_success(
52+
json!({
53+
"access_token": access_token,
54+
"refresh_token": refresh_token,
55+
}),
56+
None,
57+
)),
58+
)
59+
.into_response()
4360
}
4461

45-
pub async fn refresh_token(
46-
Json(payload): Json<serde_json::Value>,
47-
) -> Json<serde_json::Value> {
62+
pub async fn refresh_token(Json(payload): Json<serde_json::Value>) -> Response {
4863
let refresh_token = payload
4964
.get("refresh_token")
5065
.and_then(|token| token.as_str())
@@ -55,13 +70,21 @@ pub async fn refresh_token(
5570
let access_token =
5671
encode_access_token(env::var("USER_EMAIL").unwrap().to_string())
5772
.unwrap();
58-
Json(format_success(
59-
json!({
60-
"access_token": access_token
61-
}),
62-
None,
63-
))
73+
(
74+
StatusCode::OK,
75+
Json(format_success(
76+
json!({
77+
"access_token": access_token
78+
}),
79+
None,
80+
)),
81+
)
82+
.into_response()
6483
}
65-
Err(_) => Json(format_error("Invalid or expired refresh token")),
84+
Err(_) => (
85+
StatusCode::UNAUTHORIZED,
86+
Json(format_error("Invalid or expired refresh token")),
87+
)
88+
.into_response(),
6689
}
6790
}

src/app/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,21 @@ pub async fn routes() -> Router {
2727
auth::auth_controller::post_login,
2828
auth::auth_controller::post_refresh,
2929
reservations::reservation_controller::create,
30-
reservations::reservation_controller::get_all
30+
reservations::reservation_controller::get_all,
31+
reservations::reservation_controller::get_by_id,
32+
reservations::reservation_controller::delete,
33+
reservations::reservation_controller::update
3134
),
3235
components(
3336
schemas(
3437
auth::auth_dto::AuthDto,
38+
auth::auth_dto::AuthRefreshDto,
3539
auth::auth_dto::AuthResponse,
40+
auth::auth_dto::AuthData,
3641
auth::auth_dto::MessageResponse,
3742
reservations::reservation_dto::ReservationDto,
3843
reservations::reservation_dto::ReservationListResponse,
44+
reservations::reservation_dto::ReservationDetailResponse,
3945
reservations::reservation_dto::TMetas
4046
)
4147
),

src/app/reservations/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ pub mod reservation_dto;
33
pub mod reservation_service;
44

55
use axum::{
6-
routing::{get, post},
6+
routing::{delete, get, post, put},
77
Router,
88
};
9-
use reservation_controller::{create, get_all};
9+
use reservation_controller::{
10+
create, delete as delete_c, get_all, get_by_id, update,
11+
};
1012

1113
pub fn reservation_router() -> Router {
1214
Router::new()
1315
.route("/", get(get_all))
16+
.route("/:id", get(get_by_id))
1417
.route("/create", post(create))
18+
.route("/delete/:id", delete(delete_c))
19+
.route("/update/:id", put(update))
1520
}

src/app/reservations/reservation_controller.rs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,23 @@ use axum::{extract::Query, response::IntoResponse, Json};
2121
)]
2222

2323
pub async fn get_all(Query(params): Query<TMetas>) -> impl IntoResponse {
24-
println!("Raw query params: {:?}", params);
24+
fetch_reservations(params).await
25+
}
26+
27+
#[utoipa::path(
28+
get,
29+
path = "/api/reservations/{id}",
30+
security(
31+
("Bearer" = [])
32+
),
33+
responses(
34+
(status = 201, description = "Detail Reservation", body = ReservationDetailResponse),
35+
(status = 400, description = "Invalid reservation data", body = MessageResponse)
36+
),
37+
tag = "Reservations"
38+
)]
39+
40+
pub async fn get_by_id(Query(params): Query<TMetas>) -> impl IntoResponse {
2541
fetch_reservations(params).await
2642
}
2743

@@ -33,7 +49,7 @@ pub async fn get_all(Query(params): Query<TMetas>) -> impl IntoResponse {
3349
("Bearer" = [])
3450
),
3551
responses(
36-
(status = 201, description = "Reservation created", body = ReservationDto),
52+
(status = 201, description = "Reservation created", body = MessageResponse),
3753
(status = 400, description = "Invalid reservation data", body = MessageResponse)
3854
),
3955
tag = "Reservations"
@@ -42,3 +58,38 @@ pub async fn get_all(Query(params): Query<TMetas>) -> impl IntoResponse {
4258
pub async fn create(Json(payload): Json<ReservationDto>) -> impl IntoResponse {
4359
create_reservation(Json(payload)).await
4460
}
61+
62+
#[utoipa::path(
63+
delete,
64+
path = "/api/reservations/delete/{id}",
65+
security(
66+
("Bearer" = [])
67+
),
68+
responses(
69+
(status = 201, description = "Reservation deleted", body = MessageResponse),
70+
(status = 400, description = "Invalid reservation data", body = MessageResponse)
71+
),
72+
tag = "Reservations"
73+
)]
74+
75+
pub async fn delete() -> impl IntoResponse {
76+
()
77+
}
78+
79+
#[utoipa::path(
80+
put,
81+
path = "/api/reservations/update/{id}",
82+
request_body = ReservationDto,
83+
security(
84+
("Bearer" = [])
85+
),
86+
responses(
87+
(status = 201, description = "Reservation updated", body = MessageResponse),
88+
(status = 400, description = "Invalid reservation data", body = MessageResponse)
89+
),
90+
tag = "Reservations"
91+
)]
92+
93+
pub async fn update(Json(payload): Json<ReservationDto>) -> impl IntoResponse {
94+
create_reservation(Json(payload)).await
95+
}

src/app/reservations/reservation_dto.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ pub struct ReservationDto {
1717
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
1818
pub struct ReservationListResponse {
1919
pub data: Vec<ReservationDto>,
20+
pub meta: Option<TMetas>,
21+
}
22+
23+
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
24+
pub struct ReservationDetailResponse {
25+
pub data: ReservationDto,
2026
}
2127

2228
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema, IntoParams)]

0 commit comments

Comments
 (0)