Skip to content

Commit

Permalink
構造体の追加、定義場所の変更
Browse files Browse the repository at this point in the history
  • Loading branch information
cho3881392697 committed Sep 25, 2024
1 parent 9a7b5d7 commit dc39589
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
33 changes: 29 additions & 4 deletions src/handler/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use reqwest::StatusCode;
use serde::Deserialize;
use validator::Validate;

use crate::repository::users::PutMeRequest;
use crate::repository::users::UpdateUser;
use crate::repository::Repository;

#[derive(Deserialize)]
Expand Down Expand Up @@ -67,6 +67,26 @@ https://link/{jwt}"
Ok(StatusCode::NO_CONTENT)
}

#[derive(serde::Deserialize, Validate, Clone)]
#[serde(rename_all = "camelCase")]
pub struct PutMeRequest {
#[validate(length(min = 1, max = 255))]
pub user_name: Option<String>,
#[validate(length(max = 255))]
pub icon: Option<String>,
#[validate(length(max = 255))]
pub x_link: Option<String>,
#[validate(length(max = 255))]
pub github_link: Option<String>,
#[validate(length(max = 10000))]
pub self_introduction: Option<String>,
}

// todo どのファイルに書くべきかわからない&関数名がイケてない
async fn encode_icon_to_icon_url(icon: Option<String>) -> Option<String> {
icon
}

pub async fn put_me(
State(state): State<Repository>,
TypedHeader(cookie): TypedHeader<Cookie>,
Expand All @@ -82,11 +102,16 @@ pub async fn put_me(
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
.ok_or(StatusCode::UNAUTHORIZED)?;

// todo: icon -> icon_url
let new_body = body.clone();
let new_body = UpdateUser {
user_name: body.user_name,
icon_url: encode_icon_to_icon_url(body.icon).await,
x_link: body.x_link,
github_link: body.github_link,
self_introduction: body.self_introduction,
};

// iconの値がない場合空文字列を返すことになるが、この挙動はよくない気がする
let icon_url = new_body.icon.clone().unwrap_or_default();
let icon_url = new_body.icon_url.clone().unwrap_or_default();

state
.update_user(user_id, new_body)
Expand Down
16 changes: 4 additions & 12 deletions src/repository/users.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use serde::Serialize;
use sqlx::{types::chrono, FromRow};
use validator::Validate;

use super::Repository;

Expand Down Expand Up @@ -32,18 +31,11 @@ pub struct User {
pub updated_at: chrono::DateTime<chrono::Utc>,
}

#[derive(serde::Deserialize, Validate, Clone)]
#[serde(rename_all = "camelCase")]
pub struct PutMeRequest {
#[validate(length(min = 1, max = 255))]
pub struct UpdateUser {
pub user_name: Option<String>,
#[validate(length(max = 255))]
pub icon: Option<String>,
#[validate(length(max = 255))]
pub icon_url: Option<String>,
pub x_link: Option<String>,
#[validate(length(max = 255))]
pub github_link: Option<String>,
#[validate(length(max = 10000))]
pub self_introduction: Option<String>,
}

Expand All @@ -56,11 +48,11 @@ impl Repository {

Ok(user)
}
pub async fn update_user(&self, user_id: i64, body: PutMeRequest) -> anyhow::Result<()> {
pub async fn update_user(&self, user_id: i64, body: UpdateUser) -> anyhow::Result<()> {
let user = self.get_user_by_id(user_id).await?;
sqlx::query("UPDATE users SET name = ?, icon_url = ?, x_link = ?, github_link = ?, self_introduction = ? WHERE id = ?")
.bind(body.user_name.unwrap_or(user.name))
.bind(body.icon.unwrap_or(user.icon_url))
.bind(body.icon_url.unwrap_or(user.icon_url))
.bind(body.x_link.unwrap_or(user.x_link.unwrap_or_default()))
.bind(body.github_link.unwrap_or(user.github_link.unwrap_or_default()))
.bind(body.self_introduction.unwrap_or(user.self_introduction))
Expand Down

0 comments on commit dc39589

Please sign in to comment.