File size: 4,400 Bytes
b772fd3
6858ec5
249b27c
b772fd3
 
 
d94c6df
b772fd3
 
249b27c
b772fd3
249b27c
 
 
 
 
 
b772fd3
6858ec5
 
 
 
 
 
 
 
 
249b27c
 
 
6858ec5
249b27c
6858ec5
 
 
 
 
 
249b27c
 
 
 
 
 
 
 
 
 
 
6858ec5
 
 
 
 
249b27c
 
b772fd3
249b27c
6858ec5
 
 
 
249b27c
b772fd3
 
 
 
249b27c
b772fd3
 
6858ec5
 
 
 
 
b772fd3
 
1eb186a
6858ec5
 
 
 
1eb186a
 
 
 
 
 
 
 
6858ec5
 
 
 
 
1eb186a
b772fd3
6858ec5
 
 
 
d94c6df
b772fd3
 
 
 
 
 
 
 
 
 
6858ec5
 
 
 
 
b772fd3
 
 
6858ec5
 
 
 
d94c6df
b772fd3
 
 
 
 
 
 
6858ec5
 
 
 
 
1eb186a
 
 
 
 
6858ec5
1eb186a
 
 
6858ec5
 
 
 
1eb186a
 
 
 
 
 
 
 
 
 
6858ec5
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use std::collections::HashMap;
use actix_web::{ get, HttpResponse, post, web };
use serde::Serialize;
use crate::api::JsonResponse;
use crate::AppState;
use crate::entity::kb_info;
use crate::errors::AppError;
use crate::service::kb_info::Mutation;
use crate::service::kb_info::Query;
use serde::Deserialize;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AddDocs2KbParams {
    pub uid: i64,
    pub dids: Vec<i64>,
    pub kb_id: i64,
}
#[post("/v1.0/create_kb")]
async fn create(
    model: web::Json<kb_info::Model>,
    data: web::Data<AppState>
) -> Result<HttpResponse, AppError> {
    let mut docs = Query::find_kb_infos_by_name(
        &data.conn,
        model.kb_name.to_owned()
    ).await.unwrap();
    if docs.len() > 0 {
        let json_response = JsonResponse {
            code: 201,
            err: "Duplicated name.".to_owned(),
            data: (),
        };
        Ok(
            HttpResponse::Ok()
                .content_type("application/json")
                .body(serde_json::to_string(&json_response)?)
        )
    } else {
        let model = Mutation::create_kb_info(&data.conn, model.into_inner()).await?;

        let mut result = HashMap::new();
        result.insert("kb_id", model.kb_id.unwrap());

        let json_response = JsonResponse {
            code: 200,
            err: "".to_owned(),
            data: result,
        };

        Ok(
            HttpResponse::Ok()
                .content_type("application/json")
                .body(serde_json::to_string(&json_response)?)
        )
    }
}

#[post("/v1.0/add_docs_to_kb")]
async fn add_docs_to_kb(
    param: web::Json<AddDocs2KbParams>,
    data: web::Data<AppState>
) -> Result<HttpResponse, AppError> {
    let _ = Mutation::add_docs(&data.conn, param.kb_id, param.dids.to_owned()).await?;

    let json_response = JsonResponse {
        code: 200,
        err: "".to_owned(),
        data: (),
    };

    Ok(
        HttpResponse::Ok()
            .content_type("application/json")
            .body(serde_json::to_string(&json_response)?)
    )
}

#[post("/v1.0/anti_kb_docs")]
async fn anti_kb_docs(
    param: web::Json<AddDocs2KbParams>,
    data: web::Data<AppState>
) -> Result<HttpResponse, AppError> {
    let _ = Mutation::remove_docs(&data.conn, param.dids.to_owned(), Some(param.kb_id)).await?;

    let json_response = JsonResponse {
        code: 200,
        err: "".to_owned(),
        data: (),
    };

    Ok(
        HttpResponse::Ok()
            .content_type("application/json")
            .body(serde_json::to_string(&json_response)?)
    )
}
#[get("/v1.0/kbs")]
async fn list(
    model: web::Json<kb_info::Model>,
    data: web::Data<AppState>
) -> Result<HttpResponse, AppError> {
    let kbs = Query::find_kb_infos_by_uid(&data.conn, model.uid).await?;

    let mut result = HashMap::new();
    result.insert("kbs", kbs);

    let json_response = JsonResponse {
        code: 200,
        err: "".to_owned(),
        data: result,
    };

    Ok(
        HttpResponse::Ok()
            .content_type("application/json")
            .body(serde_json::to_string(&json_response)?)
    )
}

#[post("/v1.0/delete_kb")]
async fn delete(
    model: web::Json<kb_info::Model>,
    data: web::Data<AppState>
) -> Result<HttpResponse, AppError> {
    let _ = Mutation::delete_kb_info(&data.conn, model.kb_id).await?;

    let json_response = JsonResponse {
        code: 200,
        err: "".to_owned(),
        data: (),
    };

    Ok(
        HttpResponse::Ok()
            .content_type("application/json")
            .body(serde_json::to_string(&json_response)?)
    )
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DocIdsParams {
    pub uid: i64,
    pub dids: Vec<i64>,
}

#[post("/v1.0/all_relevents")]
async fn all_relevents(
    params: web::Json<DocIdsParams>,
    data: web::Data<AppState>
) -> Result<HttpResponse, AppError> {
    let dids = crate::service::doc_info::Query::all_descendent_ids(&data.conn, &params.dids).await?;
    let mut result = HashMap::new();
    let kbs = Query::find_kb_by_docs(&data.conn, dids).await?;
    result.insert("kbs", kbs);
    let json_response = JsonResponse {
        code: 200,
        err: "".to_owned(),
        data: result,
    };

    Ok(
        HttpResponse::Ok()
            .content_type("application/json")
            .body(serde_json::to_string(&json_response)?)
    )
}