/*
* 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 "services/database/TrackList.hpp"
#include
#include "utils/Logger.hpp"
#include "services/database/Artist.hpp"
#include "services/database/Cluster.hpp"
#include "services/database/Release.hpp"
#include "services/database/Session.hpp"
#include "services/database/User.hpp"
#include "services/database/Track.hpp"
#include "SqlQuery.hpp"
#include "StringViewTraits.hpp"
#include "Traits.hpp"
namespace Database {
TrackList::TrackList(std::string_view name, Type type, bool isPublic, ObjectPtr user)
: _name {name},
_type {type},
_isPublic {isPublic},
_user {getDboPtr(user)}
{
}
TrackList::pointer
TrackList::create(Session& session, std::string_view name, Type type, bool isPublic, ObjectPtr user)
{
session.checkUniqueLocked();
assert(user);
TrackList::pointer res {session.getDboSession().add( std::make_unique(name, type, isPublic, user) )};
session.getDboSession().flush();
return res;
}
TrackList::pointer
TrackList::get(Session& session, std::string_view name, Type type, ObjectPtr user)
{
session.checkSharedLocked();
assert(user);
return session.getDboSession().find()
.where("name = ?").bind(name)
.where("type = ?").bind(type)
.where("user_id = ?").bind(user->getId()).resultValue();
}
std::vector
TrackList::getAll(Session& session)
{
session.checkSharedLocked();
auto res = session.getDboSession().find().resultList();
return std::vector(res.begin(), res.end());
}
std::vector
TrackList::getAll(Session& session, ObjectPtr user)
{
session.checkSharedLocked();
auto res {session.getDboSession().find()
.where("user_id = ?").bind(user->getId())
.orderBy("name COLLATE NOCASE")
.resultList()};
return std::vector(res.begin(), res.end());
}
std::vector
TrackList::getAll(Session& session, ObjectPtr user, Type type)
{
session.checkSharedLocked();
auto res {session.getDboSession().find()
.where("user_id = ?").bind(user->getId())
.where("type = ?").bind(type)
.orderBy("name COLLATE NOCASE")
.resultList()};
return std::vector(res.begin(), res.end());
}
TrackList::pointer
TrackList::getById(Session& session, TrackListId id)
{
session.checkSharedLocked();
return session.getDboSession().find().where("id = ?").bind(id).resultValue();
}
bool
TrackList::isEmpty() const
{
return _entries.empty();
}
std::size_t
TrackList::getCount() const
{
return _entries.size();
}
TrackListEntry::pointer
TrackList::getEntry(std::size_t pos) const
{
TrackListEntry::pointer res;
auto entries = getEntries(pos, 1);
if (!entries.empty())
res = entries.front();
return res;
}
std::vector
TrackList::getEntries(std::optional offset, std::optional size) const
{
assert(session());
auto entries {
session()->find()
.where("tracklist_id = ?").bind(getId())
.orderBy("id")
.limit(size ? static_cast(*size) : -1)
.offset(offset ? static_cast(*offset) : -1)
.resultList()};
return std::vector(entries.begin(), entries.end());
}
TrackListEntry::pointer
TrackList::getEntryByTrackAndDateTime(ObjectPtr