added admin
This commit is contained in:
parent
5e2b91fda6
commit
3c7d7121e2
36
Admin.cpp
Normal file
36
Admin.cpp
Normal file
@ -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;
|
||||||
|
}
|
@ -23,6 +23,8 @@ add_executable(park
|
|||||||
headers/Park_spot.h
|
headers/Park_spot.h
|
||||||
Park_time.cpp
|
Park_time.cpp
|
||||||
headers/Park_time.h
|
headers/Park_time.h
|
||||||
|
Admin.cpp
|
||||||
|
headers/Admin.h
|
||||||
Query.cpp
|
Query.cpp
|
||||||
headers/Query.h
|
headers/Query.h
|
||||||
Interface.cpp
|
Interface.cpp
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#include "headers/Interface.h"
|
#include "headers/Interface.h"
|
||||||
|
|
||||||
|
|
||||||
// I added it to pass spots, because the parking options need it to check where
|
// 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
|
// is free parking_spots is declared in main, and if i declare it
|
||||||
// liberal use of
|
// liberal use of
|
||||||
@ -61,6 +62,21 @@ void interface_member(vector<Park_spot>& spots) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void interface_admin(vector<Park_spot>& spots) {
|
void interface_admin(vector<Park_spot>& 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 << "Welcome to the admin interface. It is not completely ready yet.\n";
|
||||||
cout << "[1] See monthly report of ALL parking spots\n";
|
cout << "[1] See monthly report of ALL parking spots\n";
|
||||||
cout << "[2] See weekly report of ALL parking spots\n";
|
cout << "[2] See weekly report of ALL parking spots\n";
|
||||||
@ -69,7 +85,8 @@ void interface_admin(vector<Park_spot>& spots) {
|
|||||||
cout << "[5] See current status of parking spots\n";
|
cout << "[5] See current status of parking spots\n";
|
||||||
cout << "[6] Make new customer\n";
|
cout << "[6] Make new customer\n";
|
||||||
cout << "[7] Make new parking spot\n";
|
cout << "[7] Make new parking spot\n";
|
||||||
cout << "option[1-7]:";
|
cout << "[8] Make new Admin\n";
|
||||||
|
cout << "option[1-8]:";
|
||||||
int option;
|
int option;
|
||||||
cin >> option;
|
cin >> option;
|
||||||
cin.ignore(10000, '\n');
|
cin.ignore(10000, '\n');
|
||||||
@ -110,6 +127,10 @@ void interface_admin(vector<Park_spot>& spots) {
|
|||||||
new_parkspot(spots);
|
new_parkspot(spots);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case 8: {
|
||||||
|
new_admin();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@ -174,6 +195,18 @@ void new_customer() {
|
|||||||
newcustomer.update_db();
|
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<Park_spot>& spots) {
|
void new_parkspot(vector<Park_spot>& spots) {
|
||||||
cout << "What type of parking spot? [1] twoweeler, [2] fourweeler: ";
|
cout << "What type of parking spot? [1] twoweeler, [2] fourweeler: ";
|
||||||
int vtype;
|
int vtype;
|
||||||
|
19
Query.cpp
19
Query.cpp
@ -65,13 +65,26 @@ Customer query_customer_with_id(int id) {
|
|||||||
string telephone = query.getColumn(4);
|
string telephone = query.getColumn(4);
|
||||||
vector<Park_time> park_instances = query_parktimes_for_customer(id);
|
vector<Park_time> park_instances = query_parktimes_for_customer(id);
|
||||||
Customer result{id, name, password, Vehicle_type(vehicle), park_instances, telephone};
|
Customer result{id, name, password, Vehicle_type(vehicle), park_instances, telephone};
|
||||||
// DEBUG
|
|
||||||
// cout << "{" << result.id << "," <<result.password <<"," <<
|
|
||||||
// int(vehicle) << "}\n";
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//----------------- ADMIN
|
||||||
|
|
||||||
|
Admin query_admin_with_id(int id) {
|
||||||
|
|
||||||
|
|
||||||
|
SQLite::Statement query(data::db, "SELECT * FROM Customer WHERE id = ?;");
|
||||||
|
query.bind(1, id);
|
||||||
|
while (query.executeStep()) {
|
||||||
|
string name = query.getColumn(1);
|
||||||
|
string password = query.getColumn(2);
|
||||||
|
Admin result{id, name, password};
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//------------------------------- parkspot info
|
//------------------------------- parkspot info
|
||||||
|
|
||||||
Park_spot query_parkspot_with_id(int id, vector<Park_spot>& parkspots) {
|
Park_spot query_parkspot_with_id(int id, vector<Park_spot>& parkspots) {
|
||||||
|
7
data.cpp
7
data.cpp
@ -18,13 +18,14 @@ SQLite::Database start_db() {
|
|||||||
}
|
}
|
||||||
db.exec(
|
db.exec(
|
||||||
"create table if not exists Customer (id integer primary key, name "
|
"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(
|
db.exec(
|
||||||
"create table if not exists Park_spot (id integer primary key, taken "
|
"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(
|
db.exec(
|
||||||
"create table if not exists Park_time (id integer primary key, "
|
"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;
|
return db;
|
||||||
}
|
}
|
||||||
|
21
headers/Admin.h
Normal file
21
headers/Admin.h
Normal file
@ -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
|
@ -3,7 +3,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Park_time.h"
|
#include "Park_time.h"
|
||||||
#include "data.h"
|
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
@ -10,4 +10,5 @@ void interface_member(vector<Park_spot>& spots);
|
|||||||
void interface_admin(vector<Park_spot>& spots);
|
void interface_admin(vector<Park_spot>& spots);
|
||||||
void park(Customer& c, vector<Park_spot>& spots);
|
void park(Customer& c, vector<Park_spot>& spots);
|
||||||
void new_customer();
|
void new_customer();
|
||||||
void new_parkspot(vector<Park_spot>& spots);
|
void new_parkspot(vector<Park_spot>& spots);
|
||||||
|
void new_admin();
|
@ -3,6 +3,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Park_spot.h"
|
#include "Park_spot.h"
|
||||||
|
#include "Admin.h"
|
||||||
|
|
||||||
/*these are the functions that search the database and create objects from it.
|
/*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++
|
2. I have no clue how many of you have done error handling in c++
|
||||||
(try/catch/finally).
|
(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
|
so now you'd do
|
||||||
|
|
||||||
vector<Customer> test = query_customer_with_name("Testman");
|
vector<Customer> test = query_customer_with_name("Testman");
|
||||||
@ -46,9 +47,6 @@ finally{
|
|||||||
do more stuff
|
do more stuff
|
||||||
}
|
}
|
||||||
|
|
||||||
3. Ya boi needs to brush up on how to create custom exceptions class, and it will complicate code
|
|
||||||
furhter.
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
vector<Park_time> query_parktimes_for_customer(int cid);
|
vector<Park_time> query_parktimes_for_customer(int cid);
|
||||||
@ -56,12 +54,11 @@ vector<Park_time> query_parktimes_for_customer(int cid);
|
|||||||
vector<Customer> query_customer_with_name(string name);
|
vector<Customer> query_customer_with_name(string name);
|
||||||
Customer query_customer_with_id(int id);
|
Customer query_customer_with_id(int id);
|
||||||
|
|
||||||
|
Admin query_admin_with_id(int id);
|
||||||
|
|
||||||
vector<Park_spot> populate_spots();
|
vector<Park_spot> populate_spots();
|
||||||
|
|
||||||
Park_spot query_parkspot_with_id(int id, vector<Park_spot>& parkspots);
|
Park_spot query_parkspot_with_id(int id, vector<Park_spot>& parkspots);
|
||||||
|
|
||||||
void reports_from_parkspot(int spotid, bool weekly = false);
|
void reports_from_parkspot(int spotid, bool weekly = false);
|
||||||
void reports_from_allparkspots(bool weekly = false);
|
void reports_from_allparkspots(bool weekly = false);
|
||||||
|
|
||||||
void current_status_parkspots(vector<Park_spot>& spots);
|
void current_status_parkspots(vector<Park_spot>& spots);
|
||||||
#endif // CUSTOMER_H
|
#endif // CUSTOMER_H
|
||||||
|
7
main.cpp
7
main.cpp
@ -1,4 +1,5 @@
|
|||||||
#include "headers/Interface.h"
|
#include "headers/Interface.h"
|
||||||
|
#include "headers/Admin.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Code structure is like this:
|
Code structure is like this:
|
||||||
@ -45,9 +46,9 @@ static vector<Customer> park_customers;
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// state of db:
|
// state of db:
|
||||||
// er zijn 10 parkspots, 5 met biketype en 5 met pickup type
|
// er zijn 5 parkspots, 2 met 2weeler en 4 met 4weeler
|
||||||
// er is een customer met id 1(testcustomer) met password "password"
|
// er zijn customers met password "password"
|
||||||
|
// er is een admin id=1 met password PASSWORD
|
||||||
interface(parking_spots);
|
interface(parking_spots);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user