make transactions higher level (pass them everywhere)

This commit is contained in:
2026-04-03 19:05:29 +02:00
parent e4cccde466
commit 449136ce37
25 changed files with 283 additions and 217 deletions

View File

@@ -9,6 +9,7 @@ use uuid::Uuid;
use crate::{
api::CompositeError,
database,
persons::{Name, Person},
users::{
User,
@@ -21,21 +22,24 @@ pub const CANT_SET_PRIMARYNAME: &str = "You don't have permission to swap primar
pub async fn get_all(headers: HeaderMap) -> Result<Response, CompositeError> {
User::authenticate(&headers)?.required()?;
Ok(Json(Person::get_all()?).into_response())
let conn = database::conn()?;
Ok(Json(Person::get_all(&conn)?).into_response())
}
pub async fn get_by_id(
Path(id): Path<Uuid>,
headers: HeaderMap,
) -> Result<Response, CompositeError> {
User::authenticate(&headers)?.required()?;
Ok(Json(Person::get_by_id(id)?).into_response())
let conn = database::conn()?;
Ok(Json(Person::get_by_id(&conn, id)?).into_response())
}
pub async fn pid_names(
Path(id): Path<Uuid>,
headers: HeaderMap,
) -> Result<Response, CompositeError> {
User::authenticate(&headers)?.required()?;
Ok(Json(Person::get_by_id(id)?.get_all_names()?).into_response())
let conn = database::conn()?;
Ok(Json(Person::get_by_id(&conn, id)?.get_all_names(&conn)?).into_response())
}
#[derive(Deserialize)]
@@ -48,7 +52,8 @@ pub async fn create(
Json(form): Json<PersonNameForm>,
) -> Result<Response, CompositeError> {
let u = User::authenticate(&headers)?.required()?;
let p = Person::create(form.name, u.id)?;
let conn = database::conn()?;
let p = Person::create(&conn, form.name, u.id)?;
Ok((StatusCode::CREATED, Json(p)).into_response())
}
pub async fn add_name(
@@ -57,27 +62,30 @@ pub async fn add_name(
Json(form): Json<PersonNameForm>,
) -> Result<Response, CompositeError> {
let u = User::authenticate(&headers)?.required()?;
let p = Person::get_by_id(id)?;
let n = p.add_name(form.name, u.id)?;
let conn = database::conn()?;
let p = Person::get_by_id(&conn, id)?;
let n = p.add_name(&conn, form.name, u.id)?;
Ok((StatusCode::CREATED, Json(n)).into_response())
}
pub async fn n_by_id(Path(id): Path<Uuid>, headers: HeaderMap) -> Result<Response, CompositeError> {
User::authenticate(&headers)?.required()?;
Ok(Json(Name::get_by_id(id)?).into_response())
let conn = database::conn()?;
Ok(Json(Name::get_by_id(&conn, id)?).into_response())
}
pub async fn n_setprimary(
Path(id): Path<Uuid>,
headers: HeaderMap,
) -> Result<Response, CompositeError> {
let u = User::authenticate(&headers)?.required()?;
if !u.has_permission(Permission::ChangePersonPrimaryName)? {
let conn = database::conn()?;
if !u.has_permission(&conn, Permission::ChangePersonPrimaryName)? {
return Ok((StatusCode::FORBIDDEN, CANT_SET_PRIMARYNAME).into_response());
}
let mut n = Name::get_by_id(id)?;
n.set_primary()?;
let mut n = Name::get_by_id(&conn, id)?;
n.set_primary(&conn)?;
n.is_primary = true;
Ok(Json(n).into_response())
}