linkrs_server/client/
session.rs1use parking_lot::RwLock;
2use std::sync::Arc;
3
4#[derive(Debug, Clone)]
5pub struct Session {
6 pub session_id: i64,
7 pub user_name: String,
8 pub space_name: Option<String>,
9 pub graph_addr: Option<String>,
10 pub timezone: Option<i32>,
11}
12
13#[derive(Debug)]
14pub struct SessionContext {
15 session: Arc<RwLock<Session>>,
16}
17
18impl SessionContext {
19 pub fn new(session: Session) -> Self {
20 Self {
21 session: Arc::new(RwLock::new(session)),
22 }
23 }
24
25 pub fn id(&self) -> i64 {
26 self.session.read().session_id
27 }
28
29 pub fn user(&self) -> String {
30 self.session.read().user_name.clone()
31 }
32
33 pub fn space_name(&self) -> Option<String> {
34 self.session.read().space_name.clone()
35 }
36
37 pub fn update_space_name(&self, space_name: String) {
38 self.session.write().space_name = Some(space_name);
39 }
40
41 pub fn timezone(&self) -> Option<i32> {
42 self.session.read().timezone
43 }
44
45 pub fn set_timezone(&self, timezone: i32) {
46 self.session.write().timezone = Some(timezone);
47 }
48
49 pub fn graph_addr(&self) -> Option<String> {
50 self.session.read().graph_addr.clone()
51 }
52
53 pub fn update_graph_addr(&self, host_addr: String) {
54 self.session.write().graph_addr = Some(host_addr);
55 }
56
57 pub fn get_session(&self) -> Session {
58 self.session.read().clone()
59 }
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 #[test]
67 fn test_session_context_creation() {
68 let session = Session {
69 session_id: 123,
70 user_name: "testuser".to_string(),
71 space_name: None,
72 graph_addr: None,
73 timezone: None,
74 };
75
76 let context = SessionContext::new(session);
77 assert_eq!(context.id(), 123);
78 assert_eq!(context.user(), "testuser");
79 }
80
81 #[test]
82 fn test_session_context_timezone() {
83 let session = Session {
84 session_id: 123,
85 user_name: "testuser".to_string(),
86 space_name: None,
87 graph_addr: None,
88 timezone: None,
89 };
90
91 let context = SessionContext::new(session);
92 assert!(context.timezone().is_none());
93
94 context.set_timezone(8);
95 assert_eq!(context.timezone(), Some(8));
96 }
97
98 #[test]
99 fn test_session_context_graph_addr() {
100 let session = Session {
101 session_id: 123,
102 user_name: "testuser".to_string(),
103 space_name: None,
104 graph_addr: None,
105 timezone: None,
106 };
107
108 let context = SessionContext::new(session);
109 assert!(context.graph_addr().is_none());
110
111 context.update_graph_addr("127.0.0.1:9779".to_string());
112 assert_eq!(context.graph_addr(), Some("127.0.0.1:9779".to_string()));
113 }
114}