-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmountain.rs
130 lines (106 loc) · 3.87 KB
/
mountain.rs
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
use mongodb::bson::{doc, Document};
use mongodb::options::FindOptions;
use mountix_kernel::model::mountain::{
Mountain, MountainBoxSearchCondition, MountainLocation, MountainSearchCondition,
};
use mountix_kernel::model::Id;
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
pub struct MountainDocument {
#[serde(rename = "_id")]
pub id: i32,
pub name: String,
pub name_kana: String,
pub area: String,
pub prefectures: Vec<String>,
pub elevation: u32,
pub tags: Vec<String>,
pub location: MountainLocationDocument,
pub gsi_url: String,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct MountainLocationDocument {
pub r#type: String,
pub coordinates: [f64; 2],
}
impl TryFrom<MountainDocument> for Mountain {
type Error = anyhow::Error;
fn try_from(mountain_doc: MountainDocument) -> Result<Self, Self::Error> {
let mountain_id: Id<Mountain> = mountain_doc.id.into();
let mountain_location = MountainLocation::new(
mountain_doc.location.coordinates[1],
mountain_doc.location.coordinates[0],
mountain_doc.gsi_url,
);
Ok(Mountain::new(
mountain_id,
mountain_doc.name,
mountain_doc.name_kana,
mountain_doc.area,
mountain_doc.prefectures,
mountain_doc.elevation,
mountain_location,
mountain_doc.tags,
))
}
}
pub struct MountainFindCommand {
pub(crate) filter: Document,
pub(crate) options: FindOptions,
}
impl TryFrom<MountainSearchCondition> for MountainFindCommand {
type Error = anyhow::Error;
fn try_from(sc: MountainSearchCondition) -> Result<Self, Self::Error> {
let mut filter = Document::new();
let mut and_doc: Vec<Document> = Vec::new();
if let Some(name) = sc.name {
and_doc.push(doc! {"$or": [{"name": {"$regex": &name, "$options": "i"}}, {"name_kana": {"$regex": &name, "$options": "i"}}]});
}
if let Some(pref) = sc.prefecture {
let pref_name = pref.name;
and_doc.push(doc! {"prefectures": &pref_name});
}
if let Some(tag) = sc.tag {
let tag_name = tag.name;
and_doc.push(doc! {"tags": &tag_name});
}
if and_doc.len() > 0 {
filter.insert("$and", and_doc);
}
let key = sc.sort.key.to_key();
let value = sc.sort.order.to_value();
let sort_doc = doc! {key: value};
let options = FindOptions::builder()
.sort(sort_doc)
.skip(sc.skip)
.limit(sc.limit)
.build();
Ok(MountainFindCommand { filter, options })
}
}
pub struct MountainFindBoxCommand {
pub(crate) filter: Document,
pub(crate) options: FindOptions,
}
impl TryFrom<MountainBoxSearchCondition> for MountainFindBoxCommand {
type Error = anyhow::Error;
fn try_from(sc: MountainBoxSearchCondition) -> Result<Self, Self::Error> {
let mut filter = Document::new();
let mut and_doc = vec![
doc! {"location": {"$geoWithin": {"$box": [[sc.box_coordinates.bottom_left.0,sc.box_coordinates.bottom_left.1], [sc.box_coordinates.upper_right.0,sc.box_coordinates.upper_right.1]]}}},
];
if let Some(name) = sc.name {
and_doc.push(doc! {"$or": [{"name": {"$regex": &name, "$options": "i"}}, {"name_kana": {"$regex": &name, "$options": "i"}}]});
}
if let Some(tag) = sc.tag {
let tag_name = tag.name;
and_doc.push(doc! {"tags": &tag_name});
}
filter.insert("$and", and_doc);
let key = sc.sort.key.to_key();
let value = sc.sort.order.to_value();
let sort_doc = doc! {key: value};
let options = FindOptions::builder().sort(sort_doc).build();
Ok(MountainFindBoxCommand { filter, options })
}
}