|
| 1 | +use crate::{proto::TodoEntity, storage::AppStorage}; |
| 2 | + |
1 | 3 | pub struct AppService {
|
2 |
| - // pub storage: AppStorage, |
| 4 | + pub storage: AppStorage, |
3 | 5 | }
|
4 | 6 |
|
5 | 7 | impl AppService {
|
6 | 8 | pub async fn new() -> Self {
|
7 |
| - // let storage = AppStorage::new().await; |
8 |
| - // Self { storage } |
9 |
| - Self {} |
| 9 | + let storage = AppStorage::new().await; |
| 10 | + Self { storage } |
| 11 | + } |
| 12 | + |
| 13 | + pub async fn add_todo(&self, todo: &TodoEntity) -> anyhow::Result<i64> { |
| 14 | + self.storage.sql.todo.add_todo(todo).await |
10 | 15 | }
|
11 | 16 |
|
12 |
| - // pub async fn add_todo(&self, todo: &TodoEntity) -> anyhow::Result<i64> { |
13 |
| - // self.storage.sql.todo.add_todo(todo).await |
14 |
| - // } |
15 |
| - // |
16 |
| - // pub async fn get_todo(&self, id: i64) -> anyhow::Result<TodoEntity> { |
17 |
| - // self.storage.sql.todo.get_todo(id).await |
18 |
| - // } |
| 17 | + pub async fn get_todo(&self, id: i64) -> anyhow::Result<TodoEntity> { |
| 18 | + self.storage.sql.todo.get_todo(id).await |
| 19 | + } |
19 | 20 | }
|
20 | 21 |
|
21 | 22 | #[cfg(test)]
|
22 | 23 | mod test {
|
23 | 24 | use super::*;
|
24 | 25 |
|
25 |
| - // async fn setup() { |
26 |
| - // // let mut db = SqlConn::new("app.db").await; |
27 |
| - // let mut db = SqlConn::default().await; |
28 |
| - // |
29 |
| - // db.init_migrations().await; |
30 |
| - // } |
| 26 | + #[tokio::test] |
| 27 | + async fn test_new_service() { |
| 28 | + let service = AppService::new().await; |
| 29 | + |
| 30 | + let ret = service |
| 31 | + .storage |
| 32 | + .sql |
| 33 | + .todo |
| 34 | + .add_todo(&TodoEntity { |
| 35 | + id: 0, |
| 36 | + title: "add todo, by service".to_string(), |
| 37 | + description: "test desc".to_string(), |
| 38 | + done: false, |
| 39 | + completed: false, |
| 40 | + }) |
| 41 | + .await |
| 42 | + .unwrap(); |
31 | 43 |
|
32 |
| - // #[tokio::test] |
33 |
| - // async fn test_new_service() { |
34 |
| - // setup().await; |
35 |
| - // |
36 |
| - // let service = AppService::new().await; |
37 |
| - // |
38 |
| - // let ret = service |
39 |
| - // .storage |
40 |
| - // .sql |
41 |
| - // .todo |
42 |
| - // .add_todo(&TodoEntity { |
43 |
| - // id: 0, |
44 |
| - // title: "add todo, by service".to_string(), |
45 |
| - // description: "test desc".to_string(), |
46 |
| - // done: false, |
47 |
| - // completed: false, |
48 |
| - // }) |
49 |
| - // .await |
50 |
| - // .unwrap(); |
51 |
| - // |
52 |
| - // println!("service add todo: {}", ret); |
53 |
| - // |
54 |
| - // let todo = service.storage.sql.todo.get_todo(ret).await.unwrap(); |
55 |
| - // |
56 |
| - // println!("service get todo: {:?}", todo); |
57 |
| - // } |
| 44 | + println!("service add todo: {}", ret); |
58 | 45 |
|
59 |
| - // #[tokio::test] |
60 |
| - // async fn test_service_add_todo() { |
61 |
| - // let service = AppService::new().await; |
62 |
| - // let ret = service |
63 |
| - // .add_todo(&TodoEntity { |
64 |
| - // id: 0, |
65 |
| - // title: "add todo, by service api".to_string(), |
66 |
| - // description: "test desc".to_string(), |
67 |
| - // done: false, |
68 |
| - // completed: false, |
69 |
| - // }) |
70 |
| - // .await |
71 |
| - // .unwrap(); |
72 |
| - // println!("service add todo: {}", ret); |
73 |
| - // |
74 |
| - // let todo = service.get_todo(ret).await.unwrap(); |
75 |
| - // println!("service get todo: {:?}", todo); |
76 |
| - // } |
| 46 | + let todo = service.storage.sql.todo.get_todo(ret).await.unwrap(); |
| 47 | + |
| 48 | + println!("service get todo: {:?}", todo); |
| 49 | + } |
| 50 | + |
| 51 | + #[tokio::test] |
| 52 | + async fn test_service_add_todo() { |
| 53 | + let service = AppService::new().await; |
| 54 | + let ret = service |
| 55 | + .add_todo(&TodoEntity { |
| 56 | + id: 0, |
| 57 | + title: "add todo, by service api".to_string(), |
| 58 | + description: "test desc".to_string(), |
| 59 | + done: false, |
| 60 | + completed: false, |
| 61 | + }) |
| 62 | + .await |
| 63 | + .unwrap(); |
| 64 | + println!("service add todo: {}", ret); |
| 65 | + |
| 66 | + let todo = service.get_todo(ret).await.unwrap(); |
| 67 | + println!("service get todo: {:?}", todo); |
| 68 | + } |
77 | 69 | }
|
0 commit comments