-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Subsonci API: added savePlayQueue / getPlayQueue support
- Loading branch information
Showing
10 changed files
with
315 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright (C) 2024 Emeric Poupon | ||
* | ||
* This file is part of LMS. | ||
* | ||
* LMS is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* LMS is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with LMS. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "database/PlayQueue.hpp" | ||
|
||
#include <Wt/Dbo/SqlTraits.h> | ||
|
||
#include "database/Directory.hpp" | ||
#include "database/MediaLibrary.hpp" | ||
#include "database/Release.hpp" | ||
#include "database/Session.hpp" | ||
#include "database/Track.hpp" | ||
#include "database/User.hpp" | ||
|
||
#include "IdTypeTraits.hpp" | ||
#include "StringViewTraits.hpp" | ||
#include "Utils.hpp" | ||
|
||
namespace lms::db | ||
{ | ||
PlayQueue::PlayQueue(const ObjectPtr<User>& user, std::string_view name) | ||
: _name{ name } | ||
, _user{ getDboPtr(user) } | ||
{ | ||
} | ||
|
||
PlayQueue::pointer PlayQueue::create(Session& session, const ObjectPtr<User>& user, std::string_view name) | ||
{ | ||
return session.getDboSession()->add(std::unique_ptr<PlayQueue>{ new PlayQueue{ user, name } }); | ||
} | ||
|
||
std::size_t PlayQueue::getCount(Session& session) | ||
{ | ||
session.checkReadTransaction(); | ||
|
||
return utils::fetchQuerySingleResult(session.getDboSession()->query<int>("SELECT COUNT(*) FROM playqueue")); | ||
} | ||
|
||
PlayQueue::pointer PlayQueue::find(Session& session, PlayQueueId id) | ||
{ | ||
session.checkReadTransaction(); | ||
|
||
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<PlayQueue>>("SELECT p from playqueue p").where("p.id = ?").bind(id)); | ||
} | ||
|
||
PlayQueue::pointer PlayQueue::find(Session& session, UserId userId, std::string_view name) | ||
{ | ||
session.checkReadTransaction(); | ||
|
||
auto query{ session.getDboSession()->query<Wt::Dbo::ptr<PlayQueue>>("SELECT p from playqueue p") }; | ||
query.where("p.user_id = ?").bind(userId); | ||
query.where("p.name = ?").bind(name); | ||
|
||
return utils::fetchQuerySingleResult(query); | ||
} | ||
|
||
void PlayQueue::clear() | ||
{ | ||
_tracks.clear(); | ||
} | ||
|
||
void PlayQueue::addTrack(const ObjectPtr<Track>& track) | ||
{ | ||
_tracks.insert(getDboPtr(track)); | ||
} | ||
|
||
Track::pointer PlayQueue::getTrackAtCurrentIndex() const | ||
{ | ||
auto query{ _tracks.find() }; | ||
query.offset(_currentIndex); | ||
query.limit(1); | ||
|
||
return utils::fetchQuerySingleResult(query); | ||
} | ||
|
||
void PlayQueue::visitTracks(const std::function<void(const ObjectPtr<Track>& track)>& visitor) const | ||
{ | ||
utils::forEachQueryResult(_tracks.find(), visitor); | ||
} | ||
|
||
} // namespace lms::db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright (C) 2024 Emeric Poupon | ||
* | ||
* This file is part of LMS. | ||
* | ||
* LMS is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* LMS is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with LMS. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <chrono> | ||
#include <span> | ||
#include <string> | ||
#include <string_view> | ||
|
||
#include <Wt/Dbo/Dbo.h> | ||
#include <Wt/WDateTime.h> | ||
|
||
#include "database/Object.hpp" | ||
#include "database/TrackId.hpp" | ||
#include "database/Types.hpp" | ||
#include "database/UserId.hpp" | ||
|
||
LMS_DECLARE_IDTYPE(PlayQueueId) | ||
|
||
namespace lms::db | ||
{ | ||
class Session; | ||
class Track; | ||
class User; | ||
|
||
// Usage only for subsonic API | ||
class PlayQueue final : public Object<PlayQueue, PlayQueueId> | ||
{ | ||
public: | ||
PlayQueue() = default; | ||
|
||
static std::size_t getCount(Session& session); | ||
static pointer find(Session& session, PlayQueueId playQueueId); | ||
static pointer find(Session& session, UserId userId, std::string_view name); | ||
|
||
// Accessors | ||
std::string_view getName() const { return _name; } | ||
std::size_t getCurrentIndex() const { return _currentIndex; } | ||
std::chrono::milliseconds getCurrentPositionInTrack() const { return _currentPositionInTrack; } | ||
ObjectPtr<User> getUser() const { return _user; } | ||
ObjectPtr<Track> getTrack(std::size_t index) const; | ||
ObjectPtr<Track> getTrackAtCurrentIndex() const; | ||
// Get tracks, ordered by position | ||
std::vector<TrackId> getTrackIds() const; | ||
void visitTracks(const std::function<void(const ObjectPtr<Track>& track)>& visitor) const; | ||
const Wt::WDateTime getLastModifiedDateTime() const { return _lastModifiedDateTime; } | ||
|
||
// Modifiers | ||
void setCurrentIndex(std::size_t index) { _currentIndex = index; } | ||
void setCurrentPositionInTrack(std::chrono::milliseconds position) { _currentPositionInTrack = position; } | ||
void clear(); | ||
void addTrack(const ObjectPtr<Track>& track); | ||
void setLastModifiedDateTime(const Wt::WDateTime& lastModified) { _lastModifiedDateTime = lastModified; } | ||
|
||
template<class Action> | ||
void persist(Action& a) | ||
{ | ||
Wt::Dbo::field(a, _name, "name"); | ||
Wt::Dbo::field(a, _currentIndex, "current_index"); | ||
Wt::Dbo::field(a, _currentPositionInTrack, "current_position_in_track"); | ||
Wt::Dbo::field(a, _lastModifiedDateTime, "last_modified_date_time"); | ||
|
||
Wt::Dbo::belongsTo(a, _user, "user", Wt::Dbo::OnDeleteCascade); | ||
Wt::Dbo::hasMany(a, _tracks, Wt::Dbo::ManyToMany, "playqueue_track", "", Wt::Dbo::OnDeleteCascade); | ||
} | ||
|
||
private: | ||
friend class Session; | ||
PlayQueue(const ObjectPtr<User>& user, std::string_view name); | ||
static pointer create(Session& session, const ObjectPtr<User>& user, std::string_view name); | ||
|
||
std::string _name; | ||
int _currentIndex{}; | ||
std::chrono::duration<int, std::milli> _currentPositionInTrack{}; | ||
Wt::WDateTime _lastModifiedDateTime; | ||
|
||
Wt::Dbo::ptr<User> _user; | ||
Wt::Dbo::collection<Wt::Dbo::ptr<Track>> _tracks; | ||
}; | ||
} // namespace lms::db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.