From 0eee67d3d94dd4bdc77a9d90268b62b403bd3312 Mon Sep 17 00:00:00 2001 From: TinyAtoms Date: Sat, 29 Jun 2019 21:50:36 -0300 Subject: [PATCH] park time can be saved to db now --- CMakeLists.txt | 4 +-- old/Customer.cpp => Customer.cpp | 28 ++++++++-------- old/Park_spot.cpp => Park_spot.cpp | 0 old/Park_time.cpp => Park_time.cpp | 47 ++++++++++++++++++++++++--- data.cpp | 2 +- {old/headers => headers}/Customer.h | 11 ++++--- {old/headers => headers}/Park_spot.h | 0 {old/headers => headers}/Park_time.h | 13 ++++++-- headers/data.h | 1 - main.cpp | 13 ++++---- test.db3 | Bin 16384 -> 16384 bytes 11 files changed, 82 insertions(+), 37 deletions(-) rename old/Customer.cpp => Customer.cpp (75%) rename old/Park_spot.cpp => Park_spot.cpp (100%) rename old/Park_time.cpp => Park_time.cpp (51%) rename {old/headers => headers}/Customer.h (83%) rename {old/headers => headers}/Park_spot.h (100%) rename {old/headers => headers}/Park_time.h (78%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 62ade13..7f77bd2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,12 +13,12 @@ add_executable(park main.cpp data.cpp headers/data.h - #[[Customer.cpp + Customer.cpp headers/Customer.h Park_spot.cpp headers/Park_spot.h Park_time.cpp - headers/Park_time.h]] + headers/Park_time.h ) diff --git a/old/Customer.cpp b/Customer.cpp similarity index 75% rename from old/Customer.cpp rename to Customer.cpp index 67de740..b88e7df 100644 --- a/old/Customer.cpp +++ b/Customer.cpp @@ -2,12 +2,12 @@ #include // constructors -Customer::Customer(string name_, Verhicle_type verhicle_, SQLite::Database& db): +Customer::Customer(string name_, Verhicle_type verhicle_): name{name_}, verhicle{verhicle_}, card_code{gen_cardcode()} { - id = auto_increment_db(db) + 1; - save_db(db); + id = auto_increment_db() + 1; + save_db(); } Customer::Customer(int id_, string name_, string card_code_, Verhicle_type verhicle_, vector instances) @@ -47,38 +47,38 @@ void Customer::gen_monthly() { //================================================================================================ // functions that interact with the database -void Customer::save_db(SQLite::Database& database) { +void Customer::save_db() { string statement{"insert into Customer values (, '', '', );"}; // after ( = 28) statement.insert(38, std::to_string(int(verhicle))); statement.insert(36, card_code); statement.insert(32, name); statement.insert(29, "null"); - SQLite::Transaction transaction(database); - database.exec(statement); + SQLite::Transaction transaction(data::db); + data::db.exec(statement); transaction.commit(); } -void Customer::update_db(SQLite::Database& database) { - string statement = "UPDATE Customer SET name = \"\", card_code = \"\" where id = '';"; +void Customer::update_db() { + string statement = "UPDATE Customer SET name = '', card_code = '' where id = '';"; statement.insert(58, std::to_string(id)); statement.insert(44, card_code); statement.insert(28, name); // std::cout << statement; TODO: set some logging here - database.exec(statement); + data::db.exec(statement); } -void Customer::delete_db(SQLite::Database& database) { +void Customer::delete_db() { string statement = "delete from Customer where id= ;"; statement.insert(statement.length() - 2, std::to_string(id)); // std::cout << statement; - SQLite::Transaction transaction(database); - database.exec(statement); + SQLite::Transaction transaction(data::db); + data::db.exec(statement); transaction.commit(); } -int Customer::auto_increment_db(SQLite::Database& database) { - SQLite::Statement max_id(database, "select max(id) from Customer;"); +int Customer::auto_increment_db() { + SQLite::Statement max_id(data::db, "select max(id) from Customer;"); int id = 0; max_id.executeStep(); id = max_id.getColumn(0); diff --git a/old/Park_spot.cpp b/Park_spot.cpp similarity index 100% rename from old/Park_spot.cpp rename to Park_spot.cpp diff --git a/old/Park_time.cpp b/Park_time.cpp similarity index 51% rename from old/Park_time.cpp rename to Park_time.cpp index 26f319b..e5cd542 100644 --- a/old/Park_time.cpp +++ b/Park_time.cpp @@ -8,7 +8,9 @@ Park_time::Park_time(int c_id, int s_id) , spot_id { s_id } , duration { 0 } , start { high_resolution_clock::now() } + , id {auto_increment_db() + 1} { + save_db(); } void Park_time::clock_out(int c_id, int s_id) @@ -26,6 +28,7 @@ void Park_time::clock_out(int c_id, int s_id) if (!duration) { end = high_resolution_clock::now(); duration = duration_cast(end - start).count(); // use mins later + update_db(); } else { std::cout << "Already clocked out. Something is wrong \n"; @@ -44,13 +47,47 @@ std::ostream& operator<<(std::ostream& os, const Park_time & pt){ return os; } -void Park_time::debug() { + +int Park_time::start_to_int(){ auto start_to_epoch = start.time_since_epoch(); auto start_value = std::chrono::duration_cast(start_to_epoch); int start_seconds = start_value.count(); + return start_seconds; +} - auto end_to_epoch = end.time_since_epoch(); - auto end_value = std::chrono::duration_cast(start_to_epoch); - int end_seconds = end_value.count(); - std::cout << "<" << start_seconds << "-" << end_seconds << ">" ; +// 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(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() { + string statement = "UPDATE Park_time SET end = , duration = where id = '';"; + statement.insert(53, std::to_string(id)); + statement.insert(40, to_string(duration)); + statement.insert(27, to_string(start_to_int() + duration)); + std::cout << statement; // TODO: set some logging here + 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; } \ No newline at end of file diff --git a/data.cpp b/data.cpp index f8d1fa3..42e09a7 100644 --- a/data.cpp +++ b/data.cpp @@ -7,7 +7,7 @@ SQLite::Database start_db() { db.exec("create table if not exists Customer (id integer primary key, name text, card_code varchar(20), verhicle int)"); db.exec("create table if not exists Park_spot (id integer primary key, taken boolean, customer_id int)"); - db.exec("create table if not exists Park_time (id integer primary key, customer_id int, spot_id int, start real, end real, duration real)"); + db.exec("create table if not exists Park_time (id integer primary key, customer_id int, spot_id int, start int, end int, duration int)"); return db; } diff --git a/old/headers/Customer.h b/headers/Customer.h similarity index 83% rename from old/headers/Customer.h rename to headers/Customer.h index f235ed9..c2a112a 100644 --- a/old/headers/Customer.h +++ b/headers/Customer.h @@ -7,6 +7,7 @@ #include #include #include +#include "data.h" using std::string; using std::vector; @@ -28,7 +29,7 @@ clock in en out creeert en compleet een park_time object. Voegt het toe aan een class Customer { public: - Customer(string name_, Verhicle_type verhicle_, SQLite::Database& db); + Customer(string name_, Verhicle_type verhicle_); Customer(int id_, string name_, string card_code_, Verhicle_type verhicle_, vector instances); // needed to construct from db // potentially: add a destructor that calls update_db() before being destroyed @@ -39,8 +40,8 @@ class Customer { void clock_in(int s_id); void clock_out(int s_id); - void update_db(SQLite::Database& database); - void delete_db(SQLite::Database& database); + void update_db(); + void delete_db(); // void gen_weekly(); TODO: this void gen_monthly(); @@ -51,8 +52,8 @@ class Customer { string gen_cardcode(); - void save_db(SQLite::Database& database); - int auto_increment_db(SQLite::Database& database); + void save_db(); + int auto_increment_db(); }; #endif // CUSTOMER_H \ No newline at end of file diff --git a/old/headers/Park_spot.h b/headers/Park_spot.h similarity index 100% rename from old/headers/Park_spot.h rename to headers/Park_spot.h diff --git a/old/headers/Park_time.h b/headers/Park_time.h similarity index 78% rename from old/headers/Park_time.h rename to headers/Park_time.h index a98d9c8..130e144 100644 --- a/old/headers/Park_time.h +++ b/headers/Park_time.h @@ -4,10 +4,13 @@ #include #include +#include +#include "data.h" using namespace std::chrono; - +using std::string; +using std::to_string; /* db repr of Park_time int id (not null, auto increment) @@ -28,14 +31,18 @@ public: int spot_id; int duration; Park_time(int c_id, int s_id); + // Park_time(int c_id, int s_id ); void clock_out(int c_id, int s_id); friend std::ostream& operator<<(std::ostream& os, const Park_time & pt); - void debug(); private: high_resolution_clock::time_point start; high_resolution_clock::time_point end; - //TODO: discuss pros cons of using chrono, ctime, or 3th party lib + void save_db(); + void update_db(); + int auto_increment_db(); // helper + int start_to_int(); // helper + }; diff --git a/headers/data.h b/headers/data.h index 1f7aff5..e28404b 100644 --- a/headers/data.h +++ b/headers/data.h @@ -6,7 +6,6 @@ namespace data { SQLite::Database start_db(); static SQLite::Database db = start_db(); - } #endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index 6df10b8..4a5b372 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include "thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h" #include "headers/data.h" +#include "headers/Park_spot.h" #include #include #include @@ -23,12 +24,12 @@ De client clockt in en uit bij een spot. using std::cout; int main() { - SQLite::Statement test(data::db, "SELECT * FROM Customer WHERE id > 2"); - while (test.executeStep()){ - int id = test.getColumn(0); - std::string name = test.getColumn(1); - cout << id << ", " << name << std::endl; + class Customer sagar{"nonsense", Verhicle_type::medium}; + sagar.update_db(); - } + Park_spot p1{1}; + p1.clock(&sagar); + p1.clock(&sagar); + sagar.gen_monthly(); } diff --git a/test.db3 b/test.db3 index 10a2d99dfe155a1442be647bb5adf13bdfde3cb1..c44648e656df65c1ee69b8f02d9ce8fe8d1c3da0 100644 GIT binary patch delta 462 zcmZo@U~Fh$oFL68Fj2-?Sb#y#M1hxqfq|K?iGjbFUzo3Hv!H?iA6FwEGrPFFJY!?_ zh;5*vz|$XcJQvb^Mw;DkV* z_`rZV@7m-7CM8xBHO{#)eqKTTZef0jLB*l9k&#uQ*-T0-C~8t`J(B8!Dr!soL{Z}%pX8E|=p0v`wg|5D32vpf@LQ+ PBRc~pBM>k#v#sZc1)j^C@t?~~=qvG8oY_+-a_S1Xpez z3g^E*f91Hs3r+whfD^z8-~@02I02jhP5>u>6Tk_4`x1Egy(?$8Zr?6Gj8kRq={*Iw zQfm883sZuYkPd>iON0x=7N{*Mam1+$|+h zm_Dtf3P?*pSG>C=JkYF2^eZ#_)FAoWB{^}v6imL~u zc^G=S5`1Zd7n|bQt=C>FK5Sg}SA9JGC+m~vdV8|k`&;NA(+jH-nJOA;`&#oT+XFj8E-%Wwf z7$@+5p1_UY7w)|Ipx9UZL;*^W*nw$!R-m_afr_VNcN6XyDPElv3fKSo=Bv-S#IjRg z3E-{|ktUYD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