remove most of the uid calls from application
This commit is contained in:
parent
d2229ca498
commit
c8dcb41f1b
@ -73,7 +73,7 @@ int main(int argc, char ** argv) {
|
|||||||
|
|
||||||
// categories factory
|
// categories factory
|
||||||
[db = dbIt->get()](eedb::User * owner) { //
|
[db = dbIt->get()](eedb::User * owner) { //
|
||||||
return std::make_unique< eedb::PgCategoriesRepository >(*db, owner);
|
return std::make_unique< eedb::PgCategoriesRepository >(*db);
|
||||||
},
|
},
|
||||||
|
|
||||||
env);
|
env);
|
||||||
|
|||||||
@ -17,7 +17,7 @@ class PgCategoriesRepository : public CategoriesRepository {
|
|||||||
public:
|
public:
|
||||||
// CategoriesRepository interface
|
// CategoriesRepository interface
|
||||||
public:
|
public:
|
||||||
PgCategoriesRepository(db::PgConnection & db, User * owner);
|
PgCategoriesRepository(db::PgConnection & db);
|
||||||
|
|
||||||
std::unique_ptr< Category > root() const override;
|
std::unique_ptr< Category > root() const override;
|
||||||
|
|
||||||
|
|||||||
@ -20,7 +20,7 @@ class PgCategory : public Category {
|
|||||||
spimpl::impl_ptr< PgCategoryPrivate > _priv;
|
spimpl::impl_ptr< PgCategoryPrivate > _priv;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PgCategory(db::PgConnection & db, User * user);
|
PgCategory(db::PgConnection & db);
|
||||||
PgCategory(spimpl::impl_ptr<PgCategoryPrivate> priv);
|
PgCategory(spimpl::impl_ptr<PgCategoryPrivate> priv);
|
||||||
|
|
||||||
string_view displayName() const override;
|
string_view displayName() const override;
|
||||||
|
|||||||
@ -7,19 +7,17 @@
|
|||||||
|
|
||||||
namespace eedb {
|
namespace eedb {
|
||||||
struct PgCategoriesRepository::PgCategoriesRepositoryPriv {
|
struct PgCategoriesRepository::PgCategoriesRepositoryPriv {
|
||||||
PgCategoriesRepositoryPriv(db::PgConnection & db, User * owner) : _db{db}, _user{owner} {}
|
PgCategoriesRepositoryPriv(db::PgConnection & db) : _db{db} {}
|
||||||
|
|
||||||
auto root() const {
|
auto root() const {
|
||||||
return std::make_unique< eedb::PgCategory >(_db, _user);
|
return std::make_unique< eedb::PgCategory >(_db);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
db::PgConnection & _db;
|
db::PgConnection & _db;
|
||||||
User * _user;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
PgCategoriesRepository::PgCategoriesRepository(db::PgConnection & db, User * owner)
|
PgCategoriesRepository::PgCategoriesRepository(db::PgConnection & db) : _priv{spimpl::make_unique_impl< PgCategoriesRepositoryPriv >(db)} {}
|
||||||
: _priv{spimpl::make_unique_impl< PgCategoriesRepositoryPriv >(db, owner)} {}
|
|
||||||
|
|
||||||
std::unique_ptr< Category > PgCategoriesRepository::root() const { //
|
std::unique_ptr< Category > PgCategoriesRepository::root() const { //
|
||||||
return _priv->root();
|
return _priv->root();
|
||||||
|
|||||||
@ -9,6 +9,8 @@
|
|||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
#include <sqlpp11/sqlpp11.h>
|
#include <sqlpp11/sqlpp11.h>
|
||||||
|
|
||||||
|
#include <eedb/db/pg/Stats.hpp>
|
||||||
|
|
||||||
namespace eedb {
|
namespace eedb {
|
||||||
|
|
||||||
struct PgCategory::PgCategoryPrivate {
|
struct PgCategory::PgCategoryPrivate {
|
||||||
@ -51,13 +53,13 @@ struct PgCategory::PgCategoryPrivate {
|
|||||||
public:
|
public:
|
||||||
Category * _self{nullptr};
|
Category * _self{nullptr};
|
||||||
|
|
||||||
PgCategoryPrivate(db::PgConnection & db, User * owner, Category * parent) : _db{db}, _owner{owner}, _parent{parent} {
|
PgCategoryPrivate(db::PgConnection & db, Category * parent) : _db{db}, _parent{parent} {
|
||||||
auto row = _db(select(columns()).from(table_list()).where(root_path_match()).limit(0ul));
|
auto row = _db(select(columns()).from(table_list()).where(root_path_match()).limit(0ul));
|
||||||
if(row.empty()) {
|
if(row.empty()) {
|
||||||
// no root category
|
// no root category
|
||||||
row = _db(sqlpp::postgresql::insert_into(t_category)
|
row = _db(sqlpp::postgresql::insert_into(t_category)
|
||||||
.set( //
|
.set( //
|
||||||
t_category.owner = _owner->uid(),
|
t_category.owner = sqlpp::verbatim< sqlpp::integer >("app_current_user_id()"),
|
||||||
t_category.status = 0,
|
t_category.status = 0,
|
||||||
t_category.parent_id = sqlpp::null,
|
t_category.parent_id = sqlpp::null,
|
||||||
t_category.name = "root",
|
t_category.name = "root",
|
||||||
@ -72,7 +74,7 @@ struct PgCategory::PgCategoryPrivate {
|
|||||||
auto children = std::make_unique< std::vector< std::unique_ptr< Category > > >();
|
auto children = std::make_unique< std::vector< std::unique_ptr< Category > > >();
|
||||||
children->reserve(100);
|
children->reserve(100);
|
||||||
for(auto & category_row : _db(select(columns()).from(table_list()).where(parent_match()))) {
|
for(auto & category_row : _db(select(columns()).from(table_list()).where(parent_match()))) {
|
||||||
auto priv = spimpl::make_impl< PgCategoryPrivate >(_db, _owner, _self);
|
auto priv = spimpl::make_impl< PgCategoryPrivate >(_db, _self);
|
||||||
priv->init_data(category_row);
|
priv->init_data(category_row);
|
||||||
children->emplace_back(std::make_unique< PgCategory >(std::move(priv)));
|
children->emplace_back(std::make_unique< PgCategory >(std::move(priv)));
|
||||||
}
|
}
|
||||||
@ -85,7 +87,7 @@ struct PgCategory::PgCategoryPrivate {
|
|||||||
|
|
||||||
auto create(std::string name, std::string description) const {
|
auto create(std::string name, std::string description) const {
|
||||||
const auto & row = _db(sqlpp::postgresql::insert_into(t_category)
|
const auto & row = _db(sqlpp::postgresql::insert_into(t_category)
|
||||||
.set(t_category.owner = _owner->uid(),
|
.set(t_category.owner = sqlpp::verbatim< sqlpp::integer >("app_current_user_id()"),
|
||||||
t_category.status = 0,
|
t_category.status = 0,
|
||||||
t_category.parent_id = _uid,
|
t_category.parent_id = _uid,
|
||||||
t_category.name = name,
|
t_category.name = name,
|
||||||
@ -93,7 +95,7 @@ struct PgCategory::PgCategoryPrivate {
|
|||||||
.returning(columns()))
|
.returning(columns()))
|
||||||
.front();
|
.front();
|
||||||
|
|
||||||
auto priv = spimpl::make_impl< PgCategoryPrivate >(_db, _owner, _self);
|
auto priv = spimpl::make_impl< PgCategoryPrivate >(_db, _self);
|
||||||
priv->init_data(row);
|
priv->init_data(row);
|
||||||
return std::make_unique< PgCategory >(std::move(priv));
|
return std::make_unique< PgCategory >(std::move(priv));
|
||||||
}
|
}
|
||||||
@ -117,7 +119,6 @@ struct PgCategory::PgCategoryPrivate {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
db::PgConnection & _db;
|
db::PgConnection & _db;
|
||||||
User * _owner;
|
|
||||||
|
|
||||||
int64_t _uid{};
|
int64_t _uid{};
|
||||||
Category * _parent{nullptr};
|
Category * _parent{nullptr};
|
||||||
@ -126,7 +127,7 @@ struct PgCategory::PgCategoryPrivate {
|
|||||||
std::string _description;
|
std::string _description;
|
||||||
};
|
};
|
||||||
|
|
||||||
PgCategory::PgCategory(db::PgConnection & db, User * user) : _priv{spimpl::make_impl< PgCategoryPrivate >(db, user, nullptr)} {
|
PgCategory::PgCategory(db::PgConnection & db) : _priv{spimpl::make_impl< PgCategoryPrivate >(db, nullptr)} {
|
||||||
_priv->_self = this;
|
_priv->_self = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -10,8 +10,7 @@ class PgCategoriesRepositoryTest : public DbTestBase< PgCategoriesRepositoryTest
|
|||||||
public:
|
public:
|
||||||
PgCategoriesRepositoryTest() : user{db()} {
|
PgCategoriesRepositoryTest() : user{db()} {
|
||||||
user._init();
|
user._init();
|
||||||
user._expect_call_uid();
|
sut = std::make_unique< eedb::PgCategoriesRepository >(db());
|
||||||
sut = std::make_unique< eedb::PgCategoriesRepository >(db(), &user);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|||||||
@ -8,8 +8,7 @@ class PgCategoryTest : public DbTestBase< PgCategoryTest > {
|
|||||||
public:
|
public:
|
||||||
PgCategoryTest() : user{db()} {
|
PgCategoryTest() : user{db()} {
|
||||||
user._init();
|
user._init();
|
||||||
user._expect_call_uid();
|
sut = std::make_unique< eedb::PgCategory >(db());
|
||||||
sut = std::make_unique< eedb::PgCategory >(db(), &user);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto insertCategory(std::string name, int64_t parentId) {
|
auto insertCategory(std::string name, int64_t parentId) {
|
||||||
|
|||||||
@ -16,7 +16,7 @@ class PgItemsRepositoryTest : public DbTestBase< PgItemsRepositoryTest > {
|
|||||||
public:
|
public:
|
||||||
PgItemsRepositoryTest() : user{db()}, category{db()}, numericParameter{db()}, textParameter{db()}, listParameter{db()} {
|
PgItemsRepositoryTest() : user{db()}, category{db()}, numericParameter{db()}, textParameter{db()}, listParameter{db()} {
|
||||||
using namespace testing;
|
using namespace testing;
|
||||||
user._init(); // create fake user
|
user._init();
|
||||||
category._init_simple("main");
|
category._init_simple("main");
|
||||||
numericParameter._init("numeric");
|
numericParameter._init("numeric");
|
||||||
textParameter._init("text");
|
textParameter._init("text");
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user