Select Git revision
Dockerfile_debian_testing
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
databasehelper.cpp 11.33 KiB
/****************************************************************************
* Copyright (C) 2017 Savoir-faire Linux *
* Author: Nicolas Jäger <nicolas.jager@savoirfairelinux.com> *
* Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com> *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
#include "databasehelper.h"
#include "api/profile.h"
namespace lrc
{
namespace authority
{
namespace database
{
std::string
getProfileId(Database& db, const std::string& uri)
{
auto ids = db.select("id", "profiles","uri=:uri", {{":uri", uri}}).payloads;
return ids.empty() ? "" : ids[0];
}
std::string
getOrInsertProfile(Database& db,
const std::string& contactUri,
const std::string& alias,
const std::string& avatar,
const std::string& type)
{
// Check if profile is already present.
auto profileAlreadyExists = db.select("id",
"profiles",
"uri=:uri",
{{":uri", contactUri}});
if (profileAlreadyExists.payloads.empty()) {
// Doesn't exists, add contact to the database
auto row = db.insertInto("profiles",
{{":uri", "uri"}, {":alias", "alias"}, {":photo", "photo"}, {":type", "type"},
{":status", "status"}},
{{":uri", contactUri}, {":alias", alias}, {":photo", avatar}, {":type", type},
{":status", "TRUSTED"}});
if (row == -1) {
qDebug() << "contact not added to the database";
return "";
}
return std::to_string(row);
} else {
// Exists, update and retrieve it.
if (!avatar.empty() && !alias.empty()) {
db.update("profiles",
"alias=:alias, photo=:photo",
{{":alias", alias}, {":photo", avatar}},
"uri=:uri", {{":uri", contactUri}});