1
+ use serde_json:: json;
2
+ use jz_model:: { member, organization, repository, users} ;
3
+ use crate :: AppModule ;
4
+ use sea_orm:: * ;
5
+ use serde:: { Deserialize , Serialize } ;
6
+
7
+ #[ derive( Deserialize , Serialize ) ]
8
+ pub struct ExploreParma {
9
+ pub page : i64 ,
10
+ pub size : i64 ,
11
+ pub filter : String ,
12
+ pub search : String ,
13
+ pub rtype : String , // repo | user | org
14
+ }
15
+
16
+
17
+ impl AppModule {
18
+ pub async fn explore ( & self , param : ExploreParma ) -> anyhow:: Result < serde_json:: Value > {
19
+ let mut value = serde_json:: Value :: Null ;
20
+ if param. rtype == "product" {
21
+ value[ "type" ] = json ! ( "product" ) ;
22
+ let mut data = vec ! [ ] ;
23
+ let mut condition = Condition :: all ( ) ;
24
+ if param. search != "" {
25
+ condition = condition. add ( repository:: Column :: Name . contains ( param. search . clone ( ) ) )
26
+ . add ( repository:: Column :: Description . contains ( param. search ) )
27
+ }
28
+ let query = repository:: Entity :: find ( )
29
+ . filter ( condition)
30
+ . order_by_desc ( repository:: Column :: CreatedAt )
31
+ . offset ( ( param. page - 1 ) as u64 * param. size as u64 )
32
+ . limit ( param. size as u64 )
33
+ . all ( & self . read )
34
+ . await ?;
35
+ for idx in query {
36
+ let owner = match self . user_info_by_id ( idx. owner_uid ) . await {
37
+ Ok ( owner) => json ! ( {
38
+ "uid" : owner. uid,
39
+ "name" : owner. username,
40
+ "avatar" : owner. avatar,
41
+ "description" : owner. description,
42
+ } ) ,
43
+ Err ( _) => {
44
+ match self . org_by_uid ( idx. owner_uid ) . await {
45
+ Ok ( org) => json ! ( {
46
+ "uid" : org. uid,
47
+ "name" : org. name,
48
+ "avatar" : org. avatar,
49
+ "description" : org. description,
50
+ } ) ,
51
+ Err ( _) => continue ,
52
+ }
53
+ }
54
+ } ;
55
+ let v = json ! ( {
56
+ "uid" : idx. uid,
57
+ "owner" : owner,
58
+ "name" : idx. name,
59
+ "description" : idx. description,
60
+ "created_at" : idx. created_at,
61
+ "updated_at" : idx. updated_at,
62
+ "star" : idx. nums_star,
63
+ "fork" : idx. nums_fork,
64
+ "watch" : idx. nums_watch,
65
+ "topic" : idx. topic,
66
+ "rtype" : idx. rtype,
67
+ "default_branch" : idx. default_branch,
68
+ } ) ;
69
+ data. push ( v) ;
70
+ }
71
+ value[ "data" ] = json ! ( data) ;
72
+ let total = repository:: Entity :: find ( )
73
+ . filter ( condition)
74
+ . count ( & self . read )
75
+ . await ?;
76
+ value[ "total" ] = json ! ( total) ;
77
+ return Ok ( value) ;
78
+ } else if param. rtype == "user" {
79
+ value[ "type" ] = json ! ( "user" ) ;
80
+ let mut data = vec ! [ ] ;
81
+ let mut cond = Condition :: all ( ) ;
82
+ if !param. search . is_empty ( ) {
83
+ cond = cond. add ( users:: Column :: Username . contains ( param. search . clone ( ) ) )
84
+ . add ( users:: Column :: Description . contains ( param. search ) )
85
+ }
86
+ let query = users:: Entity :: find ( )
87
+ . filter ( cond)
88
+ . order_by_desc ( users:: Column :: CreatedAt )
89
+ . offset ( ( param. page - 1 ) as u64 * param. size as u64 )
90
+ . limit ( param. size as u64 )
91
+ . all ( & self . read )
92
+ . await ?;
93
+ for idx in query {
94
+ let repo = repository:: Entity :: find ( )
95
+ . filter ( repository:: Column :: OwnerUid . eq ( idx. uid ) )
96
+ . count ( & self . read )
97
+ . await ?;
98
+ let v = json ! ( {
99
+ "uid" : idx. uid,
100
+ "name" : idx. username,
101
+ "avatar" : idx. avatar,
102
+ "repo" : repo,
103
+ "description" : idx. description,
104
+ "created_at" : idx. created_at,
105
+ "updated_at" : idx. updated_at,
106
+ } ) ;
107
+ data. push ( v) ;
108
+ }
109
+ value[ "data" ] = json ! ( data) ;
110
+ let total = users:: Entity :: find ( )
111
+ . filter ( cond)
112
+ . count ( & self . read )
113
+ . await ?;
114
+ value[ "total" ] = json ! ( total) ;
115
+ return Ok ( value) ;
116
+ } else if param. rtype == "organization" {
117
+ value[ "type" ] = json ! ( "organization" ) ;
118
+ let mut data = vec ! [ ] ;
119
+ let mut cond = Condition :: all ( ) ;
120
+ if !param. search . is_empty ( ) {
121
+ cond = cond. add ( organization:: Column :: Name . contains ( param. search . clone ( ) ) )
122
+ . add ( organization:: Column :: Description . contains ( param. search ) )
123
+ }
124
+ let query = organization:: Entity :: find ( )
125
+ . filter ( cond)
126
+ . order_by_desc ( organization:: Column :: CreatedAt )
127
+ . offset ( ( param. page - 1 ) as u64 * param. size as u64 )
128
+ . limit ( param. size as u64 )
129
+ . all ( & self . read )
130
+ . await ?;
131
+
132
+ for idx in query {
133
+ let member = member:: Entity :: find ( )
134
+ . filter ( member:: Column :: GroupUid . eq ( idx. uid ) )
135
+ . count ( & self . read )
136
+ . await ?;
137
+ let repo = repository:: Entity :: find ( )
138
+ . filter ( repository:: Column :: OwnerUid . eq ( idx. uid ) )
139
+ . count ( & self . read )
140
+ . await ?;
141
+ let v = json ! ( {
142
+ "uid" : idx. uid,
143
+ "name" : idx. name,
144
+ "avatar" : idx. avatar,
145
+ "member" : member,
146
+ "repo" : repo,
147
+ "description" : idx. description,
148
+ "created_at" : idx. created_at,
149
+ "updated_at" : idx. updated_at,
150
+ } ) ;
151
+ data. push ( v) ;
152
+ }
153
+ value[ "data" ] = json ! ( data) ;
154
+ let total = organization:: Entity :: find ( )
155
+ . filter ( cond)
156
+ . count ( & self . read )
157
+ . await ?;
158
+ value[ "total" ] = json ! ( total) ;
159
+ return Ok ( value) ;
160
+ }
161
+ return Err ( anyhow:: anyhow!( "rtype error" ) )
162
+ }
163
+ }
0 commit comments