editor: First changes for work editor

This commit is contained in:
Elias Projahn 2024-05-31 13:39:27 +02:00
parent 15ba043050
commit 55b344605b
19 changed files with 1291 additions and 19 deletions

View file

@ -2,7 +2,7 @@ pub mod models;
pub mod schema;
pub mod tables;
use std::collections::HashMap;
use std::{collections::HashMap, fmt::Display};
use anyhow::{anyhow, Result};
use diesel::{
@ -63,6 +63,12 @@ impl TranslatedString {
}
}
impl Display for TranslatedString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.get())
}
}
impl<DB: Backend> FromSql<Text, DB> for TranslatedString
where
String: FromSql<Text, DB>,

View file

@ -16,7 +16,7 @@ pub struct Work {
pub work_id: String,
pub name: TranslatedString,
pub parts: Vec<WorkPart>,
pub persons: Vec<Person>,
pub persons: Vec<Composer>,
pub instruments: Vec<Instrument>,
}
@ -27,6 +27,14 @@ pub struct WorkPart {
pub name: TranslatedString,
}
#[derive(Queryable, Selectable, Clone, Debug)]
pub struct Composer {
#[diesel(embed)]
pub person: Person,
#[diesel(embed)]
pub role: Role,
}
#[derive(Clone, Debug)]
pub struct Ensemble {
pub ensemble_id: String,
@ -65,6 +73,45 @@ impl PartialEq for Person {
}
}
impl Display for Person {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}
impl Display for Instrument {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}
impl Eq for Instrument {}
impl PartialEq for Instrument {
fn eq(&self, other: &Self) -> bool {
self.instrument_id == other.instrument_id
}
}
impl Display for Role {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}
impl Eq for Role {}
impl PartialEq for Role {
fn eq(&self, other: &Self) -> bool {
self.role_id == other.role_id
}
}
impl Eq for Composer {}
impl PartialEq for Composer {
fn eq(&self, other: &Self) -> bool {
self.person == other.person && self.role == other.role
}
}
impl Work {
pub fn from_table(data: tables::Work, connection: &mut SqliteConnection) -> Result<Self> {
fn visit_children(
@ -95,11 +142,11 @@ impl Work {
let parts = visit_children(&data.work_id, 0, connection)?;
let persons: Vec<Person> = persons::table
.inner_join(work_persons::table)
let persons: Vec<Composer> = persons::table
.inner_join(work_persons::table.inner_join(roles::table))
.order(work_persons::sequence_number)
.filter(work_persons::work_id.eq(&data.work_id))
.select(tables::Person::as_select())
.select(Composer::as_select())
.load(connection)?;
let instruments: Vec<Instrument> = instruments::table
@ -119,9 +166,10 @@ impl Work {
}
pub fn composers_string(&self) -> String {
// TODO: Include roles except default composer.
self.persons
.iter()
.map(|p| p.name.get().to_string())
.map(|p| p.person.name.get().to_string())
.collect::<Vec<String>>()
.join(", ")
}
@ -134,6 +182,12 @@ impl PartialEq for Work {
}
}
impl Display for Work {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.composers_string(), self.name)
}
}
impl Ensemble {
pub fn from_table(data: tables::Ensemble, connection: &mut SqliteConnection) -> Result<Self> {
let persons: Vec<(Person, Instrument)> = persons::table
@ -158,6 +212,12 @@ impl PartialEq for Ensemble {
}
}
impl Display for Ensemble {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}
impl Recording {
pub fn from_table(
data: tables::Recording,
@ -227,6 +287,12 @@ impl Recording {
}
}
impl Display for Recording {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}; {}", self.work, self.performers_string())
}
}
impl Performer {
pub fn from_table(
data: tables::RecordingPerson,

View file

@ -4,10 +4,12 @@
use chrono::NaiveDateTime;
use diesel::prelude::*;
use diesel::sqlite::Sqlite;
use gtk::glib::{self, Boxed};
use super::{schema::*, TranslatedString};
#[derive(Insertable, Queryable, Selectable, Clone, Debug)]
#[derive(Boxed, Insertable, Queryable, Selectable, Clone, Debug)]
#[boxed_type(name = "MusicusPerson")]
#[diesel(check_for_backend(Sqlite))]
pub struct Person {
pub person_id: String,
@ -18,7 +20,8 @@ pub struct Person {
pub last_played_at: Option<NaiveDateTime>,
}
#[derive(Insertable, Queryable, Selectable, Clone, Debug)]
#[derive(Boxed, Insertable, Queryable, Selectable, Clone, Debug)]
#[boxed_type(name = "MusicusRole")]
#[diesel(check_for_backend(Sqlite))]
pub struct Role {
pub role_id: String,
@ -28,7 +31,8 @@ pub struct Role {
pub last_used_at: NaiveDateTime,
}
#[derive(Insertable, Queryable, Selectable, Clone, Debug)]
#[derive(Boxed, Insertable, Queryable, Selectable, Clone, Debug)]
#[boxed_type(name = "MusicusInstrument")]
#[diesel(check_for_backend(Sqlite))]
pub struct Instrument {
pub instrument_id: String,