/ desktop / datalake / artist.cpp
artist.cpp
 1  #include <QSqlQuery>
 2  
 3  #include "database.hpp"
 4  #include "structs.hpp"
 5  
 6  KotoArtist::KotoArtist() {
 7    this->uuid = QUuid::createUuid();
 8  }
 9  
10  KotoArtist* KotoArtist::fromDb(const QSqlQuery& query, const QSqlRecord& record) {
11    KotoArtist* artist = new KotoArtist();
12    artist->uuid       = QUuid {query.value(record.indexOf("id")).toString()};
13    artist->name       = QString {query.value(record.indexOf("name")).toString()};
14    artist->path       = QString {query.value(record.indexOf("art_path")).toString()};
15    return artist;
16  }
17  
18  KotoArtist::~KotoArtist() {
19    for (auto album : this->albums) { delete album; }
20    for (auto track : this->tracks) { delete track; }
21    this->albums.clear();
22    this->tracks.clear();
23  }
24  
25  void KotoArtist::addAlbum(KotoAlbum* album) {
26    this->albums.append(album);
27  }
28  
29  void KotoArtist::addTrack(KotoTrack* track) {
30    this->tracks.append(track);
31    if (!track->album_uuid.has_value()) return;
32    for (auto album : this->albums) {
33      if (album->uuid == track->album_uuid.value()) {
34        album->addTrack(track);
35        return;
36      }
37    }
38  }
39  
40  void KotoArtist::commit() {
41    QSqlQuery query(KotoDatabase::instance().getDatabase());
42    query.prepare("INSERT INTO artists(id, name, art_path) VALUES (:id, :name, :art_path) ON CONFLICT(id) DO UPDATE SET name = :name, art_path = :art_path");
43    query.bindValue(":id", this->uuid.toString());
44    query.bindValue(":name", this->name);
45    query.bindValue(":art_path", this->path);
46    query.exec();
47  }
48  
49  QList<KotoAlbum*> KotoArtist::getAlbums() {
50    return QList {this->albums};
51  }
52  
53  std::optional<KotoAlbum*> KotoArtist::getAlbumByName(QString name) {
54    for (auto album : this->albums) {
55      if (album->getTitle().contains(name)) { return std::optional {album}; }
56    }
57  
58    return std::nullopt;
59  }
60  
61  QString KotoArtist::getName() {
62    return QString {this->name};
63  }
64  
65  QList<KotoTrack*> KotoArtist::getTracks() {
66    return QList {this->tracks};
67  }
68  
69  void KotoArtist::removeAlbum(KotoAlbum* album) {
70    this->albums.removeOne(album);
71  }
72  
73  void KotoArtist::removeTrack(KotoTrack* track) {
74    this->tracks.removeOne(track);
75  }
76  
77  void KotoArtist::setName(QString str) {
78    this->name = QString {str};
79  }
80  
81  void KotoArtist::setPath(QString str) {
82    this->path = QString {str};
83  }