Parkmanne/Park_spot.cpp

77 lines
2.1 KiB
C++
Raw Permalink Normal View History

2019-07-02 00:18:52 +00:00
#include "headers/Park_spot.h"
// constructors
2019-07-06 15:14:18 +00:00
Park_spot::Park_spot(Vehicle_type v_type_)
: parked_customer{0}, id{auto_increment_db() + 1}, taken{false}, v_type{v_type_} {
2019-07-02 00:18:52 +00:00
save_db();
}
2019-07-08 20:57:09 +00:00
Park_spot::Park_spot(int id_, bool taken_, int parked, Vehicle_type v_type_)
2019-07-02 00:51:23 +00:00
: parked_customer{parked},
2019-07-02 00:18:52 +00:00
id{id_},
2019-07-06 15:14:18 +00:00
v_type{v_type_},
2019-07-02 00:18:52 +00:00
taken{taken_} // TODO: think about how init parked?
{}
// 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) {
parked_customer = c_customer.id;
taken = true;
c_customer.clock_in(id);
update_db();
} else {
taken = false;
c_customer.clock_out(id);
parked_customer = 0;
update_db();
}
}
// --------------------- db functs
void Park_spot::update_db() {
2019-07-08 20:57:09 +00:00
string statement = "UPDATE Park_spot SET taken = '', customer_id = '' where id = '';";
2019-07-02 00:18:52 +00:00
statement.insert(63, to_string(id));
if (taken) {
statement.insert(49, to_string(parked_customer));
statement.insert(30, "1");
} else {
statement.insert(49, "NULL");
statement.insert(30, "0");
}
data::db.exec(statement);
}
void Park_spot::save_db() {
//(int id, bool taken, int customer_id)
2019-07-06 15:14:18 +00:00
string statement{"insert into Park_spot values ( , , , );"};
2019-07-02 00:18:52 +00:00
// after ( = 28)
2019-07-06 15:14:18 +00:00
statement.insert(36, to_string(int(v_type)));
2019-07-02 00:18:52 +00:00
statement.insert(34, "NULL");
statement.insert(32, "0");
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));
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;
}