Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

Commit

Permalink
Port projects to the new way of construction as well (lib)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBriza committed Mar 30, 2020
1 parent 9bf8bc9 commit b38cae0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 93 deletions.
89 changes: 1 addition & 88 deletions src/database/database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ error Database::loadUsersRelatedData(locked<User> &user) {
return err;
}

err = loadProjects(user->ID(), user->GetRelatedData()->Projects);
err = loadModels(user->ID(), user->GetRelatedData()->Projects);
if (err != noError) {
return err;
}
Expand Down Expand Up @@ -1259,93 +1259,6 @@ error Database::LoadUserByID(
return noError;
}

error Database::loadProjects(const Poco::UInt64 &UID,
ProtectedContainer<Project> &list) {

if (!UID) {
return error("Cannot load user projects without an user ID");
}

try {
list.clear();

Poco::Mutex::ScopedLock lock(session_m_);

Poco::Data::Statement select(*session_);
select <<
"SELECT projects.local_id, projects.id, projects.uid, "
"projects.name, projects.guid, projects.wid, projects.color, projects.cid, "
"projects.active, projects.billable, projects.client_guid, "
"clients.name as client_name "
"FROM projects "
"LEFT JOIN clients on projects.cid = clients.id "
"LEFT JOIN workspaces on projects.wid = workspaces.id "
"WHERE projects.uid = :uid "
"ORDER BY workspaces.name COLLATE NOCASE ASC,"
"client_name COLLATE NOCASE ASC,"
"projects.name COLLATE NOCASE ASC;",
useRef(UID);
error err = last_error("loadProjects");
if (err != noError) {
return err;
}
Poco::Data::RecordSet rs(select);
while (!select.done()) {
select.execute();
bool more = rs.moveFirst();
while (more) {
auto model = list.create();
model->SetLocalID(rs[0].convert<Poco::Int64>());
if (rs[1].isEmpty()) {
model->SetID(0);
} else {
model->SetID(rs[1].convert<Poco::UInt64>());
}
model->SetUID(rs[2].convert<Poco::UInt64>());
model->SetName(rs[3].convert<std::string>());
if (rs[4].isEmpty()) {
model->SetGUID("");
} else {
model->SetGUID(rs[4].convert<std::string>());
}
model->SetWID(rs[5].convert<Poco::UInt64>());
if (rs[6].isEmpty()) {
model->SetColor("");
} else {
model->SetColor(rs[6].convert<std::string>());
}
if (rs[7].isEmpty()) {
model->SetCID(0);
} else {
model->SetCID(rs[7].convert<Poco::UInt64>());
}
model->SetActive(rs[8].convert<bool>());
model->SetBillable(rs[9].convert<bool>());
if (rs[10].isEmpty()) {
model->SetClientGUID("");
} else {
model->SetClientGUID(rs[10].convert<std::string>());
}
if (rs[11].isEmpty()) {
model->SetClientName("");
} else {
model->SetClientName(rs[11].convert<std::string>());
}
model->ClearDirty();

more = rs.moveNext();
}
}
} catch(const Poco::Exception& exc) {
return exc.displayText();
} catch(const std::exception& ex) {
return ex.what();
} catch(const std::string & ex) {
return ex;
}
return last_error("loadProjects");
}

template <typename T>
error Database::saveRelatedModels(const Poco::UInt64 UID,
const std::string &table_name,
Expand Down
4 changes: 0 additions & 4 deletions src/database/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,6 @@ class TOGGL_INTERNAL_EXPORT Database {
const Poco::UInt64 &UID,
ProtectedContainer<T> &list);

error loadProjects(
const Poco::UInt64 &UID,
ProtectedContainer<Project> &list);

template <typename T>
error saveRelatedModels(
const Poco::UInt64 UID,
Expand Down
47 changes: 46 additions & 1 deletion src/model/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,52 @@
namespace toggl {

class TOGGL_INTERNAL_EXPORT Project : public BaseModel {
public:
inline static const std::string modelName{ kModelProject };
inline static const Query query{
Query::Table{"projects"},
Query::Columns {
{ "name", true },
{ "wid", true },
{ "color", false },
{ "cid", false },
{ "active", true },
{ "billable", true },
{ "client_guid", false },
{ "clients.name", false }
},
Query::Join{
"LEFT JOIN clients ON projects.cid = clients.id",
"LEFT JOIN workspaces ON projects.wid = workspaces.id"
},
Query::OrderBy{
"workspaces.name COLLATE NOCASE ASC",
"clients.name COLLATE NOCASE ASC",
"projects.name COLLATE NOCASE ASC"
},
&BaseModel::query
};
Project(ProtectedBase *container, Poco::Data::RecordSet &rs)
: BaseModel(container, rs)
{
size_t ptr{ query.Offset() };
load(rs, query.IsRequired(ptr), ptr, name_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, wid_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, color_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, cid_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, active_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, billable_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, client_guid_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, client_name_);
ptr++;
ClearDirty();
}
Project(ProtectedBase *container)
: BaseModel(container)
{}
Expand Down

0 comments on commit b38cae0

Please sign in to comment.