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