project.rs

  1use crate::db::{DevServerProjectId, HostedProjectId, ProjectId, Result, RoomId, ServerId, UserId};
  2use anyhow::anyhow;
  3use rpc::ConnectionId;
  4use sea_orm::entity::prelude::*;
  5
  6#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
  7#[sea_orm(table_name = "projects")]
  8pub struct Model {
  9    #[sea_orm(primary_key)]
 10    pub id: ProjectId,
 11    pub room_id: Option<RoomId>,
 12    pub host_user_id: Option<UserId>,
 13    pub host_connection_id: Option<i32>,
 14    pub host_connection_server_id: Option<ServerId>,
 15    pub hosted_project_id: Option<HostedProjectId>,
 16    pub dev_server_project_id: Option<DevServerProjectId>,
 17}
 18
 19impl Model {
 20    pub fn host_connection(&self) -> Result<ConnectionId> {
 21        let host_connection_server_id = self
 22            .host_connection_server_id
 23            .ok_or_else(|| anyhow!("empty host_connection_server_id"))?;
 24        let host_connection_id = self
 25            .host_connection_id
 26            .ok_or_else(|| anyhow!("empty host_connection_id"))?;
 27        Ok(ConnectionId {
 28            owner_id: host_connection_server_id.0 as u32,
 29            id: host_connection_id as u32,
 30        })
 31    }
 32}
 33
 34#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
 35pub enum Relation {
 36    #[sea_orm(
 37        belongs_to = "super::user::Entity",
 38        from = "Column::HostUserId",
 39        to = "super::user::Column::Id"
 40    )]
 41    HostUser,
 42    #[sea_orm(
 43        belongs_to = "super::room::Entity",
 44        from = "Column::RoomId",
 45        to = "super::room::Column::Id"
 46    )]
 47    Room,
 48    #[sea_orm(has_many = "super::worktree::Entity")]
 49    Worktrees,
 50    #[sea_orm(has_many = "super::project_collaborator::Entity")]
 51    Collaborators,
 52    #[sea_orm(has_many = "super::language_server::Entity")]
 53    LanguageServers,
 54    #[sea_orm(
 55        belongs_to = "super::hosted_project::Entity",
 56        from = "Column::HostedProjectId",
 57        to = "super::hosted_project::Column::Id"
 58    )]
 59    HostedProject,
 60    #[sea_orm(
 61        belongs_to = "super::dev_server_project::Entity",
 62        from = "Column::DevServerProjectId",
 63        to = "super::dev_server_project::Column::Id"
 64    )]
 65    RemoteProject,
 66}
 67
 68impl Related<super::user::Entity> for Entity {
 69    fn to() -> RelationDef {
 70        Relation::HostUser.def()
 71    }
 72}
 73
 74impl Related<super::room::Entity> for Entity {
 75    fn to() -> RelationDef {
 76        Relation::Room.def()
 77    }
 78}
 79
 80impl Related<super::worktree::Entity> for Entity {
 81    fn to() -> RelationDef {
 82        Relation::Worktrees.def()
 83    }
 84}
 85
 86impl Related<super::project_collaborator::Entity> for Entity {
 87    fn to() -> RelationDef {
 88        Relation::Collaborators.def()
 89    }
 90}
 91
 92impl Related<super::language_server::Entity> for Entity {
 93    fn to() -> RelationDef {
 94        Relation::LanguageServers.def()
 95    }
 96}
 97
 98impl Related<super::hosted_project::Entity> for Entity {
 99    fn to() -> RelationDef {
100        Relation::HostedProject.def()
101    }
102}
103
104impl Related<super::dev_server_project::Entity> for Entity {
105    fn to() -> RelationDef {
106        Relation::RemoteProject.def()
107    }
108}
109
110impl ActiveModelBehavior for ActiveModel {}