starting to fix the update methods

All of these methods were using the id in the db as the index in the
vec to select which one to update, therefore not actually updating the item.
This commit is contained in:
Chris Cochrun 2023-10-27 11:16:13 -05:00
parent edef42da29
commit 1b7f8d3994

View file

@ -259,43 +259,60 @@ pub mod song_model {
} }
} }
fn get_index(
mut self: Pin<&mut Self>,
song_id: i32,
) -> usize {
self.as_ref()
.songs()
.iter()
.position(|x| x.id == song_id)
.unwrap()
}
#[qinvokable] #[qinvokable]
pub fn update_title( pub fn update_title(
mut self: Pin<&mut Self>, mut self: Pin<&mut Self>,
index: i32, song_id: i32,
updated_title: QString, updated_title: QString,
) -> bool { ) -> bool {
let mut vector_roles = QVector_i32::default(); let mut vector_roles = QVector_i32::default();
vector_roles vector_roles
.append(self.as_ref().get_role(Role::TitleRole)); .append(self.as_ref().get_role(Role::TitleRole));
let index = self
.as_mut()
.songs()
.iter()
.position(|x| x.id == song_id)
.unwrap();
let model_index = &self.as_ref().index( let model_index = &self.as_ref().index(
index, index as i32,
0, 0,
&QModelIndex::default(), &QModelIndex::default(),
); );
let db = &mut self.as_mut().get_db(); let db = &mut self.as_mut().get_db();
let result = update(songs.filter(id.eq(index))) let result = update(songs.filter(id.eq(song_id)))
.set(title.eq(updated_title.to_string())) .set(title.eq(updated_title.to_string()))
.execute(db); .execute(db);
match result { match result {
Ok(_i) => { Ok(_i) => {
if let Some(song) = self debug!(_i, index);
for song in self
.as_mut() .as_mut()
.songs_mut() .songs_mut()
.get_mut(index as usize) .iter_mut()
.filter(|x| x.id == song_id)
{ {
debug!(?song);
song.title = updated_title.to_string(); song.title = updated_title.to_string();
self.as_mut().emit_data_changed(
model_index,
model_index,
&vector_roles,
);
self.as_mut().emit_title_changed();
true
} else {
false
} }
self.as_mut().emit_data_changed(
model_index,
model_index,
&vector_roles,
);
true
} }
Err(_e) => false, Err(_e) => false,
} }
@ -304,7 +321,7 @@ pub mod song_model {
#[qinvokable] #[qinvokable]
pub fn update_lyrics( pub fn update_lyrics(
mut self: Pin<&mut Self>, mut self: Pin<&mut Self>,
index: i32, song_id: i32,
updated_lyrics: QString, updated_lyrics: QString,
) -> bool { ) -> bool {
let mut vector_roles = QVector_i32::default(); let mut vector_roles = QVector_i32::default();