rs_plugin_common_interfaces/domain/
book.rs1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4use crate::domain::{
5 media::MediaItemReference, other_ids::OtherIds, person::Person, rs_ids::{ApplyRsIds, RsIds}, tag::Tag
6};
7
8#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
9#[serde(rename_all = "camelCase")]
10pub struct Book {
11 #[serde(default)]
12 pub id: String,
13 pub name: String,
14 #[serde(skip_serializing_if = "Option::is_none")]
15 #[serde(rename = "type")]
16 pub kind: Option<String>,
17 #[serde(skip_serializing_if = "Option::is_none")]
18 pub serie_ref: Option<String>,
19 #[serde(skip_serializing_if = "Option::is_none")]
20 pub volume: Option<f64>,
21 #[serde(skip_serializing_if = "Option::is_none")]
22 pub chapter: Option<f64>,
23 #[serde(skip_serializing_if = "Option::is_none")]
24 pub year: Option<u16>,
25 #[serde(skip_serializing_if = "Option::is_none")]
26 pub airdate: Option<i64>,
27 #[serde(skip_serializing_if = "Option::is_none")]
28 pub overview: Option<String>,
29 #[serde(skip_serializing_if = "Option::is_none")]
30 pub pages: Option<u32>,
31 #[serde(skip_serializing_if = "Option::is_none")]
32 pub params: Option<Value>,
33 #[serde(skip_serializing_if = "Option::is_none")]
34 pub lang: Option<String>,
35 #[serde(skip_serializing_if = "Option::is_none")]
36 pub original: Option<String>,
37 #[serde(skip_serializing_if = "Option::is_none")]
38 pub isbn13: Option<String>,
39 #[serde(skip_serializing_if = "Option::is_none")]
40 pub openlibrary_edition_id: Option<String>,
41 #[serde(skip_serializing_if = "Option::is_none")]
42 pub openlibrary_work_id: Option<String>,
43 #[serde(skip_serializing_if = "Option::is_none")]
44 pub google_books_volume_id: Option<String>,
45 #[serde(skip_serializing_if = "Option::is_none")]
46 pub asin: Option<String>,
47 pub otherids: Option<OtherIds>,
48 #[serde(default)]
49 pub modified: u64,
50 #[serde(default)]
51 pub added: u64,
52}
53
54impl From<Book> for RsIds {
55 fn from(value: Book) -> Self {
56 RsIds {
57 redseat: Some(value.id),
58 isbn13: value.isbn13,
59 openlibrary_edition_id: value.openlibrary_edition_id,
60 openlibrary_work_id: value.openlibrary_work_id,
61 google_books_volume_id: value.google_books_volume_id,
62 asin: value.asin,
63 other_ids: value.otherids,
64 volume: value.volume,
65 chapter: value.chapter,
66 ..Default::default()
67 }
68 }
69}
70
71impl ApplyRsIds for Book {
72 fn apply_rs_ids(&mut self, ids: &RsIds) {
73 if let Some(redseat) = ids.redseat.as_ref() {
74 self.id = redseat.clone();
75 }
76 if let Some(isbn13) = ids.isbn13.as_ref() {
77 self.isbn13 = Some(isbn13.clone());
78 }
79 if let Some(openlibrary_edition_id) = ids.openlibrary_edition_id.as_ref() {
80 self.openlibrary_edition_id = Some(openlibrary_edition_id.clone());
81 }
82 if let Some(openlibrary_work_id) = ids.openlibrary_work_id.as_ref() {
83 self.openlibrary_work_id = Some(openlibrary_work_id.clone());
84 }
85 if let Some(google_books_volume_id) = ids.google_books_volume_id.as_ref() {
86 self.google_books_volume_id = Some(google_books_volume_id.clone());
87 }
88 if let Some(asin) = ids.asin.as_ref() {
89 self.asin = Some(asin.clone());
90 }
91 if let Some(other_ids) = ids.other_ids.as_ref() {
92 self.otherids = Some(other_ids.clone());
93 }
94 if let Some(volume) = ids.volume {
95 self.volume = Some(volume);
96 }
97 if let Some(chapter) = ids.chapter {
98 self.chapter = Some(chapter);
99 }
100 }
101}
102
103#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
104#[serde(rename_all = "camelCase")]
105pub struct BookForUpdate {
106 pub name: Option<String>,
107 #[serde(rename = "type")]
108 pub kind: Option<String>,
109 pub serie_ref: Option<String>,
110 pub volume: Option<f64>,
111 pub chapter: Option<f64>,
112 pub year: Option<u16>,
113 pub airdate: Option<i64>,
114 pub overview: Option<String>,
115 pub pages: Option<u32>,
116 pub params: Option<Value>,
117 pub lang: Option<String>,
118 pub original: Option<String>,
119 pub isbn13: Option<String>,
120 pub openlibrary_edition_id: Option<String>,
121 pub openlibrary_work_id: Option<String>,
122 pub google_books_volume_id: Option<String>,
123 pub asin: Option<String>,
124 pub otherids: Option<OtherIds>,
125
126
127 pub add_tags: Option<Vec<MediaItemReference>>,
128 pub remove_tags: Option<Vec<String>>,
129 pub tags_lookup: Option<Vec<Tag>>,
130
131 pub add_people: Option<Vec<MediaItemReference>>,
132 pub remove_people: Option<Vec<String>>,
133 pub people_lookup: Option<Vec<Person>>,
134}
135
136impl BookForUpdate {
137 pub fn has_update(&self) -> bool {
138 self != &BookForUpdate::default()
139 }
140}
141
142#[cfg(test)]
143mod tests {
144 use super::Book;
145 use crate::domain::{
146 other_ids::OtherIds,
147 rs_ids::{ApplyRsIds, RsIds},
148 };
149 use serde_json::json;
150
151 #[test]
152 fn book_otherids_serializes_as_array_and_rejects_string() {
153 let book = Book {
154 id: "book-1".to_string(),
155 name: "Book 1".to_string(),
156 otherids: Some(OtherIds(vec!["goodreads:321".to_string()])),
157 ..Default::default()
158 };
159 let value = serde_json::to_value(&book).unwrap();
160 assert_eq!(value.get("otherids"), Some(&json!(["goodreads:321"])));
161
162 let parsed: Book = serde_json::from_value(json!({
163 "id": "book-1",
164 "name": "Book 1",
165 "otherids": ["custom:1"]
166 }))
167 .unwrap();
168 assert_eq!(
169 parsed.otherids,
170 Some(OtherIds(vec!["custom:1".to_string()]))
171 );
172
173 let invalid = serde_json::from_value::<Book>(json!({
174 "id": "book-1",
175 "name": "Book 1",
176 "otherids": "custom:1"
177 }));
178 assert!(invalid.is_err());
179 }
180
181 #[test]
182 fn book_apply_rs_ids_updates_only_present_values() {
183 let mut book = Book {
184 id: "book-old".to_string(),
185 name: "Book 1".to_string(),
186 openlibrary_work_id: Some("olw-old".to_string()),
187 chapter: Some(7.0),
188 ..Default::default()
189 };
190 let ids = RsIds {
191 redseat: Some("book-new".to_string()),
192 isbn13: Some("9783161484100".to_string()),
193 openlibrary_edition_id: Some("ole-new".to_string()),
194 google_books_volume_id: Some("gb-1".to_string()),
195 asin: Some("B00TEST".to_string()),
196 other_ids: Some(OtherIds(vec!["goodreads:42".to_string()])),
197 volume: Some(3.0),
198 ..Default::default()
199 };
200
201 book.apply_rs_ids(&ids);
202
203 assert_eq!(book.id, "book-new");
204 assert_eq!(book.isbn13.as_deref(), Some("9783161484100"));
205 assert_eq!(book.openlibrary_edition_id.as_deref(), Some("ole-new"));
206 assert_eq!(book.openlibrary_work_id.as_deref(), Some("olw-old"));
207 assert_eq!(book.google_books_volume_id.as_deref(), Some("gb-1"));
208 assert_eq!(book.asin.as_deref(), Some("B00TEST"));
209 assert_eq!(
210 book.otherids,
211 Some(OtherIds(vec!["goodreads:42".to_string()]))
212 );
213 assert_eq!(book.volume, Some(3.0));
214 assert_eq!(book.chapter, Some(7.0));
215 }
216}