gitdataai/lib/api/src/user/profile.rs

64 lines
1.6 KiB
Rust

use actix_web::{HttpRequest, HttpResponse, web};
use serde::Serialize;
use service::{
AppService,
user::profile::{
AvatarUploadResponse, UpdateUserProfileConfig, UserProfileConfig,
},
};
use session::Session;
use crate::error::ApiError;
fn ok_json<T: Serialize>(data: T) -> Result<HttpResponse, ApiError> {
Ok(HttpResponse::Ok().json(data))
}
#[utoipa::path(
put,
path = "/api/v1/user/config/profile",
request_body = UpdateUserProfileConfig,
responses(
(status = 200, body = UserProfileConfig)
),
tag = "user"
)]
pub async fn update_profile(
service: web::Data<AppService>,
session: Session,
body: web::Json<UpdateUserProfileConfig>,
) -> Result<HttpResponse, ApiError> {
let config = service
.user_update_profile_config(&session, body.into_inner())
.await?;
ok_json(config)
}
#[utoipa::path(
post,
path = "/api/v1/user/avatar",
request_body(content = Vec<u8>, content_type = "image/*"),
responses(
(status = 200, body = AvatarUploadResponse),
(status = 400, description = "Invalid file type or size")
),
tag = "user"
)]
pub async fn upload_avatar(
service: web::Data<AppService>,
session: Session,
body: web::Bytes,
req: HttpRequest,
) -> Result<HttpResponse, ApiError> {
let content_type = req
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let response = service
.user_upload_avatar(&session, body.to_vec(), content_type)
.await?;
ok_json(response)
}