Parkmanne/Park_spot.cpp

77 lines
2.0 KiB
C++
Raw Normal View History

2019-05-28 16:46:55 +00:00
#include "headers/Park_spot.h"
2019-06-30 01:48:49 +00:00
// constructors
Park_spot::Park_spot()
: parked{nullptr}, id{auto_increment_db() + 1}, taken{false} {
2019-06-30 01:30:47 +00:00
save_db();
2019-05-28 16:46:55 +00:00
}
2019-06-30 08:32:01 +00:00
Park_spot::Park_spot(Customer* parked_, int id_, bool taken_)
: parked{nullptr},
id{id_},
taken{taken_} // TODO: think about how init parked?
{}
2019-05-28 16:46:55 +00:00
Park_spot::~Park_spot() { update_db(); }
2019-06-30 01:48:49 +00:00
// clock in en out, calls de juist(in/out) van de customer aan de hand van
// internal state van taken
void Park_spot::clock(Customer* c_customer) {
if (!taken) {
2019-05-28 16:46:55 +00:00
parked = c_customer;
taken = true;
parked->clock_in(id);
2019-06-30 01:30:47 +00:00
update_db();
} else {
2019-05-28 16:46:55 +00:00
taken = false;
parked->clock_out(id);
parked = nullptr;
2019-06-30 01:30:47 +00:00
update_db();
2019-05-28 16:46:55 +00:00
}
2019-06-30 01:30:47 +00:00
}
// --------------------- db functs
void Park_spot::update_db() {
string statement =
"UPDATE Park_spot SET taken = '', customer_id = '' where id = '';";
2019-06-30 01:30:47 +00:00
statement.insert(63, to_string(id));
if (taken) {
2019-06-30 01:30:47 +00:00
statement.insert(49, to_string(parked->id));
statement.insert(30, "true");
} else {
2019-06-30 01:30:47 +00:00
statement.insert(49, "NULL");
statement.insert(30, "false");
}
data::db.exec(statement);
}
void Park_spot::save_db() {
//(int id, bool taken, int customer_id)
string statement{"insert into Park_spot values ( , , );"};
// after ( = 28)
statement.insert(34, "NULL");
statement.insert(32, "false");
statement.insert(30, to_string(id));
SQLite::Transaction transaction(data::db);
data::db.exec(statement);
transaction.commit();
}
void Park_spot::delete_db() {
string statement = "delete from Park_spot where id= ;";
statement.insert(statement.length() - 2, to_string(id));
2019-06-30 01:30:47 +00:00
SQLite::Transaction transaction(data::db);
data::db.exec(statement);
transaction.commit();
}
int Park_spot::auto_increment_db() {
SQLite::Statement max_id(data::db, "select max(id) from Park_spot;");
int id = 0;
max_id.executeStep();
id = max_id.getColumn(0);
max_id.reset();
return id;
2019-05-28 16:46:55 +00:00
}