Something went wrong. Try again.
A simple to-do app focused on tasks that can be completed within a specific time span. taskline.tobinio.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130use orm::{OrmConnection, entities::organization, entities::organization_user};use sea_orm::{ActiveValue::Set, EntityTrait, QueryFilter, SelectExt};use thiserror::Error;use types::organization::Organization;use uuid::Uuid;
use crate::User;
#[derive(Error, Debug, PartialEq)]pub enum OrganizationServiceError { #[error("database error - caused by: {0}")] DbError(#[from] sea_orm::DbErr),}
pub struct OrganizationService;
impl OrganizationService { pub async fn get_all( user: &User, db: &OrmConnection, ) -> Result<Vec<Organization>, OrganizationServiceError> { let organizations = organization::Entity::find() .find_also_related(organization_user::Entity) .filter( organization_user::COLUMN .user_id .eq(user.user_id().to_string()), ) .all(db) .await?;
Ok(organizations .into_iter() .map(|(org, _)| to_organization_dto(&org.into_ex())) .collect()) }
pub async fn exists( db: &OrmConnection, organization: Uuid, ) -> Result<bool, OrganizationServiceError> { let exists = organization::Entity::find() .filter(organization::COLUMN.id.eq(organization)) .exists(db) .await?;
Ok(exists) }
pub async fn has_access( db: &OrmConnection, user: &str, organization: Uuid, ) -> Result<bool, OrganizationServiceError> { let exists = organization_user::Entity::find() .filter(organization_user::COLUMN.user_id.eq(user)) .filter(organization_user::COLUMN.organization_id.eq(organization)) .exists(db) .await?;
Ok(exists) }
pub async fn joined( user: &User, db: &OrmConnection, organization: Uuid, ) -> Result<bool, OrganizationServiceError> { let exists = organization_user::Entity::find() .filter(organization_user::COLUMN.organization_id.eq(organization)) .filter(organization_user::COLUMN.user_id.eq(user.id().to_string())) .exists(db) .await?;
Ok(exists) }
pub async fn create( db: &OrmConnection, organization: Organization, ) -> Result<Organization, OrganizationServiceError> { let organization = organization::Entity::insert(organization::ActiveModel { id: Set(organization.uuid), name: Set(organization.name), }) .exec_with_returning(db) .await?;
Ok(to_organization_dto(&organization.into_ex())) }
pub async fn join( user: &User, db: &OrmConnection, organization: Uuid, ) -> Result<(), OrganizationServiceError> { organization_user::Entity::insert(organization_user::ActiveModel { organization_id: Set(organization), user_id: Set(user.user_id().to_string()), }) .exec_with_returning(db) .await?;
Ok(()) }
pub async fn leave( user: &User, db: &OrmConnection, organization: Uuid, ) -> Result<(), OrganizationServiceError> { organization_user::Entity::delete(organization_user::ActiveModel { organization_id: Set(organization), user_id: Set(user.user_id().to_string()), }) .exec_with_returning(db) .await?;
Ok(()) }}
#[must_use]pub fn to_organization_dto(value: &organization::ModelEx) -> Organization { Organization { uuid: value.id, name: value.name.clone(), }}