Skip to content

Commit 691124d

Browse files
committed
Return a near-empty model when missing data
1 parent 030986c commit 691124d

4 files changed

Lines changed: 86 additions & 59 deletions

File tree

database/sqlx-data.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
"type_info": {
169169
"type": "VarString",
170170
"flags": {
171-
"bits": 4105
171+
"bits": 4097
172172
},
173173
"char_set": 224,
174174
"max_size": 1020
@@ -180,7 +180,7 @@
180180
"type_info": {
181181
"type": "VarString",
182182
"flags": {
183-
"bits": 8
183+
"bits": 0
184184
},
185185
"char_set": 224,
186186
"max_size": 1020
@@ -1338,7 +1338,7 @@
13381338
"type_info": {
13391339
"type": "VarString",
13401340
"flags": {
1341-
"bits": 8
1341+
"bits": 0
13421342
},
13431343
"char_set": 224,
13441344
"max_size": 1020
@@ -1398,7 +1398,7 @@
13981398
"type_info": {
13991399
"type": "VarString",
14001400
"flags": {
1401-
"bits": 4105
1401+
"bits": 4097
14021402
},
14031403
"char_set": 224,
14041404
"max_size": 1020
@@ -1448,7 +1448,7 @@
14481448
"type_info": {
14491449
"type": "VarString",
14501450
"flags": {
1451-
"bits": 4105
1451+
"bits": 4097
14521452
},
14531453
"char_set": 224,
14541454
"max_size": 1020
@@ -1919,7 +1919,7 @@
19191919
"type_info": {
19201920
"type": "VarString",
19211921
"flags": {
1922-
"bits": 4105
1922+
"bits": 4097
19231923
},
19241924
"char_set": 224,
19251925
"max_size": 1020
@@ -1931,7 +1931,7 @@
19311931
"type_info": {
19321932
"type": "VarString",
19331933
"flags": {
1934-
"bits": 8
1934+
"bits": 0
19351935
},
19361936
"char_set": 224,
19371937
"max_size": 1020

src/controllers/api/comic/by_id.rs

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use crate::controllers::api::comic::editor_data::fetch_editor_data_for_comic;
22
use crate::controllers::api::comic::navigation_data::{
33
fetch_all_item_navigation_data, fetch_comic_item_navigation_data,
44
};
5-
use crate::models::{Comic, ComicId, Exclusion, Inclusion, ItemColor, ItemType};
5+
use crate::models::{
6+
Comic, ComicData, ComicId, Exclusion, False, Inclusion, ItemColor, ItemType, MissingComic,
7+
PresentComic, True,
8+
};
69
use crate::util::NewsUpdater;
710
use actix_web::{error, web, HttpResponse, Result};
811
use actix_web_grants::permissions::{AuthDetails, PermissionsCheck};
@@ -129,60 +132,42 @@ pub(crate) async fn by_id(
129132
let comic = if let Some(comic) = comic {
130133
Comic {
131134
comic: comic_id,
132-
has_data: true,
133-
image_type: Some(comic.image_type.into()),
134-
publish_date: comic.publish_date.map(|nd| Utc.from_utc_datetime(&nd)),
135-
is_accurate_publish_date: comic.is_accurate_publish_date != 0,
136-
title: Some(comic.title),
137-
tagline: comic.tagline,
138-
is_guest_comic: comic.is_guest_comic != 0,
139-
is_non_canon: comic.is_non_canon != 0,
140-
has_no_cast: comic.has_no_cast != 0,
141-
has_no_location: comic.has_no_location != 0,
142-
has_no_storyline: comic.has_no_storyline != 0,
143-
has_no_title: comic.has_no_title != 0,
144-
has_no_tagline: comic.has_no_tagline != 0,
145-
news: news.map(|n| n.news),
146-
previous: previous
147-
.map(TryInto::try_into)
148-
.transpose()
149-
.expect("database has valid comicIds"),
150-
next: next
151-
.map(TryInto::try_into)
152-
.transpose()
153-
.expect("database has valid comicIds"),
154135
editor_data,
155-
items: comic_navigation_items,
156136
all_items: all_navigation_items,
137+
data: ComicData::PresentComic(PresentComic {
138+
has_data: True::default(),
139+
image_type: Some(comic.image_type.into()),
140+
publish_date: comic.publish_date.map(|nd| Utc.from_utc_datetime(&nd)),
141+
is_accurate_publish_date: comic.is_accurate_publish_date != 0,
142+
title: Some(comic.title),
143+
tagline: comic.tagline,
144+
is_guest_comic: comic.is_guest_comic != 0,
145+
is_non_canon: comic.is_non_canon != 0,
146+
has_no_cast: comic.has_no_cast != 0,
147+
has_no_location: comic.has_no_location != 0,
148+
has_no_storyline: comic.has_no_storyline != 0,
149+
has_no_title: comic.has_no_title != 0,
150+
has_no_tagline: comic.has_no_tagline != 0,
151+
news: news.map(|n| n.news),
152+
previous: previous
153+
.map(TryInto::try_into)
154+
.transpose()
155+
.expect("database has valid comicIds"),
156+
next: next
157+
.map(TryInto::try_into)
158+
.transpose()
159+
.expect("database has valid comicIds"),
160+
items: comic_navigation_items,
161+
}),
157162
}
158163
} else {
159164
Comic {
160165
comic: comic_id,
161-
has_data: false,
162-
image_type: None,
163-
publish_date: None,
164-
is_accurate_publish_date: false,
165-
title: None,
166-
tagline: None,
167-
is_guest_comic: false,
168-
is_non_canon: false,
169-
has_no_cast: false,
170-
has_no_location: false,
171-
has_no_storyline: false,
172-
has_no_title: false,
173-
has_no_tagline: false,
174-
news: None,
175-
previous: previous
176-
.map(TryInto::try_into)
177-
.transpose()
178-
.expect("database has valid comicIds"),
179-
next: next
180-
.map(TryInto::try_into)
181-
.transpose()
182-
.expect("database has valid comicIds"),
183166
editor_data,
184-
items: comic_navigation_items,
185167
all_items: all_navigation_items,
168+
data: ComicData::Missing(MissingComic {
169+
has_data: False::default(),
170+
}),
186171
}
187172
};
188173

src/models.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ mod image_type;
1111
mod item_color;
1212
mod item_id;
1313
mod item_type;
14+
mod set_boolean;
1415
mod token;
1516

1617
pub use comic_id::*;
1718
pub use image_type::ImageType;
1819
pub use item_color::ItemColor;
1920
pub use item_id::*;
2021
pub use item_type::ItemType;
22+
pub use set_boolean::*;
2123
pub use token::Token;
2224

2325
#[derive(Debug, Serialize)]
@@ -44,8 +46,33 @@ impl From<DatabaseComic> for ComicList {
4446
#[serde(rename_all = "camelCase")]
4547
pub struct Comic {
4648
pub comic: ComicId,
49+
#[serde(skip_serializing_if = "Option::is_none")]
50+
pub editor_data: Option<EditorData>,
51+
#[serde(skip_serializing_if = "Vec::is_empty")]
52+
pub all_items: Vec<ItemNavigationData>,
53+
#[serde(flatten)]
54+
pub data: ComicData,
55+
}
56+
57+
#[derive(Debug, Serialize)]
58+
#[serde(untagged)]
59+
#[allow(variant_size_differences)]
60+
pub enum ComicData {
61+
Missing(MissingComic),
62+
PresentComic(PresentComic),
63+
}
64+
65+
#[derive(Debug, Serialize)]
66+
#[serde(rename_all = "camelCase")]
67+
pub struct MissingComic {
68+
pub has_data: False,
69+
}
70+
71+
#[derive(Debug, Serialize)]
72+
#[serde(rename_all = "camelCase")]
73+
pub struct PresentComic {
4774
pub image_type: Option<ImageType>,
48-
pub has_data: bool,
75+
pub has_data: True,
4976
pub publish_date: Option<DateTime<Utc>>,
5077
pub is_accurate_publish_date: bool,
5178
pub title: Option<String>,
@@ -60,11 +87,7 @@ pub struct Comic {
6087
pub news: Option<String>,
6188
pub previous: Option<ComicId>,
6289
pub next: Option<ComicId>,
63-
#[serde(skip_serializing_if = "Option::is_none")]
64-
pub editor_data: Option<EditorData>,
6590
pub items: Vec<ItemNavigationData>,
66-
#[serde(skip_serializing_if = "Vec::is_empty")]
67-
pub all_items: Vec<ItemNavigationData>,
6891
}
6992

7093
#[derive(Debug, Serialize)]

src/models/set_boolean.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
#[derive(Debug, Deserialize, Serialize)]
4+
#[serde(transparent)]
5+
pub struct True(bool);
6+
impl Default for True {
7+
fn default() -> Self {
8+
Self(true)
9+
}
10+
}
11+
12+
#[derive(Debug, Deserialize, Serialize)]
13+
#[serde(transparent)]
14+
pub struct False(bool);
15+
impl Default for False {
16+
fn default() -> Self {
17+
Self(false)
18+
}
19+
}

0 commit comments

Comments
 (0)