2019-06-30 03:37:18 +00:00
|
|
|
#include "headers/Park_time.h"
|
2019-05-28 16:46:55 +00:00
|
|
|
|
|
|
|
Park_time::Park_time(int c_id, int s_id)
|
2019-06-30 03:37:18 +00:00
|
|
|
: customer_id{c_id},
|
|
|
|
spot_id{s_id},
|
|
|
|
duration{0},
|
|
|
|
start{high_resolution_clock::now()},
|
|
|
|
id{auto_increment_db() + 1} {
|
2019-06-30 00:50:36 +00:00
|
|
|
save_db();
|
2019-05-28 16:46:55 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 03:37:18 +00:00
|
|
|
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_));
|
2019-06-30 02:22:58 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 03:37:18 +00:00
|
|
|
Park_time::~Park_time() { update_db(); }
|
|
|
|
|
|
|
|
void Park_time::clock_out(int c_id, int s_id) {
|
2019-05-28 16:46:55 +00:00
|
|
|
|
|
|
|
if (c_id != customer_id) {
|
2019-06-30 02:22:58 +00:00
|
|
|
cout << "wrong customer id, you are at the wrong location";
|
2019-05-28 16:46:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (s_id != spot_id) {
|
2019-06-30 02:22:58 +00:00
|
|
|
cout << "Wrong spot id, you're at the wrong location";
|
2019-05-28 16:46:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!duration) {
|
|
|
|
end = high_resolution_clock::now();
|
2019-06-30 03:37:18 +00:00
|
|
|
duration =
|
|
|
|
duration_cast<seconds>(end - start).count(); // use mins later
|
2019-06-30 00:50:36 +00:00
|
|
|
update_db();
|
2019-05-28 16:46:55 +00:00
|
|
|
|
|
|
|
} else {
|
2019-06-30 02:22:58 +00:00
|
|
|
cout << "Already clocked out. Something is wrong \n";
|
2019-05-28 16:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 03:37:18 +00:00
|
|
|
std::ostream& operator<<(std::ostream& os, const Park_time& pt) {
|
2019-05-28 16:46:55 +00:00
|
|
|
std::time_t start_ = system_clock::to_time_t(pt.start);
|
|
|
|
std::time_t end_ = system_clock::to_time_t(pt.end);
|
|
|
|
os << "- - - - - - - - - - - - - - - - - - - -\n";
|
|
|
|
os << "Clocked in :" << std::ctime(&start_);
|
|
|
|
os << "clocked out : " << std::ctime(&end_);
|
|
|
|
os << "duration : " << pt.duration << "\n";
|
|
|
|
os << "- - - - - - - - - - - - - - - - - - - -\n";
|
|
|
|
return os;
|
2019-06-29 18:23:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 03:37:18 +00:00
|
|
|
int Park_time::start_to_int() {
|
2019-06-29 18:23:38 +00:00
|
|
|
auto start_to_epoch = start.time_since_epoch();
|
2019-06-30 02:22:58 +00:00
|
|
|
auto start_value = duration_cast<seconds>(start_to_epoch);
|
2019-06-29 18:23:38 +00:00
|
|
|
int start_seconds = start_value.count();
|
2019-06-30 00:50:36 +00:00
|
|
|
return start_seconds;
|
|
|
|
}
|
|
|
|
|
2019-06-30 03:37:18 +00:00
|
|
|
// db funcs
|
|
|
|
// -----------------------------------------------------------------------------
|
2019-06-30 00:50:36 +00:00
|
|
|
|
|
|
|
void Park_time::save_db() {
|
|
|
|
string statement{"insert into Park_time values ( , , , , , );"};
|
|
|
|
statement.insert(41, "NULL");
|
|
|
|
statement.insert(39, "NULL");
|
2019-06-30 03:37:18 +00:00
|
|
|
statement.insert(37, to_string(start_to_int()));
|
2019-06-30 00:50:36 +00:00
|
|
|
statement.insert(35, to_string(spot_id));
|
|
|
|
statement.insert(33, to_string(customer_id));
|
|
|
|
statement.insert(31, to_string(id));
|
|
|
|
SQLite::Transaction transaction(data::db);
|
|
|
|
data::db.exec(statement);
|
|
|
|
transaction.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Park_time::update_db() {
|
2019-06-30 03:37:18 +00:00
|
|
|
string statement =
|
|
|
|
"UPDATE Park_time SET end = , duration = where id = '';";
|
2019-06-30 02:22:58 +00:00
|
|
|
statement.insert(53, to_string(id));
|
2019-06-30 00:50:36 +00:00
|
|
|
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;");
|
|
|
|
int id = 0;
|
|
|
|
max_id.executeStep();
|
|
|
|
id = max_id.getColumn(0);
|
|
|
|
max_id.reset();
|
|
|
|
return id;
|
2019-05-28 16:46:55 +00:00
|
|
|
}
|