Added initial users API.
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DecodeError, QueryConversionError, RepositoryError,
|
||||
dtos::{Delete, Upsert},
|
||||
};
|
||||
use crate::{
|
||||
database::{DatabaseError, dtos::UserRow, users as db},
|
||||
domain::User,
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
pub limit: Option<u64>,
|
||||
pub offset: Option<u64>,
|
||||
}
|
||||
|
||||
impl Default for Query {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
limit: None,
|
||||
offset: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Query> for db::Query {
|
||||
type Error = QueryConversionError;
|
||||
|
||||
fn try_from(query: Query) -> Result<Self, Self::Error> {
|
||||
Ok(db::Query {
|
||||
limit: query
|
||||
.limit
|
||||
.map(|l| i64::try_from(l).map_err(|_| QueryConversionError::Limit(l)))
|
||||
.transpose()?,
|
||||
offset: query
|
||||
.offset
|
||||
.map(|o| i64::try_from(o).map_err(|_| QueryConversionError::Offset(o)))
|
||||
.transpose()?,
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<User>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let users = db::query(executor, query.try_into()?)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
.collect::<Result<_, DecodeError>>()?;
|
||||
|
||||
Ok(users)
|
||||
}
|
||||
|
||||
pub async fn get<'e, E>(executor: E, id: Uuid) -> Result<Option<User>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let user = db::get_by_id(executor, id).await?.map(decode).transpose()?;
|
||||
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
pub async fn upsert<'e, E>(executor: E, user: User) -> Result<Upsert<User>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let user = db::upsert(executor, encode(user)).await?.try_map(decode)?;
|
||||
|
||||
Ok(user.into())
|
||||
}
|
||||
|
||||
pub async fn delete<'e, E>(executor: E, id: Uuid) -> Result<Delete, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let deleted = db::delete_by_id(executor, id)
|
||||
.await
|
||||
.map_err(classify_delete_error)?;
|
||||
|
||||
Ok(deleted.into())
|
||||
}
|
||||
|
||||
fn classify_delete_error(error: DatabaseError) -> RepositoryError {
|
||||
match &error {
|
||||
_ => RepositoryError::Database(error),
|
||||
}
|
||||
}
|
||||
|
||||
fn decode(row: UserRow) -> Result<User, DecodeError> {
|
||||
Ok(User { id: row.id })
|
||||
}
|
||||
|
||||
fn encode(user: User) -> UserRow {
|
||||
UserRow { id: user.id }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::database::Constraint;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn foreign_key_violation(constraint: Constraint) -> DatabaseError {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_unknown_user_delete_foreign_keys_generic() {
|
||||
let error = classify_delete_error(foreign_key_violation(Constraint::Unnamed));
|
||||
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
fn row() -> UserRow {
|
||||
UserRow { id: Uuid::new_v4() }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decodes_valid_rows() {
|
||||
let row = row();
|
||||
let id = row.id;
|
||||
let user = decode(row).unwrap();
|
||||
|
||||
assert_eq!(user.id, id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trips_through_rows() {
|
||||
let user = decode(row()).unwrap();
|
||||
assert_eq!(decode(encode(user.clone())).unwrap(), user);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user