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,
metadata_url text, -- possible url for imdb or other metadata-esque site to show the user
length int,
release_date datetime,
release_date date,
added_by blob not null, -- ID of the user that added it
last_updated datetime not null default CURRENT_TIMESTAMP,
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
public boolean not null default true,
watched boolean not null default false,
when_watched int,
when_watched datetime,
created_at 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,

View file

@ -2,8 +2,8 @@ create table if not exists stars (
id blob not null primary key default (julid_new()),
name text not null,
metadata_url text,
born int,
died int
born date,
died date
);
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::{
import_utils::{insert_credit, insert_star, insert_watch},
misc_util::year_to_epoch,
Credit, ShowKind, Star, Watch,
};
@ -25,7 +24,7 @@ impl From<ImportImdbMovie> for Watch {
Watch {
id: OMEGA_ID, // this is ignored by the inserter
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()),
kind: ShowKind::Movie,
metadata_url: Some(format!("https://imdb.com/title/{}/", &value.id)),
@ -39,7 +38,7 @@ impl From<&ImportImdbMovie> for Watch {
Watch {
id: OMEGA_ID,
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()),
kind: ShowKind::Movie,
metadata_url: Some(format!("https://imdb.com/title/{}/", value.id)),
@ -66,8 +65,8 @@ impl From<&ImdbStar> for Star {
Self {
name: value.name.clone(),
metadata_url,
born: year_to_epoch(value.born.as_deref()),
died: year_to_epoch(value.died.as_deref()),
born: value.born.clone(),
died: value.died.clone(),
..Default::default()
}
}

View file

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

View file

@ -35,17 +35,3 @@ where
.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")]
pub kind: Option<String>,
#[serde(default, deserialize_with = "empty_string_as_none")]
pub year: Option<i64>,
pub year: Option<String>,
}
pub async fn get_search_watch(

View file

@ -7,8 +7,8 @@ pub struct Star {
pub id: Julid,
pub name: String,
pub metadata_url: Option<String>,
pub born: Option<i64>,
pub died: Option<i64>,
pub born: Option<String>,
pub died: Option<String>,
}
#[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 crate::{
misc_util::{empty_string_as_none, year_to_epoch},
AuthSession, MyWatchesPage, ShowKind, Watch, WatchQuest,
misc_util::empty_string_as_none, AuthSession, MyWatchesPage, ShowKind, Watch, WatchQuest,
};
//-************************************************************************
@ -99,12 +98,11 @@ pub async fn post_add_new_watch(
) -> Result<impl IntoResponse, AddError> {
if let Some(user) = auth.user {
{
let release_date = year_to_epoch(form.year.as_deref());
let watch = Watch {
title: form.title,
kind: form.kind,
metadata_url: form.metadata_url,
release_date,
release_date: form.year,
added_by: user.id,
..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)
.bind(&watch.title)
.bind(watch.kind)
.bind(watch.release_date)
.bind(&watch.release_date)
.bind(&watch.metadata_url)
.bind(watch.added_by)
.bind(watch.length)

View file

@ -73,22 +73,10 @@ pub struct Watch {
pub kind: ShowKind,
pub metadata_url: Option<String>,
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
}
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
//-************************************************************************

View file

@ -12,7 +12,7 @@
{% when Some with (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>
{% else %}

View file

@ -28,7 +28,8 @@
<div class="watchlist">
<ul>
{% 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>
{% endfor %}
</ul>

View file

@ -9,7 +9,7 @@
</span>
</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>
<span id="add-watch-{{res.id}}">
<span hx-get="/watch/status/{{res.id}}" hx-target="this" hx-trigger="load, reveal"