| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
use chrono::prelude::*; |
| 21 |
use diesel::prelude::*; |
| 22 |
|
| 23 |
use crate::database::connection; |
| 24 |
use crate::errors::DataError; |
| 25 |
use crate::make_id_wrapper; |
| 26 |
use crate::models::{Save, Show, ShowId}; |
| 27 |
use crate::schema::episodes; |
| 28 |
|
| 29 |
make_id_wrapper!(EpisodeId); |
| 30 |
|
| 31 |
|
| 32 |
pub trait EpisodeModel { |
| 33 |
|
| 34 |
fn id(&self) -> EpisodeId; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
fn uri(&self) -> Option<&str>; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
fn played(&self) -> Option<NaiveDateTime>; |
| 45 |
} |
| 46 |
|
| 47 |
#[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq, Selectable)] |
| 48 |
#[diesel(table_name = episodes)] |
| 49 |
#[diesel(treat_none_as_null = true)] |
| 50 |
#[diesel(belongs_to(Show, foreign_key = show_id))] |
| 51 |
#[derive(Debug, Clone)] |
| 52 |
|
| 53 |
pub struct Episode { |
| 54 |
id: EpisodeId, |
| 55 |
title: String, |
| 56 |
uri: Option<String>, |
| 57 |
local_uri: Option<String>, |
| 58 |
description: Option<String>, |
| 59 |
image_uri: Option<String>, |
| 60 |
epoch: NaiveDateTime, |
| 61 |
length: Option<i32>, |
| 62 |
duration: Option<i32>, |
| 63 |
guid: Option<String>, |
| 64 |
played: Option<NaiveDateTime>, |
| 65 |
play_position: i32, |
| 66 |
show_id: ShowId, |
| 67 |
} |
| 68 |
|
| 69 |
impl EpisodeModel for Episode { |
| 70 |
|
| 71 |
fn id(&self) -> EpisodeId { |
| 72 |
self.id |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
fn uri(&self) -> Option<&str> { |
| 79 |
self.uri.as_deref() |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
fn played(&self) -> Option<NaiveDateTime> { |
| 86 |
self.played |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
impl Save<Episode> for Episode { |
| 91 |
type Error = DataError; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
fn save(&self) -> Result<Episode, Self::Error> { |
| 96 |
let db = connection(); |
| 97 |
let mut tempdb = db.get()?; |
| 98 |
|
| 99 |
self.save_changes::<Episode>(&mut tempdb) |
| 100 |
.map_err(From::from) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
impl Episode { |
| 105 |
|
| 106 |
pub fn id(&self) -> EpisodeId { |
| 107 |
self.id |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
pub fn title(&self) -> &str { |
| 112 |
&self.title |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
pub fn local_uri(&self) -> Option<&str> { |
| 120 |
self.local_uri.as_deref() |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
pub fn description(&self) -> Option<&str> { |
| 125 |
self.description.as_deref() |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
pub fn guid(&self) -> Option<&str> { |
| 130 |
self.guid.as_deref() |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
pub fn epoch(&self) -> NaiveDateTime { |
| 138 |
self.epoch |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
pub fn length(&self) -> Option<i32> { |
| 145 |
self.length |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
pub fn duration(&self) -> Option<i32> { |
| 152 |
self.duration |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
pub fn show_id(&self) -> ShowId { |
| 157 |
self.show_id |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
pub fn play_position(&self) -> i32 { |
| 165 |
self.play_position |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
pub fn image_uri(&self) -> Option<&str> { |
| 173 |
self.image_uri.as_deref() |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
pub fn set_play_position(&mut self, seconds: i32) -> Result<(), DataError> { |
| 178 |
self.play_position = seconds; |
| 179 |
self.save().map(|_| ()) |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
pub fn set_play_position_if_divergent(&mut self, seconds: i32) -> Result<(), DataError> { |
| 185 |
if seconds != 0 && self.play_position != 0 { |
| 186 |
if (seconds - self.play_position).abs() > 10 { |
| 187 |
return self.set_play_position(seconds); |
| 188 |
} |
| 189 |
} else { |
| 190 |
return self.set_play_position(seconds); |
| 191 |
} |
| 192 |
Ok(()) |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
#[derive(Queryable, AsChangeset, PartialEq, Selectable)] |
| 197 |
#[diesel(table_name = episodes)] |
| 198 |
#[diesel(treat_none_as_null = true)] |
| 199 |
#[diesel(primary_key(title, show_id))] |
| 200 |
#[derive(Debug, Clone)] |
| 201 |
|
| 202 |
pub struct EpisodeWidgetModel { |
| 203 |
id: EpisodeId, |
| 204 |
title: String, |
| 205 |
uri: Option<String>, |
| 206 |
local_uri: Option<String>, |
| 207 |
epoch: NaiveDateTime, |
| 208 |
length: Option<i32>, |
| 209 |
duration: Option<i32>, |
| 210 |
played: Option<NaiveDateTime>, |
| 211 |
play_position: i32, |
| 212 |
show_id: ShowId, |
| 213 |
} |
| 214 |
|
| 215 |
impl EpisodeModel for EpisodeWidgetModel { |
| 216 |
|
| 217 |
fn id(&self) -> EpisodeId { |
| 218 |
self.id |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
fn uri(&self) -> Option<&str> { |
| 225 |
self.uri.as_deref() |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
fn played(&self) -> Option<NaiveDateTime> { |
| 232 |
self.played |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
impl From<Episode> for EpisodeWidgetModel { |
| 237 |
fn from(e: Episode) -> EpisodeWidgetModel { |
| 238 |
EpisodeWidgetModel { |
| 239 |
id: e.id, |
| 240 |
title: e.title, |
| 241 |
uri: e.uri, |
| 242 |
local_uri: e.local_uri, |
| 243 |
epoch: e.epoch, |
| 244 |
length: e.length, |
| 245 |
duration: e.duration, |
| 246 |
played: e.played, |
| 247 |
play_position: e.play_position, |
| 248 |
show_id: e.show_id, |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
impl Save<usize> for EpisodeWidgetModel { |
| 254 |
type Error = DataError; |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
fn save(&self) -> Result<usize, Self::Error> { |
| 259 |
use crate::schema::episodes::dsl::*; |
| 260 |
|
| 261 |
let db = connection(); |
| 262 |
let mut tempdb = db.get()?; |
| 263 |
|
| 264 |
diesel::update(episodes.filter(id.eq(self.id))) |
| 265 |
.set(self) |
| 266 |
.execute(&mut tempdb) |
| 267 |
.map_err(From::from) |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
impl EpisodeWidgetModel { |
| 272 |
|
| 273 |
pub fn title(&self) -> &str { |
| 274 |
&self.title |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
pub fn local_uri(&self) -> Option<&str> { |
| 282 |
self.local_uri.as_deref() |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
pub fn set_local_uri(&mut self, value: Option<&str>) { |
| 287 |
self.local_uri = value.map(|x| x.to_string()); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
pub fn epoch(&self) -> NaiveDateTime { |
| 295 |
self.epoch |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
pub fn length(&self) -> Option<i32> { |
| 302 |
self.length |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
pub fn set_length(&mut self, value: Option<i32>) { |
| 307 |
self.length = value; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
pub fn duration(&self) -> Option<i32> { |
| 314 |
self.duration |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
fn set_played(&mut self, value: Option<NaiveDateTime>) { |
| 319 |
self.played = value; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
pub fn show_id(&self) -> ShowId { |
| 324 |
self.show_id |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
pub fn set_unplayed(&mut self) -> Result<(), DataError> { |
| 329 |
self.set_played(None); |
| 330 |
self.save().map(|_| ()) |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
pub fn set_played_now(&mut self) -> Result<(), DataError> { |
| 335 |
self.set_played(Some(Utc::now().naive_utc())); |
| 336 |
self.save().map(|_| ()) |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
pub fn play_position(&self) -> i32 { |
| 344 |
self.play_position |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
#[derive(Queryable, AsChangeset, PartialEq, Selectable)] |
| 349 |
#[diesel(table_name = episodes)] |
| 350 |
#[diesel(treat_none_as_null = true)] |
| 351 |
#[diesel(primary_key(title, show_id))] |
| 352 |
#[derive(Debug, Clone)] |
| 353 |
|
| 354 |
pub struct EpisodeCleanerModel { |
| 355 |
id: EpisodeId, |
| 356 |
local_uri: Option<String>, |
| 357 |
played: Option<NaiveDateTime>, |
| 358 |
} |
| 359 |
|
| 360 |
impl Save<usize> for EpisodeCleanerModel { |
| 361 |
type Error = DataError; |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
fn save(&self) -> Result<usize, Self::Error> { |
| 366 |
use crate::schema::episodes::dsl::*; |
| 367 |
|
| 368 |
let db = connection(); |
| 369 |
let mut tempdb = db.get()?; |
| 370 |
|
| 371 |
diesel::update(episodes.filter(id.eq(self.id))) |
| 372 |
.set(self) |
| 373 |
.execute(&mut tempdb) |
| 374 |
.map_err(From::from) |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
impl From<Episode> for EpisodeCleanerModel { |
| 379 |
fn from(e: Episode) -> EpisodeCleanerModel { |
| 380 |
EpisodeCleanerModel { |
| 381 |
id: e.id(), |
| 382 |
local_uri: e.local_uri, |
| 383 |
played: e.played, |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
impl EpisodeCleanerModel { |
| 389 |
|
| 390 |
pub fn id(&self) -> EpisodeId { |
| 391 |
self.id |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
pub fn local_uri(&self) -> Option<&str> { |
| 399 |
self.local_uri.as_deref() |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
pub fn set_local_uri(&mut self, value: Option<&str>) { |
| 404 |
self.local_uri = value.map(|x| x.to_string()); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
pub fn played(&self) -> Option<NaiveDateTime> { |
| 411 |
self.played |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
pub fn set_played(&mut self, value: Option<NaiveDateTime>) { |
| 416 |
self.played = value; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
#[derive(Queryable, AsChangeset, PartialEq, Selectable)] |
| 421 |
#[diesel(table_name = episodes)] |
| 422 |
#[diesel(treat_none_as_null = true)] |
| 423 |
#[diesel(primary_key(title, show_id))] |
| 424 |
#[derive(Debug, Clone)] |
| 425 |
|
| 426 |
pub struct EpisodeMinimal { |
| 427 |
id: EpisodeId, |
| 428 |
title: String, |
| 429 |
uri: Option<String>, |
| 430 |
image_uri: Option<String>, |
| 431 |
epoch: NaiveDateTime, |
| 432 |
length: Option<i32>, |
| 433 |
duration: Option<i32>, |
| 434 |
play_position: i32, |
| 435 |
guid: Option<String>, |
| 436 |
show_id: ShowId, |
| 437 |
} |
| 438 |
|
| 439 |
impl From<Episode> for EpisodeMinimal { |
| 440 |
fn from(e: Episode) -> Self { |
| 441 |
EpisodeMinimal { |
| 442 |
id: e.id, |
| 443 |
title: e.title, |
| 444 |
uri: e.uri, |
| 445 |
image_uri: e.image_uri, |
| 446 |
length: e.length, |
| 447 |
guid: e.guid, |
| 448 |
epoch: e.epoch, |
| 449 |
duration: e.duration, |
| 450 |
play_position: e.play_position, |
| 451 |
show_id: e.show_id, |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
impl EpisodeMinimal { |
| 457 |
|
| 458 |
pub fn id(&self) -> EpisodeId { |
| 459 |
self.id |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
pub fn title(&self) -> &str { |
| 464 |
&self.title |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
pub fn uri(&self) -> Option<&str> { |
| 471 |
self.uri.as_deref() |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
pub fn image_uri(&self) -> Option<&str> { |
| 476 |
self.image_uri.as_deref() |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
pub fn guid(&self) -> Option<&str> { |
| 481 |
self.guid.as_deref() |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
pub fn epoch(&self) -> NaiveDateTime { |
| 489 |
self.epoch |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
pub fn length(&self) -> Option<i32> { |
| 496 |
self.length |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
pub fn duration(&self) -> Option<i32> { |
| 503 |
self.duration |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
pub fn show_id(&self) -> ShowId { |
| 508 |
self.show_id |
| 509 |
} |
| 510 |
} |