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_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::pin::Pin;
use tracing::{debug, error};
@ -256,7 +256,6 @@ impl video_model::VideoModel {
return false;
}
let video_id = self.videos.get(index as usize).unwrap().id;
let thread = self.qt_thread();
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
@ -462,31 +461,39 @@ impl video_model::VideoModel {
let model_index =
&self.as_ref().index(index, 0, &QModelIndex::default());
let db = &mut self.as_mut().get_db();
let result = update(videos.filter(id.eq(index)))
.set(end_time.eq(updated_end_time))
.execute(db);
match result {
Ok(_i) => {
for video in self
.as_mut()
.rust_mut()
.videos
.iter_mut()
.filter(|x| x.id == index)
{
video.end_time = updated_end_time;
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let result = query!("UPDATE videos SET endTime = ? where id = ?", updated_end_time, index)
.execute(&mut self.as_mut().rust_mut().db)
.await;
match result {
Ok(_i) => {
for video in self
.as_mut()
.rust_mut()
.videos
.iter_mut()
.filter(|x| x.id == index)
{
video.end_time = updated_end_time;
debug!(title = video.title,
end_time = updated_end_time,
"updated video end_time");
}
self.as_mut().data_changed(
model_index,
model_index,
&vector_roles,
);
true
}
self.as_mut().data_changed(
model_index,
model_index,
&vector_roles,
);
println!("rust-end-time: {updated_end_time:?}");
true
Err(e) => {
error!("Error connecting to db: {e}");
false
},
}
Err(_e) => false,
}
});
true
}
pub fn update_start_time(
@ -500,31 +507,39 @@ impl video_model::VideoModel {
let model_index =
&self.as_ref().index(index, 0, &QModelIndex::default());
let db = &mut self.as_mut().get_db();
let result = update(videos.filter(id.eq(index)))
.set(start_time.eq(updated_start_time))
.execute(db);
match result {
Ok(_i) => {
for video in self
.as_mut()
.rust_mut()
.videos
.iter_mut()
.filter(|x| x.id == index)
{
video.start_time = updated_start_time;
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let result = query!("UPDATE videos SET startTime = ? where id = ?", updated_start_time, index)
.execute(&mut self.as_mut().rust_mut().db)
.await;
match result {
Ok(_i) => {
for video in self
.as_mut()
.rust_mut()
.videos
.iter_mut()
.filter(|x| x.id == index)
{
video.start_time = updated_start_time;
debug!(title = video.title,
start_time = updated_start_time,
"updated video start_time");
}
self.as_mut().data_changed(
model_index,
model_index,
&vector_roles,
);
true
}
self.as_mut().data_changed(
model_index,
model_index,
&vector_roles,
);
println!("rust-start-time: {:?}", updated_start_time);
true
Err(e) => {
error!("Error connecting to db: {e}");
false
},
}
Err(_e) => false,
}
});
true
}
pub fn update_title(
@ -538,33 +553,40 @@ impl video_model::VideoModel {
let model_index =
&self.as_ref().index(index, 0, &QModelIndex::default());
let db = &mut self.as_mut().get_db();
let result = update(videos.filter(id.eq(index)))
.set(title.eq(updated_title.to_string()))
.execute(db);
match result {
Ok(_i) => {
for video in self
.as_mut()
.rust_mut()
.videos
.iter_mut()
.filter(|x| x.id == index)
{
video.title = updated_title.clone().to_string();
println!("rust-title: {:?}", video.title);
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let title = updated_title.to_string();
let result = query!("UPDATE videos SET title = ? where id = ?", title, index)
.execute(&mut self.as_mut().rust_mut().db)
.await;
match result {
Ok(_i) => {
for video in self
.as_mut()
.rust_mut()
.videos
.iter_mut()
.filter(|x| x.id == index)
{
video.title = title.clone();
debug!(title = video.title,
title = title,
"updated video title");
}
self.as_mut().data_changed(
model_index,
model_index,
&vector_roles,
);
true
}
// TODO this seems to not be updating in the actual list
self.as_mut().data_changed(
model_index,
model_index,
&vector_roles,
);
println!("rust-title: {:?}", updated_title);
true
Err(e) => {
error!("Error connecting to db: {e}");
false
},
}
Err(_e) => false,
}
});
true
}
pub fn update_path(
@ -577,32 +599,40 @@ impl video_model::VideoModel {
let model_index =
&self.as_ref().index(index, 0, &QModelIndex::default());
let db = &mut self.as_mut().get_db();
let result = update(videos.filter(id.eq(index)))
.set(path.eq(updated_path.to_string()))
.execute(db);
match result {
Ok(_i) => {
for video in self
.as_mut()
.rust_mut()
.videos
.iter_mut()
.filter(|x| x.id == index)
{
video.path = updated_path.clone().to_string();
println!("rust-title: {:?}", video.title);
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let updated_path = updated_path.to_string();
let result = query!("UPDATE videos SET filePath = ? where id = ?", updated_path, index)
.execute(&mut self.as_mut().rust_mut().db)
.await;
match result {
Ok(_i) => {
for video in self
.as_mut()
.rust_mut()
.videos
.iter_mut()
.filter(|x| x.id == index)
{
video.path = updated_path.clone();
debug!(title = video.title,
path = updated_path,
"updated video path");
}
self.as_mut().data_changed(
model_index,
model_index,
&vector_roles,
);
true
}
self.as_mut().data_changed(
model_index,
model_index,
&vector_roles,
);
println!("rust-path: {:?}", updated_path);
true
Err(e) => {
error!("Error connecting to db: {e}");
false
},
}
Err(_e) => false,
}
});
true
}
fn search(mut self: Pin<&mut Self>, search_term: QString) {