project.rs

 1use crate::db::{ProjectId, Result, RoomId, ServerId, UserId};
 2use anyhow::Context as _;
 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            .context("empty host_connection_server_id")?;
22        let host_connection_id = self
23            .host_connection_id
24            .context("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_repository::Entity")]
49    Repositories,
50    #[sea_orm(has_many = "super::project_collaborator::Entity")]
51    Collaborators,
52    #[sea_orm(has_many = "super::language_server::Entity")]
53    LanguageServers,
54}
55
56impl Related<super::user::Entity> for Entity {
57    fn to() -> RelationDef {
58        Relation::HostUser.def()
59    }
60}
61
62impl Related<super::room::Entity> for Entity {
63    fn to() -> RelationDef {
64        Relation::Room.def()
65    }
66}
67
68impl Related<super::worktree::Entity> for Entity {
69    fn to() -> RelationDef {
70        Relation::Worktrees.def()
71    }
72}
73
74impl Related<super::project_repository::Entity> for Entity {
75    fn to() -> RelationDef {
76        Relation::Repositories.def()
77    }
78}
79
80impl Related<super::project_collaborator::Entity> for Entity {
81    fn to() -> RelationDef {
82        Relation::Collaborators.def()
83    }
84}
85
86impl Related<super::language_server::Entity> for Entity {
87    fn to() -> RelationDef {
88        Relation::LanguageServers.def()
89    }
90}
91
92impl ActiveModelBehavior for ActiveModel {}