Added constructors to construct from db info

This is in no way finished. CHECK TODOs!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
This commit is contained in:
TinyAtoms
2019-06-30 00:37:18 -03:00
parent 085cd5af08
commit bf17b82c2e
10 changed files with 136 additions and 124 deletions

View File

@ -1,22 +1,27 @@
#include"headers/Park_time.h"
#include "headers/Park_time.h"
Park_time::Park_time(int c_id, int s_id)
: customer_id { c_id }
, spot_id { s_id }
, duration { 0 }
, start { high_resolution_clock::now() }
, id {auto_increment_db() + 1}
{
: customer_id{c_id},
spot_id{s_id},
duration{0},
start{high_resolution_clock::now()},
id{auto_increment_db() + 1} {
save_db();
}
Park_time::~Park_time(){
update_db();
Park_time::Park_time(int id_, int customer_id_, int spot_id_, int start_,
int duration_)
: id{id_},
customer_id{customer_id_},
spot_id{spot_id_},
duration{duration_} {
start = time_point<system_clock>(seconds(start_));
end = time_point<system_clock>(seconds(start_ + duration_));
}
void Park_time::clock_out(int c_id, int s_id)
{
Park_time::~Park_time() { update_db(); }
void Park_time::clock_out(int c_id, int s_id) {
if (c_id != customer_id) {
cout << "wrong customer id, you are at the wrong location";
@ -29,7 +34,8 @@ void Park_time::clock_out(int c_id, int s_id)
if (!duration) {
end = high_resolution_clock::now();
duration = duration_cast<seconds>(end - start).count(); // use mins later
duration =
duration_cast<seconds>(end - start).count(); // use mins later
update_db();
} else {
@ -37,8 +43,7 @@ void Park_time::clock_out(int c_id, int s_id)
}
}
std::ostream& operator<<(std::ostream& os, const Park_time & pt){
std::ostream& operator<<(std::ostream& os, const Park_time& pt) {
std::time_t start_ = system_clock::to_time_t(pt.start);
std::time_t end_ = system_clock::to_time_t(pt.end);
os << "- - - - - - - - - - - - - - - - - - - -\n";
@ -49,23 +54,21 @@ std::ostream& operator<<(std::ostream& os, const Park_time & pt){
return os;
}
int Park_time::start_to_int(){
int Park_time::start_to_int() {
auto start_to_epoch = start.time_since_epoch();
auto start_value = duration_cast<seconds>(start_to_epoch);
int start_seconds = start_value.count();
return start_seconds;
}
// db funcs -----------------------------------------------------------------------------
// db funcs
// -----------------------------------------------------------------------------
void Park_time::save_db() {
string statement{"insert into Park_time values ( , , , , , );"};
statement.insert(41, "NULL");
statement.insert(39, "NULL");
statement.insert(37, to_string(start_to_int()) );
statement.insert(37, to_string(start_to_int()));
statement.insert(35, to_string(spot_id));
statement.insert(33, to_string(customer_id));
statement.insert(31, to_string(id));
@ -75,14 +78,14 @@ void Park_time::save_db() {
}
void Park_time::update_db() {
string statement = "UPDATE Park_time SET end = , duration = where id = '';";
string statement =
"UPDATE Park_time SET end = , duration = where id = '';";
statement.insert(53, to_string(id));
statement.insert(40, to_string(duration));
statement.insert(27, to_string(start_to_int() + duration));
data::db.exec(statement);
}
// to get id on first save to db
int Park_time::auto_increment_db() {
SQLite::Statement max_id(data::db, "select max(id) from Park_time;");