refactor
This commit is contained in:
parent
f4ef592706
commit
8d85fca3a3
@ -1,5 +1,4 @@
|
||||
set(SOURCES
|
||||
DatabaseConnection.cpp
|
||||
main.cpp)
|
||||
|
||||
|
||||
|
||||
@ -1,17 +0,0 @@
|
||||
#include <memory>
|
||||
|
||||
namespace Wt {
|
||||
class WEnvironment;
|
||||
}
|
||||
|
||||
namespace eedb::db {
|
||||
class PgConnection;
|
||||
}
|
||||
|
||||
namespace eedb {
|
||||
|
||||
class DbConnection {
|
||||
public:
|
||||
std::unique_ptr< db::PgConnection > create(const Wt::WEnvironment & env) const;
|
||||
};
|
||||
}
|
||||
@ -1,11 +1,10 @@
|
||||
#include "DatabaseConnection.hpp"
|
||||
|
||||
#include <eedb/EEDB.hpp>
|
||||
#include <eedb/Session.hpp>
|
||||
|
||||
#include <eedb/auth/PgUserAuth.hpp>
|
||||
#include <eedb/auth/Services.hpp>
|
||||
|
||||
#include <eedb/db/config.hpp>
|
||||
#include <eedb/db/connection.hpp>
|
||||
|
||||
#include <eedb/widgets/AuthWidget.hpp>
|
||||
@ -21,19 +20,21 @@ class WContainerWidget;
|
||||
|
||||
Wt::WApplication * createApplication(const Wt::WEnvironment & env) {
|
||||
using std::move;
|
||||
auto dbConnection = eedb::DbConnection{}.create(env);
|
||||
using std::make_unique;
|
||||
|
||||
auto userDatabase = std::make_unique< eedb::auth::PgUserAuth >(*dbConnection, env);
|
||||
auto services = eedb::auth::Services();
|
||||
auto dbConfig = make_unique< eedb::db::PgConfig >(env);
|
||||
auto dbConnection = make_unique< eedb::db::PgConnection >(move(dbConfig));
|
||||
auto userDatabase = make_unique< eedb::auth::PgUserAuth >(*dbConnection, env);
|
||||
auto session = make_unique< eedb::Session >(move(dbConnection), env, move(userDatabase));
|
||||
auto theme = make_unique< eedb::BootstrapTheme >();
|
||||
|
||||
auto session = std::make_unique< eedb::Session >(move(dbConnection), env, move(userDatabase));
|
||||
auto theme = std::make_unique< eedb::BootstrapTheme >();
|
||||
|
||||
auto authPageFactory = [ services, _session = session.get() ](Wt::WContainerWidget * parent)->eedb::AuthPage * {
|
||||
return new eedb::AuthPage(services, *_session, parent);
|
||||
auto authPageFactory = [_session = session.get()]()->eedb::AuthPage * {
|
||||
auto services = eedb::auth::Services();
|
||||
return new eedb::AuthPage(services, *_session);
|
||||
};
|
||||
auto homePageFactory = [_session = session.get()](Wt::WContainerWidget * parent)->eedb::HomePage * {
|
||||
return nullptr; // new eedb::HomePage();
|
||||
|
||||
auto homePageFactory = [_session = session.get()]()->eedb::HomePage * {
|
||||
return new eedb::Home(*_session);
|
||||
};
|
||||
|
||||
return new eedb::EEDB{std::move(session), std::move(theme), authPageFactory, homePageFactory};
|
||||
|
||||
@ -5,7 +5,6 @@
|
||||
|
||||
#include <eedb/Session.hpp>
|
||||
#include <eedb/auth/PgUserAuth.hpp>
|
||||
#include <eedb/auth/Services.hpp>
|
||||
#include <eedb/db/connection.hpp>
|
||||
#include <eedb/widgets/AuthWidget.hpp>
|
||||
#include <eedb/widgets/HomePage.hpp>
|
||||
@ -41,34 +40,33 @@ EEDB::EEDB(std::unique_ptr< eedb::Session > session,
|
||||
_authPage(nullptr),
|
||||
_homePageFactory(move(homePageFactory)),
|
||||
_homePage(nullptr) {
|
||||
// _login.changed().connect(this, &EEDB::authEvent);
|
||||
// _userDatabase = std::make_unique< eedb::auth::PgUserAuth >(*_dbConnection, env);
|
||||
|
||||
root()->addStyleClass("container");
|
||||
useStyleSheet("/resources/style.css");
|
||||
|
||||
if(_theme)
|
||||
setTheme(_theme->create());
|
||||
|
||||
_authPage = _authPageFactory(root());
|
||||
// _authWidget = new eedb::AuthPage(eedb::auth::Services{}, *_userDatabase, _login);
|
||||
// root()->addWidget(_authWidget);
|
||||
_authPage = _authPageFactory();
|
||||
_authPage->onUserWeakLogin().connect([this](auto...) { this->authEventLogin(LoginState::weak); });
|
||||
_authPage->onUserStrongLogin().connect([this](auto...) { this->authEventLogin(LoginState::strong); });
|
||||
_authPage->onUserLogout().connect([this](auto...) { this->authEventLogout(); });
|
||||
|
||||
root()->addWidget(_authPage);
|
||||
}
|
||||
|
||||
void EEDB::authEvent() {
|
||||
void EEDB::authEventLogin(EEDB::LoginState state) {
|
||||
using namespace Wt;
|
||||
|
||||
// if(_login.loggedIn()) {
|
||||
// Wt::WContainerWidget * container = root();
|
||||
// setInternalPath("/app");
|
||||
// root()->removeStyleClass("container");
|
||||
// Wt::log("notice") << "Clearing root and creating widgets";
|
||||
Wt::log("notice") << "Clearing root and creating widgets";
|
||||
// // new eedb::Home(container, &_authWidget->login());
|
||||
}
|
||||
|
||||
// } else {
|
||||
void EEDB::authEventLogout() {
|
||||
// Wt::log("notice") << "User logged out.";
|
||||
// redirect(url());
|
||||
// quit();
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,17 +13,20 @@ class HomePage;
|
||||
class Session;
|
||||
class Theme;
|
||||
|
||||
using AuthPageFactory = std::function< AuthPage *(Wt::WContainerWidget *) >;
|
||||
using HomePageFactory = std::function< HomePage *(Wt::WContainerWidget *) >;
|
||||
using AuthPageFactory = std::function< AuthPage *() >;
|
||||
using HomePageFactory = std::function< HomePage *() >;
|
||||
|
||||
class EEDB : public Wt::WApplication {
|
||||
public:
|
||||
enum class LoginState { weak, strong };
|
||||
EEDB(std::unique_ptr< eedb::Session > session,
|
||||
std::unique_ptr< Theme > theme,
|
||||
AuthPageFactory authPageFactory,
|
||||
HomePageFactory homePageFactory);
|
||||
|
||||
void authEvent();
|
||||
void authEventLogin(LoginState state);
|
||||
|
||||
void authEventLogout();
|
||||
|
||||
private:
|
||||
std::unique_ptr< eedb::Session > _session;
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
set(SOURCE
|
||||
connection.cpp
|
||||
config.cpp
|
||||
)
|
||||
|
||||
find_package(Sqlpp11 REQUIRED)
|
||||
|
||||
@ -1,14 +1,10 @@
|
||||
#include "DatabaseConnection.hpp"
|
||||
#include <eedb/db/config.hpp>
|
||||
|
||||
#include <Wt/WEnvironment>
|
||||
#include <Wt/WServer>
|
||||
|
||||
#include <eedb/db/connection.hpp>
|
||||
|
||||
namespace eedb {
|
||||
std::unique_ptr< db::PgConnection > eedb::DbConnection::create(const Wt::WEnvironment & env) const {
|
||||
auto config = std::make_shared< sqlpp::postgresql::connection_config >();
|
||||
|
||||
namespace eedb::db {
|
||||
PgConfig::PgConfig(const Wt::WEnvironment & env) {
|
||||
auto readString = [&env](auto && name, auto & val, auto def) {
|
||||
if(!env.server()->readConfigurationProperty(name, val)) {
|
||||
val = def;
|
||||
@ -33,13 +29,11 @@ std::unique_ptr< db::PgConnection > eedb::DbConnection::create(const Wt::WEnviro
|
||||
}
|
||||
};
|
||||
|
||||
readString("db_host", config->host, "localhost");
|
||||
readString("db_user", config->user, "postgres");
|
||||
readString("db_password", config->password, "postgres");
|
||||
readString("db_name", config->dbname, "eedb");
|
||||
readInt("db_port", config->port, 5432);
|
||||
readBool("db_debug", config->debug, true);
|
||||
|
||||
return std::make_unique< db::PgConnection >(config);
|
||||
readString("db_host", host, "localhost");
|
||||
readString("db_user", user, "postgres");
|
||||
readString("db_password", password, "postgres");
|
||||
readString("db_name", dbname, "eedb");
|
||||
readInt("db_port", port, 5432);
|
||||
readBool("db_debug", debug, true);
|
||||
}
|
||||
}
|
||||
15
src/eedb/db/config.hpp
Normal file
15
src/eedb/db/config.hpp
Normal file
@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <sqlpp11/postgresql/connection_config.h>
|
||||
|
||||
namespace Wt {
|
||||
class WEnvironment;
|
||||
}
|
||||
|
||||
namespace eedb::db {
|
||||
|
||||
class PgConfig : public sqlpp::postgresql::connection_config {
|
||||
public:
|
||||
PgConfig(const Wt::WEnvironment & env);
|
||||
};
|
||||
}
|
||||
@ -1,8 +1,7 @@
|
||||
#include <eedb/db/config.hpp>
|
||||
#include <eedb/db/connection.hpp>
|
||||
|
||||
|
||||
namespace eedb::db {
|
||||
|
||||
|
||||
|
||||
PgConnection::PgConnection(std::shared_ptr<eedb::db::PgConfig> config) : _conn(std::make_unique< sqlpp::postgresql::connection >(config)) {}
|
||||
}
|
||||
|
||||
@ -5,11 +5,11 @@
|
||||
#include <sqlpp11/postgresql/postgresql.h>
|
||||
|
||||
namespace eedb::db {
|
||||
class PgConfig;
|
||||
|
||||
class PgConnection {
|
||||
public:
|
||||
PgConnection(std::shared_ptr< sqlpp::postgresql::connection_config > config)
|
||||
: _conn(std::make_unique< sqlpp::postgresql::connection >(config)) {}
|
||||
PgConnection(std::shared_ptr< eedb::db::PgConfig > config);
|
||||
|
||||
sqlpp::postgresql::connection * native() {
|
||||
return _conn.get();
|
||||
|
||||
@ -3,11 +3,24 @@
|
||||
#include <eedb/widgets/AuthWidget.hpp>
|
||||
|
||||
namespace eedb {
|
||||
AuthPage::AuthPage(const auth::Services & baseAuth, Session & session, Wt::WContainerWidget * parent)
|
||||
: Wt::Auth::AuthWidget(*baseAuth.authService(), session.userDatabase(), session.login(), parent) {
|
||||
AuthPage::AuthPage(const auth::Services & baseAuth, Session & session)
|
||||
: Wt::Auth::AuthWidget(*baseAuth.authService(), session.userDatabase(), session.login(), nullptr) {
|
||||
this->model()->addPasswordAuth(eedb::auth::Services::passwordService());
|
||||
this->model()->addOAuth(eedb::auth::Services::oAuthServices());
|
||||
this->setRegistrationEnabled(true);
|
||||
|
||||
login().changed().connect([this](auto...) {
|
||||
if(this->login().state() == Wt::Auth::LoginState::StrongLogin) {
|
||||
_onUserStrongLogin();
|
||||
} else if(this->login().state() == Wt::Auth::LoginState::WeakLogin) {
|
||||
_onUserWeakLogin();
|
||||
} else if(this->login().state() == Wt::Auth::LoginState::DisabledLogin) {
|
||||
_onNeedEmailVerification();
|
||||
} else {
|
||||
_onUserLogout();
|
||||
}
|
||||
});
|
||||
|
||||
this->processEnvironment();
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,7 +25,28 @@ class AuthPage : public Wt::Auth::AuthWidget {
|
||||
using _base = Wt::Auth::AuthWidget;
|
||||
|
||||
public:
|
||||
AuthPage(const eedb::auth::Services & baseAuth, eedb::Session & session, Wt::WContainerWidget * parent = nullptr);
|
||||
|
||||
AuthPage(const eedb::auth::Services & baseAuth, Session & session);
|
||||
|
||||
Wt::Signal<> & needVerification() {
|
||||
return _onNeedEmailVerification;
|
||||
}
|
||||
|
||||
Wt::Signal<> & onUserWeakLogin() {
|
||||
return _onUserWeakLogin;
|
||||
}
|
||||
|
||||
Wt::Signal<> & onUserStrongLogin() {
|
||||
return _onUserStrongLogin;
|
||||
}
|
||||
|
||||
Wt::Signal<> & onUserLogout() {
|
||||
return _onUserLogout;
|
||||
}
|
||||
|
||||
private:
|
||||
Wt::Signal<> _onNeedEmailVerification;
|
||||
Wt::Signal<> _onUserWeakLogin;
|
||||
Wt::Signal<> _onUserStrongLogin;
|
||||
Wt::Signal<> _onUserLogout;
|
||||
};
|
||||
}
|
||||
|
||||
@ -39,7 +39,9 @@ auto make_tree() {
|
||||
return tree;
|
||||
}
|
||||
|
||||
eedb::Home::Home(Wt::WContainerWidget * container, Wt::Auth::Login * login) : _login(login) {
|
||||
eedb::Home::Home(eedb::Session & session) : _session(session) {}
|
||||
|
||||
void Home::show(Wt::WContainerWidget * container) {
|
||||
// Create a navigation bar with a link to a web page.
|
||||
auto navigation = new Wt::WNavigationBar();
|
||||
navigation->setTitle("eedb", "http://eedb.pl");
|
||||
@ -90,19 +92,13 @@ eedb::Home::Home(Wt::WContainerWidget * container, Wt::Auth::Login * login) : _l
|
||||
Wt::WStackedWidget * contents = new Wt::WStackedWidget();
|
||||
|
||||
_categoryTree = make_tree();
|
||||
|
||||
_categoryTree->clicked().connect([=](auto...) {
|
||||
Wt::log("notice") << "Item selection changed";
|
||||
});
|
||||
|
||||
_categoryTree->mouseWentDown().connect([=](auto...) {
|
||||
Wt::log("notice") << "Mouse went down";
|
||||
});
|
||||
|
||||
_categoryTree->mouseWentUp().connect([=](auto...) {
|
||||
Wt::log("notice") << "Mouse went up";
|
||||
});
|
||||
|
||||
|
||||
_categoryTree->clicked().connect([=](auto...) { Wt::log("notice") << "Item selection changed"; });
|
||||
|
||||
_categoryTree->mouseWentDown().connect([=](auto...) { Wt::log("notice") << "Mouse went down"; });
|
||||
|
||||
_categoryTree->mouseWentUp().connect([=](auto...) { Wt::log("notice") << "Mouse went up"; });
|
||||
|
||||
hbox->addWidget(_categoryTree, 1);
|
||||
hbox->addWidget(contents, 2);
|
||||
hbox->setResizable(0, true);
|
||||
|
||||
@ -6,10 +6,15 @@ namespace Wt {
|
||||
class WContainerWidget;
|
||||
class WTreeView;
|
||||
}
|
||||
|
||||
namespace Wt::Auth {
|
||||
class Login;
|
||||
}
|
||||
|
||||
namespace eedb {
|
||||
class Session;
|
||||
}
|
||||
|
||||
namespace eedb {
|
||||
|
||||
class HomePage {
|
||||
@ -18,11 +23,12 @@ class HomePage {
|
||||
|
||||
class Home final : public HomePage {
|
||||
public:
|
||||
Home(Wt::WContainerWidget * container, Wt::Auth::Login * login);
|
||||
Home(Session & session);
|
||||
|
||||
void show(Wt::WContainerWidget * parent) override {}
|
||||
void show(Wt::WContainerWidget * parent) override;
|
||||
|
||||
private:
|
||||
Session & _session;
|
||||
const Wt::Auth::Login * _login;
|
||||
Wt::WTreeView * _categoryTree;
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user