From 3c7d7121e24caf73b92cffe3f885de7b99646db7 Mon Sep 17 00:00:00 2001 From: MassiveAtoms Date: Tue, 9 Jul 2019 09:17:33 -0300 Subject: [PATCH] added admin --- Admin.cpp | 36 ++++++++++++++++++++++++++++++++++++ CMakeLists.txt | 2 ++ Interface.cpp | 35 ++++++++++++++++++++++++++++++++++- Query.cpp | 19 ++++++++++++++++--- data.cpp | 7 ++++--- headers/Admin.h | 21 +++++++++++++++++++++ headers/Customer.h | 1 - headers/Interface.h | 3 ++- headers/Query.h | 11 ++++------- main.cpp | 7 ++++--- test.db3 | Bin 16384 -> 24576 bytes 11 files changed, 123 insertions(+), 19 deletions(-) create mode 100644 Admin.cpp create mode 100644 headers/Admin.h diff --git a/Admin.cpp b/Admin.cpp new file mode 100644 index 0000000..f3d625d --- /dev/null +++ b/Admin.cpp @@ -0,0 +1,36 @@ +#include "headers/Admin.h" +using std::to_string; + +Admin::Admin(string name_, string password_) + : id{auto_increment_db() + 1}, name{name_}, password{hash_password(password_)} { + save_db(); +} + +Admin::Admin(int id_, string name_, string password_) : id{id_}, name{name_}, password{password_} {} + +void Admin::save_db() { + string statement{"insert into Admin values (, '', '');"}; + statement.insert(33, password); + statement.insert(29, name); + statement.insert(26, to_string(id)); + SQLite::Transaction transaction(data::db); + data::db.exec(statement); + transaction.commit(); +} + +void Admin::update_db() { + string statement = "UPDATE Admin SET name = '', password = '' where id = '';"; + statement.insert(54, to_string(id)); + statement.insert(40, password); + statement.insert(25, name); + data::db.exec(statement); +} + +int Admin::auto_increment_db() { + SQLite::Statement max_id(data::db, "select max(id) from Admin;"); + int id = 0; + max_id.executeStep(); + id = max_id.getColumn(0); + max_id.reset(); + return id; +} diff --git a/CMakeLists.txt b/CMakeLists.txt index 97671bf..b5f4121 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,8 @@ add_executable(park headers/Park_spot.h Park_time.cpp headers/Park_time.h + Admin.cpp + headers/Admin.h Query.cpp headers/Query.h Interface.cpp diff --git a/Interface.cpp b/Interface.cpp index 3aec372..0b15bf2 100644 --- a/Interface.cpp +++ b/Interface.cpp @@ -1,5 +1,6 @@ #include "headers/Interface.h" + // I added it to pass spots, because the parking options need it to check where // is free parking_spots is declared in main, and if i declare it // liberal use of @@ -61,6 +62,21 @@ void interface_member(vector& spots) { } void interface_admin(vector& spots) { + int id; + string password; + cout << "\nPlease input id:"; + cin >> id; + cin.ignore(10000, '\n'); + cin.clear(); + Admin admin = query_admin_with_id(id); + cout << "\nPlease input password:"; + std::getline(cin, password); + while (!(verify_password(admin.password, password))) { + cout << "ERROR: wrong password. Please retype your password:\n"; + std::getline(cin, password); + } + + cout << "Logged in succesfully\n"; cout << "Welcome to the admin interface. It is not completely ready yet.\n"; cout << "[1] See monthly report of ALL parking spots\n"; cout << "[2] See weekly report of ALL parking spots\n"; @@ -69,7 +85,8 @@ void interface_admin(vector& spots) { cout << "[5] See current status of parking spots\n"; cout << "[6] Make new customer\n"; cout << "[7] Make new parking spot\n"; - cout << "option[1-7]:"; + cout << "[8] Make new Admin\n"; + cout << "option[1-8]:"; int option; cin >> option; cin.ignore(10000, '\n'); @@ -110,6 +127,10 @@ void interface_admin(vector& spots) { new_parkspot(spots); break; } + case 8: { + new_admin(); + break; + } default: break; @@ -174,6 +195,18 @@ void new_customer() { newcustomer.update_db(); } +void new_admin() { + string name; + string password; + cout << "What's the name of the admin? "; + std::getline(cin, name); + cout << "What's the password?"; + std::getline(cin, password); + Admin newadmin{name, password}; + cout << "New admin sucessfully created\n"; + newadmin.update_db(); +} + void new_parkspot(vector& spots) { cout << "What type of parking spot? [1] twoweeler, [2] fourweeler: "; int vtype; diff --git a/Query.cpp b/Query.cpp index 8fcc5b0..13d1656 100644 --- a/Query.cpp +++ b/Query.cpp @@ -65,13 +65,26 @@ Customer query_customer_with_id(int id) { string telephone = query.getColumn(4); vector park_instances = query_parktimes_for_customer(id); Customer result{id, name, password, Vehicle_type(vehicle), park_instances, telephone}; - // DEBUG - // cout << "{" << result.id << "," <& parkspots) { diff --git a/data.cpp b/data.cpp index 91b9033..8fe4fae 100644 --- a/data.cpp +++ b/data.cpp @@ -18,13 +18,14 @@ SQLite::Database start_db() { } db.exec( "create table if not exists Customer (id integer primary key, name " - "text, password text, vehicle int, telephone text)"); + "text, password text, vehicle int, telephone text);"); db.exec( "create table if not exists Park_spot (id integer primary key, taken " - "int, customer_id int, vehicle_type int)"); + "int, customer_id int, vehicle_type int);"); 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)"); + "customer_id int, spot_id int, start int, end int, duration int);"); + db.exec("create table if not exists Admin (id int primary key, name text, password text);"); return db; } diff --git a/headers/Admin.h b/headers/Admin.h new file mode 100644 index 0000000..9619bae --- /dev/null +++ b/headers/Admin.h @@ -0,0 +1,21 @@ +#ifndef ADMIN_H +#define ADMIN_H +#pragma once + +#include "data.h" + +class Admin { + public: + int id; + string name; + string password; + Admin(string name_, string password_); + Admin(int id_, string name_, string password); + +// private: + void update_db(); + void save_db(); + int auto_increment_db(); +}; + +#endif // CUSTOMER_H diff --git a/headers/Customer.h b/headers/Customer.h index a457f80..9f7c9b2 100644 --- a/headers/Customer.h +++ b/headers/Customer.h @@ -3,7 +3,6 @@ #pragma once #include "Park_time.h" -#include "data.h" #include diff --git a/headers/Interface.h b/headers/Interface.h index 7051928..06598ff 100644 --- a/headers/Interface.h +++ b/headers/Interface.h @@ -10,4 +10,5 @@ void interface_member(vector& spots); void interface_admin(vector& spots); void park(Customer& c, vector& spots); void new_customer(); -void new_parkspot(vector& spots); \ No newline at end of file +void new_parkspot(vector& spots); +void new_admin(); \ No newline at end of file diff --git a/headers/Query.h b/headers/Query.h index 61e33f5..cf15521 100644 --- a/headers/Query.h +++ b/headers/Query.h @@ -3,6 +3,7 @@ #pragma once #include "Park_spot.h" +#include "Admin.h" /*these are the functions that search the database and create objects from it. @@ -25,7 +26,7 @@ customers who have the same name. 2. I have no clue how many of you have done error handling in c++ (try/catch/finally). -Ya boi is nice and doesn't want to bombard you with more new concepts than needed. +I dont want to bombard you with more new concepts than needed. so now you'd do vector test = query_customer_with_name("Testman"); @@ -46,9 +47,6 @@ finally{ do more stuff } -3. Ya boi needs to brush up on how to create custom exceptions class, and it will complicate code -furhter. - */ vector query_parktimes_for_customer(int cid); @@ -56,12 +54,11 @@ vector query_parktimes_for_customer(int cid); vector query_customer_with_name(string name); Customer query_customer_with_id(int id); +Admin query_admin_with_id(int id); + vector populate_spots(); - Park_spot query_parkspot_with_id(int id, vector& parkspots); - void reports_from_parkspot(int spotid, bool weekly = false); void reports_from_allparkspots(bool weekly = false); - void current_status_parkspots(vector& spots); #endif // CUSTOMER_H diff --git a/main.cpp b/main.cpp index 596b79c..23c08ff 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,5 @@ #include "headers/Interface.h" +#include "headers/Admin.h" /* Code structure is like this: @@ -45,9 +46,9 @@ static vector park_customers; int main() { // state of db: - // er zijn 10 parkspots, 5 met biketype en 5 met pickup type - // er is een customer met id 1(testcustomer) met password "password" - + // er zijn 5 parkspots, 2 met 2weeler en 4 met 4weeler + // er zijn customers met password "password" + // er is een admin id=1 met password PASSWORD interface(parking_spots); } diff --git a/test.db3 b/test.db3 index e591386fa0f5dbcd250b2f914e0d97ad7408db94..823aad0c2b6da605f6f8e75f44726783fe734929 100644 GIT binary patch delta 676 zcmaix&2Q3R7{K4RPzEIQg;V2- zKjF9<?%=X`t}7z`VJ*6XtaXPZNw`KD*BuE-UJ_-~;HhRNd6c1!l26MS$_!DD zMV=C6?cGQ8$iKgj+sFh7fhhBWp-x7kCUm%SO)>qBnb2WOT+d_Q42bv8X(3hsy+nXm zZ9MKNv3zCO0r%E5$CH*OoN_YFP5X)*5P5o*VB)lt2r_}N->W4U?>NEG!{Kb0X$2>m zG56Q`=s3~}8ghd!`ljuYdOQl}>wNvJkUJ>*bB*j7V}uulQjAMY$9lM4>H8MhUN|0= z6?x8e&s9+tRH$C;S)tp8E&=+5exUE@8~TDSHy-K}I<(nvUvGn?%wlp?;E%w+n`GAP(8~xDjHX4HYKP3{~Ulfb0`Aec&sI*<`6G hKLPr^HUG)x0reF*aSMbPX0bRNe*tK`wEF-6 delta 85 zcmZoTz}V2hI6+#Fm4ShQ1&CpQd7_T7Fe`(ei2^TBh?y^kfxnvXFJI1PL4jYqn{)VP gFtRi8GcfSi@^4mD_{z6Qo`rESi-Evq7K1