feat: migrate devices api to repository

This commit is contained in:
Jef Roosens 2025-02-27 22:08:49 +01:00
parent 952f92c178
commit a2233d9da8
No known key found for this signature in database
GPG key ID: 21FD3D77D56BAF49
8 changed files with 206 additions and 50 deletions

View file

@ -6,13 +6,12 @@ use axum::{
};
use crate::{
db,
gpodder::{self, DeviceRepository},
server::{
error::{AppError, AppResult},
gpodder::{
auth_middleware,
format::{Format, StringWithFormat},
models::{Device, DevicePatch, DeviceType},
},
Context,
},
@ -28,8 +27,8 @@ pub fn router(ctx: Context) -> Router<Context> {
async fn get_devices(
State(ctx): State<Context>,
Path(username): Path<StringWithFormat>,
Extension(user): Extension<db::User>,
) -> AppResult<Json<Vec<Device>>> {
Extension(user): Extension<gpodder::User>,
) -> AppResult<Json<Vec<gpodder::Device>>> {
if username.format != Format::Json {
return Err(AppError::NotFound);
}
@ -38,56 +37,27 @@ async fn get_devices(
return Err(AppError::BadRequest);
}
let devices = tokio::task::spawn_blocking(move || db::Device::for_user(&ctx.pool, user.id))
.await
.unwrap()?
.into_iter()
.map(|d| Device {
id: d.device_id,
caption: d.caption,
r#type: d.type_.into(),
// TODO implement subscription count
subscriptions: 0,
})
.collect();
Ok(Json(devices))
Ok(
tokio::task::spawn_blocking(move || ctx.repo.devices_for_user(&user))
.await
.unwrap()
.map(Json)?,
)
}
async fn post_device(
State(ctx): State<Context>,
Path((_username, id)): Path<(String, StringWithFormat)>,
Extension(user): Extension<db::User>,
Json(patch): Json<DevicePatch>,
Extension(user): Extension<gpodder::User>,
Json(patch): Json<gpodder::DevicePatch>,
) -> AppResult<()> {
if id.format != Format::Json {
return Err(AppError::NotFound);
}
tokio::task::spawn_blocking(move || {
if let Some(mut device) = db::Device::by_device_id(&ctx.pool, user.id, &id)? {
if let Some(caption) = patch.caption {
device.caption = caption;
}
if let Some(type_) = patch.r#type {
device.type_ = type_.into();
}
device.update(&ctx.pool)
} else {
db::NewDevice::new(
user.id,
id.to_string(),
patch.caption.unwrap_or(String::new()),
patch.r#type.unwrap_or(DeviceType::Other).into(),
)
.insert(&ctx.pool)
.map(|_| ())
}
})
.await
.unwrap()?;
tokio::task::spawn_blocking(move || ctx.repo.update_device_info(&user, &id, patch))
.await
.unwrap()?;
Ok(())
}

View file

@ -20,7 +20,7 @@ use axum_extra::{
};
use tower_http::set_header::SetResponseHeaderLayer;
use crate::{db, server::error::AppError};
use crate::{db, gpodder, server::error::AppError};
use super::Context;
@ -90,7 +90,11 @@ pub async fn auth_middleware(State(ctx): State<Context>, mut req: Request, next:
}
if let Some(user) = auth_user {
req.extensions_mut().insert(user);
req.extensions_mut().insert(user.clone());
req.extensions_mut().insert(gpodder::User {
username: user.username,
id: user.id,
});
let res = next.run(req).await;
if let Some(session_id) = new_session_id {
@ -117,3 +121,14 @@ pub async fn auth_middleware(State(ctx): State<Context>, mut req: Request, next:
res
}
}
impl From<gpodder::AuthErr> for AppError {
fn from(value: gpodder::AuthErr) -> Self {
match value {
gpodder::AuthErr::UnknownUser
| gpodder::AuthErr::UnknownSession
| gpodder::AuthErr::InvalidPassword => Self::Unauthorized,
gpodder::AuthErr::Other(err) => Self::Other(err),
}
}
}