1
0
Fork 0
forked from gorb/backend

feat: move me endpoint to /me and add /me/servers

This commit is contained in:
Radical 2025-05-27 07:46:10 +00:00
parent b8cf21903e
commit 39d01bb0d0
7 changed files with 61 additions and 8 deletions

96
src/api/v1/me/mod.rs Normal file
View file

@ -0,0 +1,96 @@
use actix_multipart::form::{MultipartForm, json::Json as MpJson, tempfile::TempFile};
use actix_web::{get, patch, web, HttpRequest, HttpResponse, Scope};
use serde::Deserialize;
use crate::{
Data, api::v1::auth::check_access_token, error::Error, structs::Me, utils::get_auth_header,
};
mod servers;
pub fn web() -> Scope {
web::scope("/me")
.service(get)
.service(update)
}
#[get("")]
pub async fn get(req: HttpRequest, data: web::Data<Data>) -> Result<HttpResponse, Error> {
let headers = req.headers();
let auth_header = get_auth_header(headers)?;
let mut conn = data.pool.get().await?;
let uuid = check_access_token(auth_header, &mut conn).await?;
let me = Me::get(&mut conn, uuid).await?;
Ok(HttpResponse::Ok().json(me))
}
#[derive(Debug, Deserialize)]
struct NewInfo {
username: Option<String>,
display_name: Option<String>,
password: Option<String>,
email: Option<String>,
}
#[derive(Debug, MultipartForm)]
struct UploadForm {
#[multipart(limit = "100MB")]
avatar: Option<TempFile>,
json: Option<MpJson<NewInfo>>,
}
#[patch("")]
pub async fn update(
req: HttpRequest,
MultipartForm(form): MultipartForm<UploadForm>,
data: web::Data<Data>,
) -> Result<HttpResponse, Error> {
let headers = req.headers();
let auth_header = get_auth_header(headers)?;
let mut conn = data.pool.get().await?;
let uuid = check_access_token(auth_header, &mut conn).await?;
let mut me = Me::get(&mut conn, uuid).await?;
if let Some(avatar) = form.avatar {
let bytes = tokio::fs::read(avatar.file).await?;
let byte_slice: &[u8] = &bytes;
me.set_avatar(
&data.bunny_cdn,
&mut conn,
data.config.bunny.cdn_url.clone(),
byte_slice.into(),
)
.await?;
}
if let Some(new_info) = form.json {
if let Some(username) = &new_info.username {
todo!();
}
if let Some(display_name) = &new_info.display_name {
todo!();
}
if let Some(password) = &new_info.password {
todo!();
}
if let Some(email) = &new_info.email {
todo!();
}
}
Ok(HttpResponse::Ok().finish())
}