sticker/Database: wrap in class StickerDatabase

This commit is contained in:
Max Kellermann
2019-04-24 15:18:01 +02:00
parent c88d5616f7
commit 77c9081f78
9 changed files with 273 additions and 243 deletions

View File

@@ -38,6 +38,7 @@
#include "Permission.hxx"
#include "tag/Type.h"
#include "Partition.hxx"
#include "Instance.hxx"
#include "client/Client.hxx"
#include "client/Response.hxx"
#include "util/Macros.hxx"
@@ -216,7 +217,7 @@ command_available(gcc_unused const Partition &partition,
{
#ifdef ENABLE_SQLITE
if (StringIsEqual(cmd->cmd, "sticker"))
return sticker_enabled();
return partition.instance.HasStickerDatabase();
#endif
#ifdef ENABLE_NEIGHBOR_PLUGINS

View File

@@ -28,6 +28,7 @@
#include "client/Client.hxx"
#include "client/Response.hxx"
#include "Partition.hxx"
#include "Instance.hxx"
#include "util/StringAPI.hxx"
#include "util/ScopeExit.hxx"
@@ -50,7 +51,9 @@ sticker_song_find_print_cb(const LightSong &song, const char *value,
}
static CommandResult
handle_sticker_song(Response &r, Partition &partition, Request args)
handle_sticker_song(Response &r, Partition &partition,
StickerDatabase &sticker_database,
Request args)
{
const Database &db = partition.GetDatabaseOrThrow();
@@ -62,7 +65,8 @@ handle_sticker_song(Response &r, Partition &partition, Request args)
assert(song != nullptr);
AtScopeExit(&db, song) { db.ReturnSong(song); };
const auto value = sticker_song_get_value(*song, args[3]);
const auto value = sticker_song_get_value(sticker_database,
*song, args[3]);
if (value.empty()) {
r.Error(ACK_ERROR_NO_EXIST, "no such sticker");
return CommandResult::ERROR;
@@ -77,7 +81,7 @@ handle_sticker_song(Response &r, Partition &partition, Request args)
assert(song != nullptr);
AtScopeExit(&db, song) { db.ReturnSong(song); };
const auto sticker = sticker_song_get(*song);
const auto sticker = sticker_song_get(sticker_database, *song);
sticker_print(r, sticker);
return CommandResult::OK;
@@ -87,7 +91,8 @@ handle_sticker_song(Response &r, Partition &partition, Request args)
assert(song != nullptr);
AtScopeExit(&db, song) { db.ReturnSong(song); };
sticker_song_set_value(*song, args[3], args[4]);
sticker_song_set_value(sticker_database, *song,
args[3], args[4]);
return CommandResult::OK;
/* delete song song_id [key] */
} else if ((args.size == 3 || args.size == 4) &&
@@ -97,8 +102,9 @@ handle_sticker_song(Response &r, Partition &partition, Request args)
AtScopeExit(&db, song) { db.ReturnSong(song); };
bool ret = args.size == 3
? sticker_song_delete(*song)
: sticker_song_delete_value(*song, args[3]);
? sticker_song_delete(sticker_database, *song)
: sticker_song_delete_value(sticker_database, *song,
args[3]);
if (!ret) {
r.Error(ACK_ERROR_NO_EXIST, "no such sticker");
return CommandResult::ERROR;
@@ -138,7 +144,7 @@ handle_sticker_song(Response &r, Partition &partition, Request args)
args[3],
};
sticker_song_find(db, base_uri, data.name,
sticker_song_find(sticker_database, db, base_uri, data.name,
op, value,
sticker_song_find_print_cb, &data);
@@ -154,13 +160,18 @@ handle_sticker(Client &client, Request args, Response &r)
{
assert(args.size >= 3);
if (!sticker_enabled()) {
auto &instance = client.GetInstance();
if (!instance.HasStickerDatabase()) {
r.Error(ACK_ERROR_UNKNOWN, "sticker database is disabled");
return CommandResult::ERROR;
}
auto &sticker_database = *instance.sticker_database;
if (StringIsEqual(args[1], "song"))
return handle_sticker_song(r, client.GetPartition(), args);
return handle_sticker_song(r, client.GetPartition(),
sticker_database,
args);
else {
r.Error(ACK_ERROR_ARG, "unknown sticker domain");
return CommandResult::ERROR;