glues_core/backend/proxy/
server.rs

1use super::request::ProxyRequest;
2use super::response::{ProxyResponse, ResultPayload};
3use crate::backend::{BackendBox, CoreBackend};
4
5pub struct ProxyServer {
6    pub db: BackendBox,
7}
8
9impl ProxyServer {
10    pub fn new(db: BackendBox) -> Self {
11        Self { db }
12    }
13
14    pub async fn handle(&mut self, req: ProxyRequest) -> ProxyResponse {
15        use ProxyRequest::*;
16        match req {
17            RootId => ProxyResponse::Ok(ResultPayload::Id(self.db.root_id())),
18            FetchDirectory { directory_id } => match self.db.fetch_directory(directory_id).await {
19                Ok(dir) => ProxyResponse::Ok(ResultPayload::Directory(dir)),
20                Err(e) => ProxyResponse::Err(e.to_string()),
21            },
22            FetchDirectories { parent_id } => match self.db.fetch_directories(parent_id).await {
23                Ok(dirs) => ProxyResponse::Ok(ResultPayload::Directories(dirs)),
24                Err(e) => ProxyResponse::Err(e.to_string()),
25            },
26            AddDirectory { parent_id, name } => {
27                match self.db.add_directory(parent_id, name).await {
28                    Ok(dir) => ProxyResponse::Ok(ResultPayload::Directory(dir)),
29                    Err(e) => ProxyResponse::Err(e.to_string()),
30                }
31            }
32            RemoveDirectory { directory_id } => {
33                match self.db.remove_directory(directory_id).await {
34                    Ok(()) => ProxyResponse::Ok(ResultPayload::Unit),
35                    Err(e) => ProxyResponse::Err(e.to_string()),
36                }
37            }
38            MoveDirectory {
39                directory_id,
40                parent_id,
41            } => match self.db.move_directory(directory_id, parent_id).await {
42                Ok(()) => ProxyResponse::Ok(ResultPayload::Unit),
43                Err(e) => ProxyResponse::Err(e.to_string()),
44            },
45            RenameDirectory { directory_id, name } => {
46                match self.db.rename_directory(directory_id, name).await {
47                    Ok(()) => ProxyResponse::Ok(ResultPayload::Unit),
48                    Err(e) => ProxyResponse::Err(e.to_string()),
49                }
50            }
51            FetchNotes { directory_id } => match self.db.fetch_notes(directory_id).await {
52                Ok(notes) => ProxyResponse::Ok(ResultPayload::Notes(notes)),
53                Err(e) => ProxyResponse::Err(e.to_string()),
54            },
55            FetchNoteContent { note_id } => match self.db.fetch_note_content(note_id).await {
56                Ok(content) => ProxyResponse::Ok(ResultPayload::Text(content)),
57                Err(e) => ProxyResponse::Err(e.to_string()),
58            },
59            AddNote { directory_id, name } => match self.db.add_note(directory_id, name).await {
60                Ok(note) => ProxyResponse::Ok(ResultPayload::Note(note)),
61                Err(e) => ProxyResponse::Err(e.to_string()),
62            },
63            RemoveNote { note_id } => match self.db.remove_note(note_id).await {
64                Ok(()) => ProxyResponse::Ok(ResultPayload::Unit),
65                Err(e) => ProxyResponse::Err(e.to_string()),
66            },
67            RenameNote { note_id, name } => match self.db.rename_note(note_id, name).await {
68                Ok(()) => ProxyResponse::Ok(ResultPayload::Unit),
69                Err(e) => ProxyResponse::Err(e.to_string()),
70            },
71            UpdateNoteContent { note_id, content } => {
72                match self.db.update_note_content(note_id, content).await {
73                    Ok(()) => ProxyResponse::Ok(ResultPayload::Unit),
74                    Err(e) => ProxyResponse::Err(e.to_string()),
75                }
76            }
77            MoveNote {
78                note_id,
79                directory_id,
80            } => match self.db.move_note(note_id, directory_id).await {
81                Ok(()) => ProxyResponse::Ok(ResultPayload::Unit),
82                Err(e) => ProxyResponse::Err(e.to_string()),
83            },
84            Log { category, message } => match self.db.log(category, message).await {
85                Ok(()) => ProxyResponse::Ok(ResultPayload::Unit),
86                Err(e) => ProxyResponse::Err(e.to_string()),
87            },
88        }
89    }
90}