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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use crate::{db::burrow, models::content::Post};
use rocket::serde::{Deserialize, Serialize};
use sea_orm::FromQueryResult;
#[derive(Debug, FromQueryResult)]
pub struct LastBurrowSeq {
last_value: i64,
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
pub struct BurrowTotalCount {
pub total: i64,
}
#[derive(Serialize, Deserialize)]
pub struct BurrowInfo {
pub description: String,
pub title: String,
}
#[derive(Serialize, Deserialize)]
pub struct BurrowCreateResponse {
pub burrow_id: i64,
}
#[derive(Serialize, Deserialize)]
pub struct BurrowShowResponse {
pub title: String,
pub description: String,
pub posts: Vec<Post>,
}
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct BurrowMetadata {
pub burrow_id: i64,
pub title: String,
pub description: String,
pub post_num: i32,
}
impl From<burrow::Model> for BurrowMetadata {
fn from(burrow: burrow::Model) -> BurrowMetadata {
match burrow.burrow_state {
0 => BurrowMetadata {
burrow_id: burrow.burrow_id,
title: burrow.title,
description: burrow.description,
post_num: burrow.post_num,
},
_ => BurrowMetadata {
burrow_id: burrow.burrow_id,
title: "Admin has banned this burrow".to_string(),
description: "Admin has banned this burrow".to_string(),
post_num: burrow.post_num,
},
}
}
}
impl From<&burrow::Model> for BurrowMetadata {
fn from(burrow: &burrow::Model) -> BurrowMetadata {
match burrow.burrow_state {
0 => BurrowMetadata {
burrow_id: burrow.burrow_id,
title: burrow.title.clone(),
description: burrow.description.clone(),
post_num: burrow.post_num,
},
_ => BurrowMetadata {
burrow_id: burrow.burrow_id,
title: "Admin has banned this burrow".to_string(),
description: "Admin has banned this burrow".to_string(),
post_num: burrow.post_num,
},
}
}
}
impl From<LastBurrowSeq> for BurrowTotalCount {
fn from(seq: LastBurrowSeq) -> BurrowTotalCount {
BurrowTotalCount {
total: seq.last_value,
}
}
}
impl From<&LastBurrowSeq> for BurrowTotalCount {
fn from(seq: &LastBurrowSeq) -> BurrowTotalCount {
BurrowTotalCount {
total: seq.last_value,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::{FixedOffset, Utc};
#[test]
fn test_burrow_metadata() {
let now = Utc::now().with_timezone(&FixedOffset::east(8 * 3600));
let burrow_id: i64 = 666;
let title = "title".to_string();
let description = "description".to_string();
let post_num: i32 = 10;
let burrow = burrow::Model {
burrow_id,
title: title.clone(),
description: description.clone(),
uid: 123i64,
burrow_state: 0i32,
post_num,
create_time: now,
update_time: now,
credit: 0i32,
badge: "badge".to_string(),
avatar: "default.jpg".to_string(),
permission: 0,
};
let burrow_banned = burrow::Model {
burrow_id,
title: title.clone(),
description: description.clone(),
uid: 123i64,
burrow_state: 1i32,
post_num,
create_time: now,
update_time: now,
credit: 1i32,
badge: "badge".to_string(),
avatar: "default.jpg".to_string(),
permission: 0,
};
let burrow_ref = &burrow;
let burrow_banned_ref = &burrow_banned;
let burrow_data = BurrowMetadata {
burrow_id,
title,
description,
post_num,
};
let burrow_banned_data = BurrowMetadata {
burrow_id,
title: "Admin has banned this burrow".to_string(),
description: "Admin has banned this burrow".to_string(),
post_num,
};
assert_eq!(burrow_data, burrow_ref.into());
assert_eq!(burrow_banned_data, burrow_banned_ref.into());
assert_eq!(burrow_data, burrow.into());
assert_eq!(burrow_banned_data, burrow_banned.into());
}
#[test]
fn test_burrow_count() {
let last_value: i64 = 666;
let seq = LastBurrowSeq { last_value };
let seq_ref = &seq;
let burrow_cnt = BurrowTotalCount { total: last_value };
assert_eq!(burrow_cnt, seq_ref.into());
assert_eq!(burrow_cnt, seq.into());
}
}