Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
N
Neos-rs
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Neos-rs
Commits
c1143cce
Commit
c1143cce
authored
Nov 26, 2022
by
Chunchi Che
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
impl cards service
parent
d446d876
Pipeline
#18098
failed with stages
in 7 minutes and 32 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
16 deletions
+47
-16
src/service/cards/db/mod.rs
src/service/cards/db/mod.rs
+6
-13
src/service/cards/db/model.rs
src/service/cards/db/model.rs
+2
-2
src/service/cards/mod.rs
src/service/cards/mod.rs
+39
-1
No files found.
src/service/cards/db/mod.rs
View file @
c1143cce
mod
model
;
mod
schema
;
use
crate
::
infra
::
DbConn
;
use
diesel
::{
ExpressionMethods
,
QueryDsl
,
RunQueryDsl
};
use
std
::
collections
::
HashMap
;
use
crate
::
infra
::
DbConn
;
pub
use
model
::{
CardDatas
,
CardTexts
}
;
/// `cards.cdb`业务层模块
pub
struct
CardsDB
{
...
...
@@ -24,15 +25,11 @@ impl CardsDB {
}
/// 通过`id`获取`Card`元数据
pub
fn
select_card_datas
(
&
mut
self
,
ids
:
&
[
i64
],
)
->
anyhow
::
Result
<
HashMap
<
i64
,
model
::
CardDatas
>>
{
pub
fn
select_card_datas
(
&
mut
self
,
ids
:
&
[
i64
])
->
anyhow
::
Result
<
HashMap
<
i64
,
CardDatas
>>
{
use
schema
::
datas
;
let
conn
=
&
mut
*
self
.conn
;
let
records
:
Vec
<
model
::
CardDatas
>
=
datas
::
table
.filter
(
datas
::
id
.eq_any
(
ids
))
.load
(
conn
)
?
;
let
records
:
Vec
<
CardDatas
>
=
datas
::
table
.filter
(
datas
::
id
.eq_any
(
ids
))
.load
(
conn
)
?
;
let
records
=
records
.into_iter
()
...
...
@@ -42,15 +39,11 @@ impl CardsDB {
}
/// 通过`id`获取`Card`文本数据
pub
fn
select_card_texts
(
&
mut
self
,
ids
:
&
[
i64
],
)
->
anyhow
::
Result
<
HashMap
<
i64
,
model
::
CardTexts
>>
{
pub
fn
select_card_texts
(
&
mut
self
,
ids
:
&
[
i64
])
->
anyhow
::
Result
<
HashMap
<
i64
,
CardTexts
>>
{
use
schema
::
texts
;
let
conn
=
&
mut
*
self
.conn
;
let
records
:
Vec
<
model
::
CardTexts
>
=
texts
::
table
.filter
(
texts
::
id
.eq_any
(
ids
))
.load
(
conn
)
?
;
let
records
:
Vec
<
CardTexts
>
=
texts
::
table
.filter
(
texts
::
id
.eq_any
(
ids
))
.load
(
conn
)
?
;
let
records
=
records
.into_iter
()
...
...
src/service/cards/db/model.rs
View file @
c1143cce
use
diesel_derives
::
Queryable
;
#[derive(Queryable,
Clone,
Debug,
Default,
PartialEq,
Eq)]
#[derive(
serde::Serialize,
Queryable,
Clone,
Debug,
Default,
PartialEq,
Eq)]
#[diesel(table_name
=
datas)]
pub
struct
CardDatas
{
pub
id
:
i64
,
...
...
@@ -17,7 +17,7 @@ pub struct CardDatas {
}
// TODO: 这里字段应该命名得更清晰一点
#[derive(Queryable,
Clone,
Debug,
Default,
PartialEq,
Eq)]
#[derive(
serde::Serialize,
Queryable,
Clone,
Debug,
Default,
PartialEq,
Eq)]
#[diesel(table_name
=
texts)]
pub
struct
CardTexts
{
pub
id
:
i64
,
...
...
src/service/cards/mod.rs
View file @
c1143cce
mod
db
;
// TODO: 这里更合理的做法应该是前端通过`POST`请求
// 数据,二进制格式使用PB协议编码,PB文件定义在`neos-protobuf`项目里面,
// `neos-rs`项目添加`neos-protobuf`项目作为子模块。
//
// 暂时先实现简单的`GET`接口
pub
fn
service
(
param
:
String
)
->
String
{
todo!
()
let
id
=
param
.parse
::
<
i64
>
()
.unwrap_or_default
();
match
(||
{
// TODO: 应该在服务冷启动的时候获取`CardsDB`实例,
// 而不是在每次`HTTP`请求的时候重新获取
let
mut
cards_db
=
db
::
CardsDB
::
new
()
?
;
let
datas
=
cards_db
.select_card_datas
(
&
[
id
])
?
.remove
(
&
id
)
.unwrap_or_default
();
let
texts
=
cards_db
.select_card_texts
(
&
[
id
])
?
.remove
(
&
id
)
.unwrap_or_default
();
Ok
::
<
(
db
::
CardDatas
,
db
::
CardTexts
),
anyhow
::
Error
>
((
datas
,
texts
))
})()
{
Ok
((
data
,
text
))
=>
{
let
meta
=
CardMeta
{
data
,
text
};
serde_json
::
to_string
(
&
meta
)
.unwrap_or_default
()
}
Err
(
e
)
=>
{
log
::
error!
(
"Handle cards service error={:?}"
,
e
);
serde_json
::
to_string
(
&
CardMeta
::
default
())
.unwrap_or_default
()
}
}
}
#[derive(serde::Serialize,
Default,
Debug)]
pub
struct
CardMeta
{
pub
data
:
db
::
CardDatas
,
pub
text
:
db
::
CardTexts
,
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment