/* * Copyright (C) 2014 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 . */ #include "Types.hpp" namespace Database { Playlist::Playlist() : _isPublic(false) { } Playlist::Playlist(std::string name, bool isPublic, Wt::Dbo::ptr user) : _name(name), _isPublic(isPublic), _user(user) { } Playlist::pointer Playlist::create(Wt::Dbo::Session& session, std::string name, bool isPublic, Wt::Dbo::ptr user) { return session.add( new Playlist(name, isPublic, user) ); } PlaylistEntry::PlaylistEntry() : _pos(0) { } Playlist::pointer Playlist::get(Wt::Dbo::Session& session, std::string name, Wt::Dbo::ptr user) { return session.find().where("name = ? AND user_id = ?").bind(name).bind(user.id()); } std::vector Playlist::getAll(Wt::Dbo::Session& session, Wt::Dbo::ptr user) { Wt::Dbo::collection res = session.find().where("user_id = ?").bind(user.id()).orderBy("name"); return std::vector(res.begin(), res.end()); } void Playlist::addTrack(Wt::Dbo::ptr track) { _entries.insert( PlaylistEntry::create(*session(), track, self(), _entries.size()) ); } PlaylistEntry::PlaylistEntry(Wt::Dbo::ptr track, Wt::Dbo::ptr playlist, int pos) : _pos(pos), _track(track), _playlist(playlist) { } PlaylistEntry::pointer PlaylistEntry::create(Wt::Dbo::Session& session, Wt::Dbo::ptr track, Wt::Dbo::ptr playlist, int pos) { return session.add( new PlaylistEntry( track, playlist, pos) ); } std::vector> Playlist::getTracks(int offset, int size) const { assert(session()); Wt::Dbo::collection> entries = session()->find().where("playlist_id = ?").bind(self().id()).orderBy("pos").offset(offset).limit(size); std::vector> res; for (auto entry : entries) { res.push_back(entry->getTrack()); } return res; } } // namespace Database