Skip to content

Commit

Permalink
Merge pull request #34 from traP-jp/feat/#9-get-users-userid
Browse files Browse the repository at this point in the history
Feat/#9 get users userId
  • Loading branch information
shobonvip authored Oct 1, 2024
2 parents 43536bd + 08d3b30 commit 64bfd99
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ pub fn make_router(app_state: Repository) -> Router {

let users_router = Router::new()
.route("/me", get(users::get_me).put(users::put_me))
.route("/me/email", put(users::put_me_email));
.route("/me/email", put(users::put_me_email))
.route("/:userId", get(users::get_user));

Router::new()
.nest("/", authentication_router)
Expand Down
32 changes: 27 additions & 5 deletions src/handler/users.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use axum::{extract::State, response::IntoResponse, Json};
use axum::{extract::Path, extract::State, response::IntoResponse, Json};
use axum_extra::{headers::Cookie, TypedHeader};
use lettre::Address;
use reqwest::StatusCode;
Expand Down Expand Up @@ -28,7 +28,8 @@ pub async fn get_me(
let user = state
.get_user_by_id(user_id)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
.ok_or(StatusCode::INTERNAL_SERVER_ERROR)?;

Ok(Json(user))
}
Expand Down Expand Up @@ -83,7 +84,7 @@ pub struct PutMeRequest {
}

// todo とりえずの仮置き
fn encode_icon_to_icon_url(icon: String) -> String {
fn encode_icon_to_icon_url(icon: Option<String>) -> Option<String> {
icon
}

Expand All @@ -105,11 +106,14 @@ pub async fn put_me(
let user = state
.get_user_by_id(user_id)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
.ok_or(StatusCode::INTERNAL_SERVER_ERROR)?;

let new_body = UpdateUser {
user_name: body.user_name.unwrap_or(user.name),
icon_url: body.icon.map_or(user.icon_url, encode_icon_to_icon_url),
icon_url: body
.icon
.map_or(user.icon_url, |icon| encode_icon_to_icon_url(Some(icon))),
x_link: body.x_link.or(user.x_link),
github_link: body.github_link.or(user.github_link),
self_introduction: body.self_introduction.unwrap_or(user.self_introduction),
Expand All @@ -124,3 +128,21 @@ pub async fn put_me(

Ok(Json(serde_json::json!({"iconUrl": icon_url})))
}

pub async fn get_user(
State(state): State<Repository>,
Path(user_id): Path<String>,
) -> anyhow::Result<impl IntoResponse, StatusCode> {
let user_id: i64 = match user_id.parse() {
Ok(num) => num,
Err(_) => return Err(StatusCode::BAD_REQUEST),
};

let user = state
.get_user_by_id(user_id)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
.ok_or(StatusCode::NOT_FOUND)?;

Ok(Json(user))
}
9 changes: 5 additions & 4 deletions src/repository/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct User {
pub name: String,
pub traq_id: Option<String>,
pub github_id: Option<String>,
pub icon_url: String,
pub icon_url: Option<String>,
pub x_link: Option<String>,
pub github_link: Option<String>,
pub self_introduction: String,
Expand All @@ -33,21 +33,22 @@ pub struct User {

pub struct UpdateUser {
pub user_name: String,
pub icon_url: String,
pub icon_url: Option<String>,
pub x_link: Option<String>,
pub github_link: Option<String>,
pub self_introduction: String,
}

impl Repository {
pub async fn get_user_by_id(&self, user_id: i64) -> anyhow::Result<User> {
pub async fn get_user_by_id(&self, user_id: i64) -> anyhow::Result<Option<User>> {
let user = sqlx::query_as::<_, User>("SELECT * FROM users WHERE id = ?")
.bind(user_id)
.fetch_one(&self.pool)
.fetch_optional(&self.pool)
.await?;

Ok(user)
}

pub async fn update_user(&self, user_id: i64, body: UpdateUser) -> anyhow::Result<()> {
sqlx::query("UPDATE users SET name = ?, icon_url = ?, x_link = ?, github_link = ?, self_introduction = ? WHERE id = ?")
.bind(body.user_name)
Expand Down

0 comments on commit 64bfd99

Please sign in to comment.