finishing video_model transition to sqlx

This commit is contained in:
Chris Cochrun 2024-09-25 11:08:03 -05:00
parent df07b13b28
commit a697e4d89c

View file

@ -180,7 +180,7 @@ mod video_model {
use cxx_qt::{CxxQtType, Threading}; use cxx_qt::{CxxQtType, Threading};
use cxx_qt_lib::{QByteArray, QModelIndex, QString, QUrl, QVariant}; use cxx_qt_lib::{QByteArray, QModelIndex, QString, QUrl, QVariant};
use sqlx::{query, query_as, SqliteConnection}; use sqlx::{query, query_as, Connection, SqliteConnection};
use std::path::PathBuf; use std::path::PathBuf;
use std::pin::Pin; use std::pin::Pin;
use tracing::{debug, error}; use tracing::{debug, error};
@ -256,7 +256,6 @@ impl video_model::VideoModel {
return false; return false;
} }
let video_id = self.videos.get(index as usize).unwrap().id; let video_id = self.videos.get(index as usize).unwrap().id;
let thread = self.qt_thread();
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async { rt.block_on(async {
@ -462,10 +461,11 @@ impl video_model::VideoModel {
let model_index = let model_index =
&self.as_ref().index(index, 0, &QModelIndex::default()); &self.as_ref().index(index, 0, &QModelIndex::default());
let db = &mut self.as_mut().get_db(); let rt = tokio::runtime::Runtime::new().unwrap();
let result = update(videos.filter(id.eq(index))) rt.block_on(async {
.set(end_time.eq(updated_end_time)) let result = query!("UPDATE videos SET endTime = ? where id = ?", updated_end_time, index)
.execute(db); .execute(&mut self.as_mut().rust_mut().db)
.await;
match result { match result {
Ok(_i) => { Ok(_i) => {
for video in self for video in self
@ -476,17 +476,24 @@ impl video_model::VideoModel {
.filter(|x| x.id == index) .filter(|x| x.id == index)
{ {
video.end_time = updated_end_time; video.end_time = updated_end_time;
debug!(title = video.title,
end_time = updated_end_time,
"updated video end_time");
} }
self.as_mut().data_changed( self.as_mut().data_changed(
model_index, model_index,
model_index, model_index,
&vector_roles, &vector_roles,
); );
println!("rust-end-time: {updated_end_time:?}");
true true
} }
Err(_e) => false, Err(e) => {
error!("Error connecting to db: {e}");
false
},
} }
});
true
} }
pub fn update_start_time( pub fn update_start_time(
@ -500,10 +507,11 @@ impl video_model::VideoModel {
let model_index = let model_index =
&self.as_ref().index(index, 0, &QModelIndex::default()); &self.as_ref().index(index, 0, &QModelIndex::default());
let db = &mut self.as_mut().get_db(); let rt = tokio::runtime::Runtime::new().unwrap();
let result = update(videos.filter(id.eq(index))) rt.block_on(async {
.set(start_time.eq(updated_start_time)) let result = query!("UPDATE videos SET startTime = ? where id = ?", updated_start_time, index)
.execute(db); .execute(&mut self.as_mut().rust_mut().db)
.await;
match result { match result {
Ok(_i) => { Ok(_i) => {
for video in self for video in self
@ -514,17 +522,24 @@ impl video_model::VideoModel {
.filter(|x| x.id == index) .filter(|x| x.id == index)
{ {
video.start_time = updated_start_time; video.start_time = updated_start_time;
debug!(title = video.title,
start_time = updated_start_time,
"updated video start_time");
} }
self.as_mut().data_changed( self.as_mut().data_changed(
model_index, model_index,
model_index, model_index,
&vector_roles, &vector_roles,
); );
println!("rust-start-time: {:?}", updated_start_time);
true true
} }
Err(_e) => false, Err(e) => {
error!("Error connecting to db: {e}");
false
},
} }
});
true
} }
pub fn update_title( pub fn update_title(
@ -538,10 +553,12 @@ impl video_model::VideoModel {
let model_index = let model_index =
&self.as_ref().index(index, 0, &QModelIndex::default()); &self.as_ref().index(index, 0, &QModelIndex::default());
let db = &mut self.as_mut().get_db(); let rt = tokio::runtime::Runtime::new().unwrap();
let result = update(videos.filter(id.eq(index))) rt.block_on(async {
.set(title.eq(updated_title.to_string())) let title = updated_title.to_string();
.execute(db); let result = query!("UPDATE videos SET title = ? where id = ?", title, index)
.execute(&mut self.as_mut().rust_mut().db)
.await;
match result { match result {
Ok(_i) => { Ok(_i) => {
for video in self for video in self
@ -551,20 +568,25 @@ impl video_model::VideoModel {
.iter_mut() .iter_mut()
.filter(|x| x.id == index) .filter(|x| x.id == index)
{ {
video.title = updated_title.clone().to_string(); video.title = title.clone();
println!("rust-title: {:?}", video.title); debug!(title = video.title,
title = title,
"updated video title");
} }
// TODO this seems to not be updating in the actual list
self.as_mut().data_changed( self.as_mut().data_changed(
model_index, model_index,
model_index, model_index,
&vector_roles, &vector_roles,
); );
println!("rust-title: {:?}", updated_title);
true true
} }
Err(_e) => false, Err(e) => {
error!("Error connecting to db: {e}");
false
},
} }
});
true
} }
pub fn update_path( pub fn update_path(
@ -577,10 +599,12 @@ impl video_model::VideoModel {
let model_index = let model_index =
&self.as_ref().index(index, 0, &QModelIndex::default()); &self.as_ref().index(index, 0, &QModelIndex::default());
let db = &mut self.as_mut().get_db(); let rt = tokio::runtime::Runtime::new().unwrap();
let result = update(videos.filter(id.eq(index))) rt.block_on(async {
.set(path.eq(updated_path.to_string())) let updated_path = updated_path.to_string();
.execute(db); let result = query!("UPDATE videos SET filePath = ? where id = ?", updated_path, index)
.execute(&mut self.as_mut().rust_mut().db)
.await;
match result { match result {
Ok(_i) => { Ok(_i) => {
for video in self for video in self
@ -590,19 +614,25 @@ impl video_model::VideoModel {
.iter_mut() .iter_mut()
.filter(|x| x.id == index) .filter(|x| x.id == index)
{ {
video.path = updated_path.clone().to_string(); video.path = updated_path.clone();
println!("rust-title: {:?}", video.title); debug!(title = video.title,
path = updated_path,
"updated video path");
} }
self.as_mut().data_changed( self.as_mut().data_changed(
model_index, model_index,
model_index, model_index,
&vector_roles, &vector_roles,
); );
println!("rust-path: {:?}", updated_path);
true true
} }
Err(_e) => false, Err(e) => {
error!("Error connecting to db: {e}");
false
},
} }
});
true
} }
fn search(mut self: Pin<&mut Self>, search_term: QString) { fn search(mut self: Pin<&mut Self>, search_term: QString) {