Repo layer refactor.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct UserAppSecret {
|
pub struct UserAppSecret {
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub app_id: Uuid,
|
pub app_id: Uuid,
|
||||||
|
|||||||
@@ -21,9 +21,7 @@ pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<Account>, Repo
|
|||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
let accounts = db::list(
|
let query = db::Query {
|
||||||
executor,
|
|
||||||
db::Query {
|
|
||||||
name: query.name,
|
name: query.name,
|
||||||
limit: query
|
limit: query
|
||||||
.limit
|
.limit
|
||||||
@@ -33,12 +31,13 @@ where
|
|||||||
.offset
|
.offset
|
||||||
.map(|o| i64::try_from(o).map_err(|_| QueryConversionError::Offset(o)))
|
.map(|o| i64::try_from(o).map_err(|_| QueryConversionError::Offset(o)))
|
||||||
.transpose()?,
|
.transpose()?,
|
||||||
},
|
};
|
||||||
)
|
|
||||||
|
let accounts = db::list(executor, query)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(decode)
|
.map(decode)
|
||||||
.collect::<Result<_, DecodeError>>()?;
|
.collect::<Result<_, _>>()?;
|
||||||
|
|
||||||
Ok(accounts)
|
Ok(accounts)
|
||||||
}
|
}
|
||||||
@@ -67,12 +66,14 @@ where
|
|||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(decode)
|
.map(decode)
|
||||||
.collect::<Result<Vec<_>, DecodeError>>()?;
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
|
||||||
Ok(accounts
|
let account_map = accounts
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|account| (account.id, account))
|
.map(|account| (account.id, account))
|
||||||
.collect())
|
.collect();
|
||||||
|
|
||||||
|
Ok(account_map)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn upsert<'e, E>(
|
pub async fn upsert<'e, E>(
|
||||||
@@ -84,20 +85,22 @@ where
|
|||||||
{
|
{
|
||||||
let account = db::upsert(executor, encode(account))
|
let account = db::upsert(executor, encode(account))
|
||||||
.await?
|
.await?
|
||||||
.try_map(decode)?;
|
.try_map(decode)?
|
||||||
|
.into();
|
||||||
|
|
||||||
Ok(account.into())
|
Ok(account)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, RepositoryError>
|
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, RepositoryError>
|
||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
let deleted = db::delete_by_id(executor, id)
|
let result = db::delete_by_id(executor, id)
|
||||||
.await
|
.await
|
||||||
.map_err(classify_delete_error)?;
|
.map_err(classify_delete_error)?
|
||||||
|
.into();
|
||||||
|
|
||||||
Ok(deleted.into())
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn classify_delete_error(error: DatabaseError) -> RepositoryError {
|
fn classify_delete_error(error: DatabaseError) -> RepositoryError {
|
||||||
@@ -202,6 +205,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn round_trips_through_rows() {
|
fn round_trips_through_rows() {
|
||||||
let account = decode(row()).unwrap();
|
let account = decode(row()).unwrap();
|
||||||
assert_eq!(decode(encode(account.clone())).unwrap(), account);
|
let round_trip = decode(encode(account.clone())).unwrap();
|
||||||
|
assert_eq!(round_trip, account);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,9 +27,7 @@ pub async fn list<'e, E>(
|
|||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
let transactions = db::list(
|
let query = db::Query {
|
||||||
executor,
|
|
||||||
db::Query {
|
|
||||||
limit: query
|
limit: query
|
||||||
.limit
|
.limit
|
||||||
.map(|l| i64::try_from(l).map_err(|_| QueryConversionError::Limit(l)))
|
.map(|l| i64::try_from(l).map_err(|_| QueryConversionError::Limit(l)))
|
||||||
@@ -38,8 +36,9 @@ where
|
|||||||
.offset
|
.offset
|
||||||
.map(|o| i64::try_from(o).map_err(|_| QueryConversionError::Offset(o)))
|
.map(|o| i64::try_from(o).map_err(|_| QueryConversionError::Offset(o)))
|
||||||
.transpose()?,
|
.transpose()?,
|
||||||
},
|
};
|
||||||
)
|
|
||||||
|
let transactions = db::list(executor, query)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(decode)
|
.map(decode)
|
||||||
@@ -70,9 +69,10 @@ where
|
|||||||
let transaction = db::upsert(executor, encode(transaction))
|
let transaction = db::upsert(executor, encode(transaction))
|
||||||
.await
|
.await
|
||||||
.map_err(classify_upsert_error)?
|
.map_err(classify_upsert_error)?
|
||||||
.try_map(decode)?;
|
.try_map(decode)?
|
||||||
|
.into();
|
||||||
|
|
||||||
Ok(transaction.into())
|
Ok(transaction)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||||
@@ -98,7 +98,9 @@ pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, Reposi
|
|||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
Ok(db::delete_by_id(executor, id).await?.into())
|
let result = db::delete_by_id(executor, id).await?.into();
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decode(row: RecurringTransactionRow) -> Result<RecurringTransaction, DecodeError> {
|
fn decode(row: RecurringTransactionRow) -> Result<RecurringTransaction, DecodeError> {
|
||||||
@@ -238,6 +240,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn round_trips_through_rows() {
|
fn round_trips_through_rows() {
|
||||||
let transaction = decode(row()).unwrap();
|
let transaction = decode(row()).unwrap();
|
||||||
assert_eq!(decode(encode(transaction.clone())).unwrap(), transaction);
|
let round_trip = decode(encode(transaction.clone())).unwrap();
|
||||||
|
assert_eq!(round_trip, transaction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,9 +59,10 @@ where
|
|||||||
let transaction = db::upsert(executor, encode(transaction))
|
let transaction = db::upsert(executor, encode(transaction))
|
||||||
.await
|
.await
|
||||||
.map_err(classify_upsert_error)?
|
.map_err(classify_upsert_error)?
|
||||||
.try_map(decode)?;
|
.try_map(decode)?
|
||||||
|
.into();
|
||||||
|
|
||||||
Ok(transaction.into())
|
Ok(transaction)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||||
@@ -82,7 +83,9 @@ pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, Reposi
|
|||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
Ok(db::delete_by_id(executor, id).await?.into())
|
let result = db::delete_by_id(executor, id).await?.into();
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decode(row: TransactionRow) -> Result<Transaction, DecodeError> {
|
fn decode(row: TransactionRow) -> Result<Transaction, DecodeError> {
|
||||||
@@ -180,6 +183,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn round_trips_through_rows() {
|
fn round_trips_through_rows() {
|
||||||
let transaction = decode(row()).unwrap();
|
let transaction = decode(row()).unwrap();
|
||||||
assert_eq!(decode(encode(transaction.clone())).unwrap(), transaction);
|
let round_trip = decode(encode(transaction.clone())).unwrap();
|
||||||
|
assert_eq!(round_trip, transaction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use super::{
|
|||||||
use crate::{
|
use crate::{
|
||||||
database::{Constraint, DatabaseError, dtos::UserAppSecretRow, user_app_secrets as db},
|
database::{Constraint, DatabaseError, dtos::UserAppSecretRow, user_app_secrets as db},
|
||||||
domain::UserAppSecret,
|
domain::UserAppSecret,
|
||||||
|
repository::DecodeError,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Query {
|
pub struct Query {
|
||||||
@@ -18,9 +19,7 @@ pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<UserAppSecret>
|
|||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
Ok(db::list(
|
let query = db::Query {
|
||||||
executor,
|
|
||||||
db::Query {
|
|
||||||
app_id: query.app_id,
|
app_id: query.app_id,
|
||||||
limit: query
|
limit: query
|
||||||
.limit
|
.limit
|
||||||
@@ -28,16 +27,17 @@ where
|
|||||||
.transpose()?,
|
.transpose()?,
|
||||||
offset: query
|
offset: query
|
||||||
.offset
|
.offset
|
||||||
.map(|offset| {
|
.map(|offset| i64::try_from(offset).map_err(|_| QueryConversionError::Offset(offset)))
|
||||||
i64::try_from(offset).map_err(|_| QueryConversionError::Offset(offset))
|
|
||||||
})
|
|
||||||
.transpose()?,
|
.transpose()?,
|
||||||
},
|
};
|
||||||
)
|
|
||||||
|
let secrets = db::list(executor, query)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(decode)
|
.map(decode)
|
||||||
.collect())
|
.collect::<Result<Vec<_>, DecodeError>>()?;
|
||||||
|
|
||||||
|
Ok(secrets)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_by_id<'e, E>(
|
pub async fn get_by_id<'e, E>(
|
||||||
@@ -48,7 +48,12 @@ pub async fn get_by_id<'e, E>(
|
|||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
Ok(db::get_by_id(executor, app_id, id).await?.map(decode))
|
let secret = db::get_by_id(executor, app_id, id)
|
||||||
|
.await?
|
||||||
|
.map(decode)
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
|
Ok(secret)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn upsert<'e, E>(
|
pub async fn upsert<'e, E>(
|
||||||
@@ -58,14 +63,13 @@ pub async fn upsert<'e, E>(
|
|||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
let upsert = db::upsert(executor, encode(secret))
|
let secret = db::upsert(executor, encode(secret))
|
||||||
.await
|
.await
|
||||||
.map_err(classify_upsert_error)?;
|
.map_err(classify_upsert_error)?
|
||||||
|
.try_map(decode)?
|
||||||
|
.into();
|
||||||
|
|
||||||
Ok(match upsert {
|
Ok(secret)
|
||||||
crate::database::dtos::Upsert::Created(row) => Upsert::Created(decode(row)),
|
|
||||||
crate::database::dtos::Upsert::Updated(row) => Upsert::Updated(decode(row)),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||||
@@ -90,7 +94,9 @@ pub async fn verify<'e, E>(
|
|||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
Ok(db::verify(executor, app_id, secret_hash).await?)
|
let result = db::verify(executor, app_id, secret_hash).await?;
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete_by_id<'e, E>(
|
pub async fn delete_by_id<'e, E>(
|
||||||
@@ -101,17 +107,19 @@ pub async fn delete_by_id<'e, E>(
|
|||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
Ok(db::delete_by_id(executor, app_id, id).await?.into())
|
let result = db::delete_by_id(executor, app_id, id).await?.into();
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decode(row: UserAppSecretRow) -> UserAppSecret {
|
fn decode(row: UserAppSecretRow) -> Result<UserAppSecret, DecodeError> {
|
||||||
UserAppSecret {
|
Ok(UserAppSecret {
|
||||||
id: row.id,
|
id: row.id,
|
||||||
app_id: row.app_id,
|
app_id: row.app_id,
|
||||||
secret_hash: row.secret_hash,
|
secret_hash: row.secret_hash,
|
||||||
expires_at: row.expires_at,
|
expires_at: row.expires_at,
|
||||||
created_at: row.created_at,
|
created_at: row.created_at,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encode(secret: UserAppSecret) -> UserAppSecretRow {
|
fn encode(secret: UserAppSecret) -> UserAppSecretRow {
|
||||||
@@ -168,12 +176,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn round_trips_through_rows() {
|
fn round_trips_through_rows() {
|
||||||
let secret = decode(row());
|
let secret = decode(row()).unwrap();
|
||||||
let round_trip = decode(encode(secret.clone()));
|
let round_trip = decode(encode(secret.clone())).unwrap();
|
||||||
|
assert_eq!(round_trip, secret);
|
||||||
assert_eq!(round_trip.id, secret.id);
|
|
||||||
assert_eq!(round_trip.app_id, secret.app_id);
|
|
||||||
assert_eq!(round_trip.secret_hash, secret.secret_hash);
|
|
||||||
assert_eq!(round_trip.expires_at, secret.expires_at);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ use super::{QueryConversionError, RepositoryError, dtos::Upsert};
|
|||||||
use crate::{
|
use crate::{
|
||||||
database::{Constraint, DatabaseError, dtos::UserAppRow, user_apps as db},
|
database::{Constraint, DatabaseError, dtos::UserAppRow, user_apps as db},
|
||||||
domain::UserApp,
|
domain::UserApp,
|
||||||
|
repository::DecodeError,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Query {
|
pub struct Query {
|
||||||
@@ -16,9 +17,7 @@ pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<UserApp>, Repo
|
|||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
Ok(db::list(
|
let query = db::Query {
|
||||||
executor,
|
|
||||||
db::Query {
|
|
||||||
user_id: query.user_id,
|
user_id: query.user_id,
|
||||||
limit: query
|
limit: query
|
||||||
.limit
|
.limit
|
||||||
@@ -26,16 +25,17 @@ where
|
|||||||
.transpose()?,
|
.transpose()?,
|
||||||
offset: query
|
offset: query
|
||||||
.offset
|
.offset
|
||||||
.map(|offset| {
|
.map(|offset| i64::try_from(offset).map_err(|_| QueryConversionError::Offset(offset)))
|
||||||
i64::try_from(offset).map_err(|_| QueryConversionError::Offset(offset))
|
|
||||||
})
|
|
||||||
.transpose()?,
|
.transpose()?,
|
||||||
},
|
};
|
||||||
)
|
|
||||||
|
let apps = db::list(executor, query)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(decode)
|
.map(decode)
|
||||||
.collect())
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
|
||||||
|
Ok(apps)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_by_id<'e, E>(
|
pub async fn get_by_id<'e, E>(
|
||||||
@@ -46,21 +46,25 @@ pub async fn get_by_id<'e, E>(
|
|||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
Ok(db::get_by_id(executor, user_id, id).await?.map(decode))
|
let app = db::get_by_id(executor, user_id, id)
|
||||||
|
.await?
|
||||||
|
.map(decode)
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
|
Ok(app)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn upsert<'e, E>(executor: E, app: UserApp) -> Result<Upsert<UserApp>, RepositoryError>
|
pub async fn upsert<'e, E>(executor: E, app: UserApp) -> Result<Upsert<UserApp>, RepositoryError>
|
||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
let upsert = db::upsert(executor, encode(app))
|
let app = db::upsert(executor, encode(app))
|
||||||
.await
|
.await
|
||||||
.map_err(classify_upsert_error)?;
|
.map_err(classify_upsert_error)?
|
||||||
|
.try_map(decode)?
|
||||||
|
.into();
|
||||||
|
|
||||||
Ok(match upsert {
|
Ok(app)
|
||||||
crate::database::dtos::Upsert::Created(row) => Upsert::Created(decode(row)),
|
|
||||||
crate::database::dtos::Upsert::Updated(row) => Upsert::Updated(decode(row)),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||||
@@ -85,14 +89,16 @@ pub async fn delete_by_id<'e, E>(
|
|||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
Ok(db::delete_by_id(executor, user_id, id).await?.into())
|
let result = db::delete_by_id(executor, user_id, id).await?.into();
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decode(row: UserAppRow) -> UserApp {
|
fn decode(row: UserAppRow) -> Result<UserApp, DecodeError> {
|
||||||
UserApp {
|
Ok(UserApp {
|
||||||
id: row.id,
|
id: row.id,
|
||||||
user_id: row.user_id,
|
user_id: row.user_id,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encode(app: UserApp) -> UserAppRow {
|
fn encode(app: UserApp) -> UserAppRow {
|
||||||
@@ -143,7 +149,7 @@ mod tests {
|
|||||||
fn decodes_valid_rows() {
|
fn decodes_valid_rows() {
|
||||||
let row = row();
|
let row = row();
|
||||||
let (id, user_id) = (row.id, row.user_id);
|
let (id, user_id) = (row.id, row.user_id);
|
||||||
let app = decode(row);
|
let app = decode(row).unwrap();
|
||||||
|
|
||||||
assert_eq!(app.id, id);
|
assert_eq!(app.id, id);
|
||||||
assert_eq!(app.user_id, user_id);
|
assert_eq!(app.user_id, user_id);
|
||||||
@@ -151,7 +157,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn round_trips_through_rows() {
|
fn round_trips_through_rows() {
|
||||||
let app = decode(row());
|
let app = decode(row()).unwrap();
|
||||||
assert_eq!(decode(encode(app.clone())), app);
|
let round_trip = decode(encode(app.clone())).unwrap();
|
||||||
|
assert_eq!(round_trip, app);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,9 +18,7 @@ pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<User>, Reposit
|
|||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
let users = db::list(
|
let query = db::Query {
|
||||||
executor,
|
|
||||||
db::Query {
|
|
||||||
limit: query
|
limit: query
|
||||||
.limit
|
.limit
|
||||||
.map(|l| i64::try_from(l).map_err(|_| QueryConversionError::Limit(l)))
|
.map(|l| i64::try_from(l).map_err(|_| QueryConversionError::Limit(l)))
|
||||||
@@ -29,8 +27,9 @@ where
|
|||||||
.offset
|
.offset
|
||||||
.map(|o| i64::try_from(o).map_err(|_| QueryConversionError::Offset(o)))
|
.map(|o| i64::try_from(o).map_err(|_| QueryConversionError::Offset(o)))
|
||||||
.transpose()?,
|
.transpose()?,
|
||||||
},
|
};
|
||||||
)
|
|
||||||
|
let users = db::list(executor, query)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(decode)
|
.map(decode)
|
||||||
@@ -52,20 +51,24 @@ pub async fn upsert<'e, E>(executor: E, user: User) -> Result<Upsert<User>, Repo
|
|||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
let user = db::upsert(executor, encode(user)).await?.try_map(decode)?;
|
let user = db::upsert(executor, encode(user))
|
||||||
|
.await?
|
||||||
|
.try_map(decode)?
|
||||||
|
.into();
|
||||||
|
|
||||||
Ok(user.into())
|
Ok(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, RepositoryError>
|
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, RepositoryError>
|
||||||
where
|
where
|
||||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||||
{
|
{
|
||||||
let deleted = db::delete_by_id(executor, id)
|
let result = db::delete_by_id(executor, id)
|
||||||
.await
|
.await
|
||||||
.map_err(classify_delete_error)?;
|
.map_err(classify_delete_error)?
|
||||||
|
.into();
|
||||||
|
|
||||||
Ok(deleted.into())
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn classify_delete_error(error: DatabaseError) -> RepositoryError {
|
fn classify_delete_error(error: DatabaseError) -> RepositoryError {
|
||||||
|
|||||||
@@ -39,14 +39,13 @@ async fn list_accounts(
|
|||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Query(query): Query<QueryParams>,
|
Query(query): Query<QueryParams>,
|
||||||
) -> Result<Json<Vec<AccountResponse>>, AppError> {
|
) -> Result<Json<Vec<AccountResponse>>, AppError> {
|
||||||
let accounts = repository::accounts::list(
|
let query = repository::accounts::Query {
|
||||||
&pool,
|
|
||||||
repository::accounts::Query {
|
|
||||||
name: query.name,
|
name: query.name,
|
||||||
limit: Some(query.limit.unwrap_or(20)),
|
limit: Some(query.limit.unwrap_or(20)),
|
||||||
offset: query.offset,
|
offset: query.offset,
|
||||||
},
|
};
|
||||||
)
|
|
||||||
|
let accounts = repository::accounts::list(&pool, query)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(AccountResponse::from)
|
.map(AccountResponse::from)
|
||||||
|
|||||||
@@ -43,14 +43,13 @@ async fn list_user_app_secrets(
|
|||||||
Path((user_id, app_id)): Path<(Uuid, Uuid)>,
|
Path((user_id, app_id)): Path<(Uuid, Uuid)>,
|
||||||
Query(query): Query<QueryParams>,
|
Query(query): Query<QueryParams>,
|
||||||
) -> Result<Json<Vec<UserAppSecretResponse>>, AppError> {
|
) -> Result<Json<Vec<UserAppSecretResponse>>, AppError> {
|
||||||
let secrets = repository::user_app_secrets::list(
|
let query = repository::user_app_secrets::Query {
|
||||||
&pool,
|
|
||||||
repository::user_app_secrets::Query {
|
|
||||||
app_id,
|
app_id,
|
||||||
limit: Some(query.limit.unwrap_or(20)),
|
limit: Some(query.limit.unwrap_or(20)),
|
||||||
offset: query.offset,
|
offset: query.offset,
|
||||||
},
|
};
|
||||||
)
|
|
||||||
|
let secrets = repository::user_app_secrets::list(&pool, query)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(UserAppSecretResponse::from)
|
.map(UserAppSecretResponse::from)
|
||||||
|
|||||||
@@ -39,14 +39,13 @@ async fn list_user_apps(
|
|||||||
Path(user_id): Path<Uuid>,
|
Path(user_id): Path<Uuid>,
|
||||||
Query(query): Query<QueryParams>,
|
Query(query): Query<QueryParams>,
|
||||||
) -> Result<Json<Vec<UserAppResponse>>, AppError> {
|
) -> Result<Json<Vec<UserAppResponse>>, AppError> {
|
||||||
let apps = repository::user_apps::list(
|
let query = repository::user_apps::Query {
|
||||||
&pool,
|
|
||||||
repository::user_apps::Query {
|
|
||||||
user_id,
|
user_id,
|
||||||
limit: Some(query.limit.unwrap_or(20)),
|
limit: Some(query.limit.unwrap_or(20)),
|
||||||
offset: query.offset,
|
offset: query.offset,
|
||||||
},
|
};
|
||||||
)
|
|
||||||
|
let apps = repository::user_apps::list(&pool, query)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(UserAppResponse::from)
|
.map(UserAppResponse::from)
|
||||||
|
|||||||
@@ -38,13 +38,12 @@ async fn list_users(
|
|||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Query(query): Query<QueryParams>,
|
Query(query): Query<QueryParams>,
|
||||||
) -> Result<Json<Vec<UserResponse>>, AppError> {
|
) -> Result<Json<Vec<UserResponse>>, AppError> {
|
||||||
let users = repository::users::list(
|
let query = repository::users::Query {
|
||||||
&pool,
|
|
||||||
repository::users::Query {
|
|
||||||
limit: Some(query.limit.unwrap_or(20)),
|
limit: Some(query.limit.unwrap_or(20)),
|
||||||
offset: query.offset,
|
offset: query.offset,
|
||||||
},
|
};
|
||||||
)
|
|
||||||
|
let users = repository::users::list(&pool, query)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(UserResponse::from)
|
.map(UserResponse::from)
|
||||||
|
|||||||
Reference in New Issue
Block a user