Skip to content

Commit 5f964f0

Browse files
refactor: convert query's structs to enums for search
1 parent fe73756 commit 5f964f0

File tree

13 files changed

+145
-278
lines changed

13 files changed

+145
-278
lines changed

clients/javascript/lib/gen_types/DateTimeQuery.ts

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,9 @@
33
/**
44
* Query parameters for searching on a date time
55
*/
6-
export type DateTimeQuery = {
7-
/**
8-
* Optional "equal" query (UTC)
9-
*/
10-
eq?: Date
11-
/**
12-
* Optional "greater than or equal" query (UTC)
13-
*/
14-
gte?: Date
15-
/**
16-
* Optional "less than or equal" query (UTC)
17-
*/
18-
lte?: Date
19-
/**
20-
* Optional "greater than" query (UTC)
21-
* This is exclusive of the value
22-
*/
23-
gt?: Date
24-
/**
25-
* Optional "less than" query (UTC)
26-
* This is exclusive of the value
27-
*/
28-
lt?: Date
29-
}
6+
export type DateTimeQuery =
7+
| { eq: Date }
8+
| { gte: Date }
9+
| { lte: Date }
10+
| { gt: Date }
11+
| { lt: Date }

clients/javascript/lib/gen_types/IDQuery.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,8 @@ import type { ID } from './ID'
44
/**
55
* Query parameters for searching on an ID (or list of IDs)
66
*/
7-
export type IDQuery = {
8-
/**
9-
* Optional ID (equality test)
10-
*/
11-
eq?: ID
12-
/**
13-
* Optional ID (inequality test)
14-
* If "eq" is provided, this field is ignored
15-
*/
16-
ne?: ID
17-
/**
18-
* Optional bool (existence test)
19-
* If "eq" is provided, this field is ignored
20-
*/
21-
exists?: boolean
22-
/**
23-
* Optional list of IDs (equality test)
24-
* If "eq" is provided, this field is ignored
25-
* (use r# in the field name as "in" is a reserved keyword)
26-
*/
27-
in?: Array<ID>
28-
}
7+
export type IDQuery =
8+
| { eq: ID }
9+
| { ne: ID }
10+
| { exists: boolean }
11+
| { in: Array<ID> }

clients/javascript/lib/gen_types/StringQuery.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,8 @@
33
/**
44
* Query parameters for searching on a string
55
*/
6-
export type StringQuery = {
7-
/**
8-
* Optional String (equality test)
9-
*/
10-
eq?: string
11-
/**
12-
* Optional string (inequality test)
13-
* If "eq" is provided, this field is ignored
14-
*/
15-
ne?: string
16-
/**
17-
* Optional bool (existence test)
18-
* If "eq" is provided, this field is ignored
19-
*/
20-
exists?: boolean
21-
/**
22-
* Optional list of strings (equality test)
23-
* If "eq" is provided, this field is ignored
24-
* (use r# in the field name as "in" is a reserved keyword)
25-
*/
26-
in?: Array<string>
27-
}
6+
export type StringQuery =
7+
| { eq: string }
8+
| { ne: string }
9+
| { exists: boolean }
10+
| { in: Array<string> }

crates/api/src/account/account_search_events.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use actix_web::{web, HttpRequest, HttpResponse};
22
use nittei_api_structs::{account_search_events::*, dtos::CalendarEventDTO};
3-
use nittei_domain::{DateTimeQuery, IdQuery, StringQuery, ID};
3+
use nittei_domain::{DateTimeQuery, IDQuery, StringQuery, ID};
44
use nittei_infra::{NitteiContext, SearchEventsForAccountParams, SearchEventsParams};
55

66
use crate::{
@@ -46,7 +46,7 @@ pub struct AccountSearchEventsUseCase {
4646
pub parent_id: Option<StringQuery>,
4747

4848
/// Optional query on the group ID
49-
pub group_id: Option<IdQuery>,
49+
pub group_id: Option<IDQuery>,
5050

5151
/// Optional query on start time - "lower than or equal", or "great than or equal" (UTC)
5252
pub start_time: Option<DateTimeQuery>,

crates/api/src/event/search_events.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use actix_web::{web, HttpRequest, HttpResponse};
22
use nittei_api_structs::{dtos::CalendarEventDTO, search_events::*};
3-
use nittei_domain::{DateTimeQuery, IdQuery, StringQuery, ID};
3+
use nittei_domain::{DateTimeQuery, IDQuery, StringQuery, ID};
44
use nittei_infra::{NitteiContext, SearchEventsForUserParams, SearchEventsParams};
55

66
use crate::{
@@ -55,7 +55,7 @@ pub struct SearchEventsUseCase {
5555
pub parent_id: Option<StringQuery>,
5656

5757
/// Optional query on the group ID
58-
pub group_id: Option<IdQuery>,
58+
pub group_id: Option<IDQuery>,
5959

6060
/// Optional query on start time - "lower than or equal", or "great than or equal" (UTC)
6161
pub start_time: Option<DateTimeQuery>,

crates/api_structs/src/account/api.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub mod remove_account_integration {
148148

149149
/// Request body for searching events for a whole account (across all users)
150150
pub mod account_search_events {
151-
use nittei_domain::{DateTimeQuery, IdQuery, StringQuery};
151+
use nittei_domain::{DateTimeQuery, IDQuery, StringQuery};
152152
use serde::{Deserialize, Serialize};
153153
use ts_rs::TS;
154154
use validator::Validate;
@@ -162,15 +162,15 @@ pub mod account_search_events {
162162
pub struct RequestBody {
163163
/// Optional query on user ID, or list of user IDs
164164
#[ts(optional)]
165-
pub user_id: Option<IdQuery>,
165+
pub user_id: Option<IDQuery>,
166166

167167
/// Optional query on parent ID (which is a string as it's an ID from an external system)
168168
#[ts(optional)]
169169
pub parent_id: Option<StringQuery>,
170170

171171
/// Optional query on the group ID
172172
#[ts(optional)]
173-
pub group_id: Option<IdQuery>,
173+
pub group_id: Option<IDQuery>,
174174

175175
/// Optional query on start time - e.g. "lower than or equal", or "great than or equal" (UTC)
176176
#[ts(optional)]

crates/api_structs/src/event/api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ pub mod get_events_by_calendars {
297297
}
298298

299299
pub mod search_events {
300-
use nittei_domain::{DateTimeQuery, IdQuery, StringQuery};
300+
use nittei_domain::{DateTimeQuery, IDQuery, StringQuery};
301301

302302
use super::*;
303303

@@ -321,7 +321,7 @@ pub mod search_events {
321321

322322
/// Optional query on group ID
323323
#[ts(optional)]
324-
pub group_id: Option<IdQuery>,
324+
pub group_id: Option<IDQuery>,
325325

326326
/// Optional query on start time - "lower than or equal", or "great than or equal" (UTC)
327327
#[ts(optional)]

crates/domain/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub use service::{
4040
pub use shared::{
4141
datetime_query::DateTimeQuery,
4242
entity::{Entity, ID},
43-
id_query::IdQuery,
43+
id_query::IDQuery,
4444
metadata::{Meta, Metadata},
4545
recurrence::{RRuleFrequency, RRuleOptions, WeekDayRecurrence},
4646
string_query::StringQuery,
Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
use chrono::{DateTime, Utc};
22
use serde::{Deserialize, Serialize};
33
use ts_rs::TS;
4-
use validator::Validate;
54

65
/// Query parameters for searching on a date time
7-
#[derive(Deserialize, Serialize, TS, Debug, Validate, Clone)]
6+
#[derive(Deserialize, Serialize, TS, Debug, Clone)]
87
#[serde(rename_all = "camelCase")]
98
#[ts(export, rename = "DateTimeQuery")]
10-
pub struct DateTimeQuery {
11-
/// Optional "equal" query (UTC)
12-
#[ts(type = "Date", optional)]
13-
pub eq: Option<DateTime<Utc>>,
9+
pub enum DateTimeQuery {
10+
/// "equal" query (UTC)
11+
#[ts(type = "Date")]
12+
Eq(DateTime<Utc>),
1413

15-
/// Optional "greater than or equal" query (UTC)
16-
#[ts(type = "Date", optional)]
17-
pub gte: Option<DateTime<Utc>>,
14+
/// "greater than or equal" query (UTC)
15+
#[ts(type = "Date")]
16+
Gte(DateTime<Utc>),
1817

19-
/// Optional "less than or equal" query (UTC)
20-
#[ts(type = "Date", optional)]
21-
pub lte: Option<DateTime<Utc>>,
18+
/// "less than or equal" query (UTC)
19+
#[ts(type = "Date")]
20+
Lte(DateTime<Utc>),
2221

23-
/// Optional "greater than" query (UTC)
22+
/// "greater than" query (UTC)
2423
/// This is exclusive of the value
25-
#[ts(type = "Date", optional)]
26-
pub gt: Option<DateTime<Utc>>,
24+
#[ts(type = "Date")]
25+
Gt(DateTime<Utc>),
2726

28-
/// Optional "less than" query (UTC)
27+
/// "less than" query (UTC)
2928
/// This is exclusive of the value
30-
#[ts(type = "Date", optional)]
31-
pub lt: Option<DateTime<Utc>>,
29+
#[ts(type = "Date")]
30+
Lt(DateTime<Utc>),
3231
}
Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
11
use serde::{Deserialize, Serialize};
22
use ts_rs::TS;
3-
use validator::Validate;
43

54
use crate::ID;
65

76
/// Query parameters for searching on an ID (or list of IDs)
8-
#[derive(Deserialize, Serialize, TS, Debug, Validate, Clone)]
7+
#[derive(Deserialize, Serialize, TS, Debug, Clone)]
98
#[serde(rename_all = "camelCase")]
109
#[ts(export, rename = "IDQuery")]
11-
pub struct IdQuery {
12-
/// Optional ID (equality test)
13-
#[ts(optional)]
14-
pub eq: Option<ID>,
10+
pub enum IDQuery {
11+
/// ID (equality test)
12+
Eq(ID),
1513

16-
/// Optional ID (inequality test)
17-
/// If "eq" is provided, this field is ignored
18-
#[ts(optional)]
19-
pub ne: Option<ID>,
14+
/// ID (inequality test)
15+
Ne(ID),
2016

21-
/// Optional bool (existence test)
22-
/// If "eq" is provided, this field is ignored
23-
#[ts(optional)]
24-
pub exists: Option<bool>,
17+
/// Bool (existence test)
18+
Exists(bool),
2519

26-
/// Optional list of IDs (equality test)
27-
/// If "eq" is provided, this field is ignored
28-
/// (use r# in the field name as "in" is a reserved keyword)
29-
#[ts(optional)]
30-
pub r#in: Option<Vec<ID>>,
20+
/// List of IDs (equality test)
21+
In(Vec<ID>),
3122
}

0 commit comments

Comments
 (0)