project.rs

 1use crate::db::{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}
16
17impl Model {
18    pub fn host_connection(&self) -> Result<ConnectionId> {
19        let host_connection_server_id = self
20            .host_connection_server_id
21            .ok_or_else(|| anyhow!("empty host_connection_server_id"))?;
22        let host_connection_id = self
23            .host_connection_id
24            .ok_or_else(|| anyhow!("empty host_connection_id"))?;
25        Ok(ConnectionId {
26            owner_id: host_connection_server_id.0 as u32,
27            id: host_connection_id as u32,
28        })
29    }
30}
31
32#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
33pub enum Relation {
34    #[sea_orm(
35        belongs_to = "super::user::Entity",
36        from = "Column::HostUserId",
37        to = "super::user::Column::Id"
38    )]
39    HostUser,
40    #[sea_orm(
41        belongs_to = "super::room::Entity",
42        from = "Column::RoomId",
43        to = "super::room::Column::Id"
44    )]
45    Room,
46    #[sea_orm(has_many = "super::worktree::Entity")]
47    Worktrees,
48    #[sea_orm(has_many = "super::project_collaborator::Entity")]
49    Collaborators,
50    #[sea_orm(has_many = "super::language_server::Entity")]
51    LanguageServers,
52}
53
54impl Related<super::user::Entity> for Entity {
55    fn to() -> RelationDef {
56        Relation::HostUser.def()
57    }
58}
59
60impl Related<super::room::Entity> for Entity {
61    fn to() -> RelationDef {
62        Relation::Room.def()
63    }
64}
65
66impl Related<super::worktree::Entity> for Entity {
67    fn to() -> RelationDef {
68        Relation::Worktrees.def()
69    }
70}
71
72impl Related<super::project_collaborator::Entity> for Entity {
73    fn to() -> RelationDef {
74        Relation::Collaborators.def()
75    }
76}
77
78impl Related<super::language_server::Entity> for Entity {
79    fn to() -> RelationDef {
80        Relation::LanguageServers.def()
81    }
82}
83
84impl ActiveModelBehavior for ActiveModel {}