Update the code to use new datetime in the DB.

This commit is contained in:
Joe Ardent 2024-04-07 14:00:56 -07:00
parent 51427ecdb5
commit a7104e54aa
12 changed files with 21 additions and 49 deletions

View File

@ -5,7 +5,7 @@ create table if not exists watches (
title text not null, title text not null,
metadata_url text, -- possible url for imdb or other metadata-esque site to show the user metadata_url text, -- possible url for imdb or other metadata-esque site to show the user
length int, length int,
release_date datetime, release_date date,
added_by blob not null, -- ID of the user that added it added_by blob not null, -- ID of the user that added it
last_updated datetime not null default CURRENT_TIMESTAMP, last_updated datetime not null default CURRENT_TIMESTAMP,
foreign key (added_by) references users (id) foreign key (added_by) references users (id)
@ -26,7 +26,7 @@ create table if not exists watch_quests (
priority int, -- 1-5 how much do you want to watch it priority int, -- 1-5 how much do you want to watch it
public boolean not null default true, public boolean not null default true,
watched boolean not null default false, watched boolean not null default false,
when_watched int, when_watched datetime,
created_at datetime not null default CURRENT_TIMESTAMP, created_at datetime not null default CURRENT_TIMESTAMP,
last_updated datetime not null default CURRENT_TIMESTAMP, last_updated datetime not null default CURRENT_TIMESTAMP,
foreign key (user) references users (id) on delete cascade on update no action, foreign key (user) references users (id) on delete cascade on update no action,

View File

@ -2,8 +2,8 @@ create table if not exists stars (
id blob not null primary key default (julid_new()), id blob not null primary key default (julid_new()),
name text not null, name text not null,
metadata_url text, metadata_url text,
born int, born date,
died int died date
); );
create index if not exists stars_name_dex on stars (lower(name)); create index if not exists stars_name_dex on stars (lower(name));

View File

@ -3,7 +3,6 @@ use sqlx::{Connection, Sqlite, SqlitePool};
use crate::{ use crate::{
import_utils::{insert_credit, insert_star, insert_watch}, import_utils::{insert_credit, insert_star, insert_watch},
misc_util::year_to_epoch,
Credit, ShowKind, Star, Watch, Credit, ShowKind, Star, Watch,
}; };
@ -25,7 +24,7 @@ impl From<ImportImdbMovie> for Watch {
Watch { Watch {
id: OMEGA_ID, // this is ignored by the inserter id: OMEGA_ID, // this is ignored by the inserter
title: value.title, title: value.title,
release_date: year_to_epoch(value.year.as_deref()), release_date: value.year,
length: value.length.and_then(|v| v.parse::<i64>().ok()), length: value.length.and_then(|v| v.parse::<i64>().ok()),
kind: ShowKind::Movie, kind: ShowKind::Movie,
metadata_url: Some(format!("https://imdb.com/title/{}/", &value.id)), metadata_url: Some(format!("https://imdb.com/title/{}/", &value.id)),
@ -39,7 +38,7 @@ impl From<&ImportImdbMovie> for Watch {
Watch { Watch {
id: OMEGA_ID, id: OMEGA_ID,
title: value.title.to_string(), title: value.title.to_string(),
release_date: year_to_epoch(value.year.as_deref()), release_date: value.year.clone(),
length: value.length.as_ref().and_then(|v| v.parse::<i64>().ok()), length: value.length.as_ref().and_then(|v| v.parse::<i64>().ok()),
kind: ShowKind::Movie, kind: ShowKind::Movie,
metadata_url: Some(format!("https://imdb.com/title/{}/", value.id)), metadata_url: Some(format!("https://imdb.com/title/{}/", value.id)),
@ -66,8 +65,8 @@ impl From<&ImdbStar> for Star {
Self { Self {
name: value.name.clone(), name: value.name.clone(),
metadata_url, metadata_url,
born: year_to_epoch(value.born.as_deref()), born: value.born.clone(),
died: year_to_epoch(value.died.as_deref()), died: value.died.clone(),
..Default::default() ..Default::default()
} }
} }

View File

@ -31,8 +31,8 @@ pub async fn insert_star(star: &Star, db: &mut SqliteConnection) -> Julid {
sqlx::query_scalar(q) sqlx::query_scalar(q)
.bind(&star.name) .bind(&star.name)
.bind(&star.metadata_url) .bind(&star.metadata_url)
.bind(star.born) .bind(&star.born)
.bind(star.died) .bind(&star.died)
.fetch_one(db) .fetch_one(db)
.await .await
.unwrap() .unwrap()

View File

@ -35,17 +35,3 @@ where
.map(Some), .map(Some),
} }
} }
/// Convert a stringy number like "1999" to a 64-bit signed unix epoch-based
/// timestamp
pub fn year_to_epoch(year: Option<&str>) -> Option<i64> {
year?
.trim()
.parse::<i32>()
.map(|year| {
let years = (year - 1970) as f32;
let days = (years * 365.2425) as i64;
days * 24 * 60 * 60
})
.ok()
}

View File

@ -32,7 +32,7 @@ pub struct SearchQuery {
#[serde(default, deserialize_with = "empty_string_as_none")] #[serde(default, deserialize_with = "empty_string_as_none")]
pub kind: Option<String>, pub kind: Option<String>,
#[serde(default, deserialize_with = "empty_string_as_none")] #[serde(default, deserialize_with = "empty_string_as_none")]
pub year: Option<i64>, pub year: Option<String>,
} }
pub async fn get_search_watch( pub async fn get_search_watch(

View File

@ -7,8 +7,8 @@ pub struct Star {
pub id: Julid, pub id: Julid,
pub name: String, pub name: String,
pub metadata_url: Option<String>, pub metadata_url: Option<String>,
pub born: Option<i64>, pub born: Option<String>,
pub died: Option<i64>, pub died: Option<String>,
} }
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, FromRow)] #[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, FromRow)]

View File

@ -10,8 +10,7 @@ use sqlx::{query, query_as, query_scalar, SqlitePool};
use super::templates::{AddNewWatchPage, AddWatchButton, GetWatchPage}; use super::templates::{AddNewWatchPage, AddWatchButton, GetWatchPage};
use crate::{ use crate::{
misc_util::{empty_string_as_none, year_to_epoch}, misc_util::empty_string_as_none, AuthSession, MyWatchesPage, ShowKind, Watch, WatchQuest,
AuthSession, MyWatchesPage, ShowKind, Watch, WatchQuest,
}; };
//-************************************************************************ //-************************************************************************
@ -99,12 +98,11 @@ pub async fn post_add_new_watch(
) -> Result<impl IntoResponse, AddError> { ) -> Result<impl IntoResponse, AddError> {
if let Some(user) = auth.user { if let Some(user) = auth.user {
{ {
let release_date = year_to_epoch(form.year.as_deref());
let watch = Watch { let watch = Watch {
title: form.title, title: form.title,
kind: form.kind, kind: form.kind,
metadata_url: form.metadata_url, metadata_url: form.metadata_url,
release_date, release_date: form.year,
added_by: user.id, added_by: user.id,
..Default::default() ..Default::default()
}; };
@ -132,7 +130,7 @@ async fn add_new_watch_impl(db_pool: &SqlitePool, watch: &Watch) -> Result<Julid
let watch_id: Julid = query_scalar(ADD_WATCH_QUERY) let watch_id: Julid = query_scalar(ADD_WATCH_QUERY)
.bind(&watch.title) .bind(&watch.title)
.bind(watch.kind) .bind(watch.kind)
.bind(watch.release_date) .bind(&watch.release_date)
.bind(&watch.metadata_url) .bind(&watch.metadata_url)
.bind(watch.added_by) .bind(watch.added_by)
.bind(watch.length) .bind(watch.length)

View File

@ -73,22 +73,10 @@ pub struct Watch {
pub kind: ShowKind, pub kind: ShowKind,
pub metadata_url: Option<String>, pub metadata_url: Option<String>,
pub length: Option<i64>, pub length: Option<i64>,
pub release_date: Option<i64>, pub release_date: Option<String>,
pub added_by: Julid, // this shouldn't be exposed to randos in the application pub added_by: Julid, // this shouldn't be exposed to randos in the application
} }
impl Watch {
pub fn year(&self) -> Option<String> {
if let Some(year) = self.release_date {
let date = chrono::NaiveDateTime::from_timestamp_opt(year, 0)?;
let year = format!("{}", date.format("%Y"));
Some(year)
} else {
None
}
}
}
//-************************************************************************ //-************************************************************************
/// Something a user wants to watch /// Something a user wants to watch
//-************************************************************************ //-************************************************************************

View File

@ -12,7 +12,7 @@
{% when Some with (watch) %} {% when Some with (watch) %}
<div class="watch"> <div class="watch">
<span class="watchtitle">{{watch.title}}</span> -- {% call m::get_or_default(watch.year(), "when??") %} <span class="watchtitle">{{watch.title}}</span> -- {% call m::get_or_default(watch.release_date, "when??") %}
</div> </div>
{% else %} {% else %}

View File

@ -28,7 +28,8 @@
<div class="watchlist"> <div class="watchlist">
<ul> <ul>
{% for watch in watches %} {% for watch in watches %}
<li><span class="watchtitle">{{watch.title}}</span> -- {% call m::get_or_default(watch.year(), "when??") %} <li><span class="watchtitle">{{watch.title}}</span> -- {% call m::get_or_default(watch.release_date, "when??")
%}
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>

View File

@ -9,7 +9,7 @@
</span> </span>
</td> </td>
<td>{{res.kind}}</td> <td>{{res.kind}}</td>
<td> {% call m::get_or_default(res.year(), "when??") -%}</td> <td> {% call m::get_or_default(res.release_date, "when??") -%}</td>
<td> <td>
<span id="add-watch-{{res.id}}"> <span id="add-watch-{{res.id}}">
<span hx-get="/watch/status/{{res.id}}" hx-target="this" hx-trigger="load, reveal" <span hx-get="/watch/status/{{res.id}}" hx-target="this" hx-trigger="load, reveal"