remove all that .map_err nonsense at once
This commit is contained in:
@@ -1,18 +1,8 @@
|
||||
use axum::{
|
||||
Router,
|
||||
response::{IntoResponse, Response},
|
||||
routing::{delete, get, patch, post},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
database::DatabaseError,
|
||||
persons::PersonError,
|
||||
quotes::QuoteError,
|
||||
tags::TagError,
|
||||
users::{UserError, auth::AuthError, sessions::SessionError},
|
||||
web::RedirectViaError,
|
||||
};
|
||||
|
||||
mod auth;
|
||||
mod persons;
|
||||
mod quotes;
|
||||
@@ -58,32 +48,3 @@ pub fn api_router() -> Router {
|
||||
.route("/api/quotes", post(quotes::create))
|
||||
.route("/api/quotes/{id}", get(quotes::get_by_id))
|
||||
}
|
||||
|
||||
pub struct CompositeError(Response);
|
||||
impl IntoResponse for CompositeError {
|
||||
fn into_response(self) -> Response {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! composite_from {
|
||||
($($t:ty),+ $(,)?) => {
|
||||
$(
|
||||
impl From<$t> for CompositeError {
|
||||
fn from(e: $t) -> Self {
|
||||
CompositeError(e.into_response())
|
||||
}
|
||||
}
|
||||
)+
|
||||
};
|
||||
}
|
||||
composite_from!(
|
||||
AuthError,
|
||||
UserError,
|
||||
SessionError,
|
||||
TagError,
|
||||
PersonError,
|
||||
QuoteError,
|
||||
DatabaseError,
|
||||
RedirectViaError,
|
||||
);
|
||||
|
||||
@@ -8,8 +8,8 @@ use serde::Deserialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
api::CompositeError,
|
||||
database::{self, DatabaseError},
|
||||
database::{self},
|
||||
error::CompositeError,
|
||||
logs::{LogAction, LogEntry},
|
||||
persons::{Name, Person},
|
||||
users::{
|
||||
@@ -54,7 +54,7 @@ pub async fn create(
|
||||
) -> Result<Response, CompositeError> {
|
||||
let u = User::authenticate(&headers)?.required()?;
|
||||
let mut conn = database::conn()?;
|
||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
||||
let tx = conn.transaction()?;
|
||||
|
||||
let p = Person::create(&tx, form.name, u.id)?;
|
||||
LogEntry::new(
|
||||
@@ -65,7 +65,7 @@ pub async fn create(
|
||||
pname: p.primary_name.as_str().to_string(),
|
||||
},
|
||||
)?;
|
||||
tx.commit().map_err(DatabaseError::from)?;
|
||||
tx.commit()?;
|
||||
Ok((StatusCode::CREATED, Json(p)).into_response())
|
||||
}
|
||||
pub async fn add_name(
|
||||
@@ -75,7 +75,7 @@ pub async fn add_name(
|
||||
) -> Result<Response, CompositeError> {
|
||||
let u = User::authenticate(&headers)?.required()?;
|
||||
let mut conn = database::conn()?;
|
||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
||||
let tx = conn.transaction()?;
|
||||
|
||||
let p = Person::get_by_id(&tx, id)?;
|
||||
let n = p.add_name(&tx, form.name, u.id)?;
|
||||
@@ -89,7 +89,7 @@ pub async fn add_name(
|
||||
nn: n.name.clone(),
|
||||
},
|
||||
)?;
|
||||
tx.commit().map_err(DatabaseError::from)?;
|
||||
tx.commit()?;
|
||||
Ok((StatusCode::CREATED, Json(n)).into_response())
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ pub async fn n_setprimary(
|
||||
) -> Result<Response, CompositeError> {
|
||||
let u = User::authenticate(&headers)?.required()?;
|
||||
let mut conn = database::conn()?;
|
||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
||||
let tx = conn.transaction()?;
|
||||
|
||||
if !u.has_permission(&tx, Permission::ChangePersonPrimaryName)? {
|
||||
return Ok((StatusCode::FORBIDDEN, CANT_SET_PRIMARYNAME).into_response());
|
||||
@@ -124,7 +124,7 @@ pub async fn n_setprimary(
|
||||
nn: n.name.clone(),
|
||||
},
|
||||
)?;
|
||||
tx.commit().map_err(DatabaseError::from)?;
|
||||
tx.commit()?;
|
||||
|
||||
Ok(Json(n).into_response())
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ use serde::Deserialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
api::CompositeError,
|
||||
database::{self, DatabaseError},
|
||||
database::{self},
|
||||
error::CompositeError,
|
||||
logs::{LogAction, LogEntry},
|
||||
persons::Name,
|
||||
quotes::Quote,
|
||||
@@ -50,7 +50,7 @@ pub async fn create(
|
||||
) -> Result<Response, CompositeError> {
|
||||
let u = User::authenticate(&headers)?.required()?;
|
||||
let mut conn = database::conn()?;
|
||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
||||
let tx = conn.transaction()?;
|
||||
|
||||
let lines = form
|
||||
.lines
|
||||
@@ -69,6 +69,6 @@ pub async fn create(
|
||||
)?;
|
||||
|
||||
LogEntry::new(&tx, u, LogAction::CreateQuote { id: q.id })?;
|
||||
tx.commit().map_err(DatabaseError::from)?;
|
||||
tx.commit()?;
|
||||
Ok((StatusCode::CREATED, Json(q)).into_response())
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@ use axum::{
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
api::CompositeError,
|
||||
database::{self, DatabaseError},
|
||||
database::{self},
|
||||
error::CompositeError,
|
||||
logs::{LogAction, LogEntry},
|
||||
users::{
|
||||
User,
|
||||
@@ -43,7 +43,7 @@ pub async fn revoke_by_id(
|
||||
) -> Result<Response, CompositeError> {
|
||||
let u = User::authenticate(&headers)?.required()?;
|
||||
let mut conn = database::conn()?;
|
||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
||||
let tx = conn.transaction()?;
|
||||
|
||||
let mut s = Session::get_by_id(&tx, id)?;
|
||||
match s.user_id == u.id
|
||||
@@ -53,7 +53,7 @@ pub async fn revoke_by_id(
|
||||
true => {
|
||||
s.revoke(&tx, Some(&u))?;
|
||||
LogEntry::new(&tx, u, LogAction::ManuallyRevokeSession { id })?;
|
||||
tx.commit().map_err(DatabaseError::from)?;
|
||||
tx.commit()?;
|
||||
Ok(Json(s).into_response())
|
||||
}
|
||||
false => match u.has_permission(&tx, Permission::ListOthersSessions)? {
|
||||
|
||||
@@ -8,8 +8,8 @@ use serde::Deserialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
api::CompositeError,
|
||||
database::{self, DatabaseError},
|
||||
database::{self},
|
||||
error::CompositeError,
|
||||
logs::{LogAction, LogEntry},
|
||||
tags::{Tag, TagName},
|
||||
users::{
|
||||
@@ -58,7 +58,7 @@ pub async fn create(
|
||||
) -> Result<Response, CompositeError> {
|
||||
let u = User::authenticate(&headers)?.required()?;
|
||||
let mut conn = database::conn()?;
|
||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
||||
let tx = conn.transaction()?;
|
||||
|
||||
if !u.has_permission(&tx, Permission::CreateTags)? {
|
||||
return Ok((StatusCode::FORBIDDEN, CANT_MAKE_TAGS).into_response());
|
||||
@@ -73,7 +73,7 @@ pub async fn create(
|
||||
name: t.name.as_str().to_string(),
|
||||
},
|
||||
)?;
|
||||
tx.commit().map_err(DatabaseError::from)?;
|
||||
tx.commit()?;
|
||||
Ok(Json(t).into_response())
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ pub async fn rename(
|
||||
) -> Result<Response, CompositeError> {
|
||||
let u = User::authenticate(&headers)?.required()?;
|
||||
let mut conn = database::conn()?;
|
||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
||||
let tx = conn.transaction()?;
|
||||
|
||||
if !u.has_permission(&tx, Permission::RenameTags)? {
|
||||
return Ok((StatusCode::FORBIDDEN, CANT_RENAME_TAGS).into_response());
|
||||
@@ -101,7 +101,7 @@ pub async fn rename(
|
||||
nn: tag.name.as_str().to_string(),
|
||||
},
|
||||
)?;
|
||||
tx.commit().map_err(DatabaseError::from)?;
|
||||
tx.commit()?;
|
||||
|
||||
Ok(Json(tag).into_response())
|
||||
}
|
||||
@@ -109,7 +109,7 @@ pub async fn rename(
|
||||
pub async fn delete(Path(id): Path<Uuid>, headers: HeaderMap) -> Result<Response, CompositeError> {
|
||||
let u = User::authenticate(&headers)?.required()?;
|
||||
let mut conn = database::conn()?;
|
||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
||||
let tx = conn.transaction()?;
|
||||
|
||||
if !u.has_permission(&tx, Permission::DeleteTags)? {
|
||||
return Ok((StatusCode::FORBIDDEN, CANT_DEL_TAGS).into_response());
|
||||
@@ -118,7 +118,7 @@ pub async fn delete(Path(id): Path<Uuid>, headers: HeaderMap) -> Result<Response
|
||||
let name = t.name.as_str().to_string();
|
||||
t.delete(&tx)?;
|
||||
LogEntry::new(&tx, u, LogAction::DeleteTag { id, name })?;
|
||||
tx.commit().map_err(DatabaseError::from)?;
|
||||
tx.commit()?;
|
||||
|
||||
Ok((StatusCode::OK, TAG_DELETED).into_response())
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ use serde::Deserialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
api::CompositeError,
|
||||
database::{self, DatabaseError},
|
||||
database::{self},
|
||||
error::CompositeError,
|
||||
logs::{LogAction, LogEntry},
|
||||
users::{
|
||||
User,
|
||||
@@ -63,7 +63,7 @@ pub async fn create(
|
||||
) -> Result<Response, CompositeError> {
|
||||
let u = User::authenticate(&headers)?.required()?;
|
||||
let mut conn = database::conn()?;
|
||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
||||
let tx = conn.transaction()?;
|
||||
|
||||
if !u.has_permission(&tx, Permission::ManuallyCreateUsers)? {
|
||||
return Ok((StatusCode::FORBIDDEN, CANT_MANUALLY_MAKE_USERS).into_response());
|
||||
@@ -78,7 +78,7 @@ pub async fn create(
|
||||
handle: nu.handle.as_str().to_string(),
|
||||
},
|
||||
)?;
|
||||
tx.commit().map_err(DatabaseError::from)?;
|
||||
tx.commit()?;
|
||||
|
||||
Ok(Json(nu).into_response())
|
||||
}
|
||||
@@ -89,7 +89,7 @@ pub async fn change_handle(
|
||||
) -> Result<Response, CompositeError> {
|
||||
let u = User::authenticate(&headers)?.required()?;
|
||||
let mut conn = database::conn()?;
|
||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
||||
let tx = conn.transaction()?;
|
||||
|
||||
let mut target = if u.id == id {
|
||||
u.clone()
|
||||
@@ -111,7 +111,7 @@ pub async fn change_handle(
|
||||
new: target.handle.as_str().to_string(),
|
||||
},
|
||||
)?;
|
||||
tx.commit().map_err(DatabaseError::from)?;
|
||||
tx.commit()?;
|
||||
|
||||
Ok(HANDLE_CHANGED_SUCCESS.into_response())
|
||||
}
|
||||
@@ -127,7 +127,7 @@ pub async fn change_password(
|
||||
) -> Result<Response, CompositeError> {
|
||||
let u = User::authenticate(&headers)?.required()?;
|
||||
let mut conn = database::conn()?;
|
||||
let tx = conn.transaction().map_err(DatabaseError::from)?;
|
||||
let tx = conn.transaction()?;
|
||||
|
||||
let mut target = if u.id == id {
|
||||
u.clone()
|
||||
@@ -144,7 +144,7 @@ pub async fn change_password(
|
||||
u,
|
||||
LogAction::ManuallyChangeUsersPassword { id: target.id },
|
||||
)?;
|
||||
tx.commit().map_err(DatabaseError::from)?;
|
||||
tx.commit()?;
|
||||
|
||||
Ok(PASSW_CHANGED_SUCCESS.into_response())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user