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 pub windows_paths: bool,
16 pub features: String,
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 .context("empty host_connection_server_id")?;
24 let host_connection_id = self
25 .host_connection_id
26 .context("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_repository::Entity")]
51 Repositories,
52 #[sea_orm(has_many = "super::project_collaborator::Entity")]
53 Collaborators,
54 #[sea_orm(has_many = "super::language_server::Entity")]
55 LanguageServers,
56}
57
58impl Related<super::user::Entity> for Entity {
59 fn to() -> RelationDef {
60 Relation::HostUser.def()
61 }
62}
63
64impl Related<super::room::Entity> for Entity {
65 fn to() -> RelationDef {
66 Relation::Room.def()
67 }
68}
69
70impl Related<super::worktree::Entity> for Entity {
71 fn to() -> RelationDef {
72 Relation::Worktrees.def()
73 }
74}
75
76impl Related<super::project_repository::Entity> for Entity {
77 fn to() -> RelationDef {
78 Relation::Repositories.def()
79 }
80}
81
82impl Related<super::project_collaborator::Entity> for Entity {
83 fn to() -> RelationDef {
84 Relation::Collaborators.def()
85 }
86}
87
88impl Related<super::language_server::Entity> for Entity {
89 fn to() -> RelationDef {
90 Relation::LanguageServers.def()
91 }
92}
93
94impl ActiveModelBehavior for ActiveModel {}