Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HW4 #7

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


HW1/CMakeLists.txt

HW1/.idea/
Expand Down Expand Up @@ -46,4 +44,43 @@ HW3/cmake-build-debug/cmake_install.cmake

HW3/cmake-build-debug/CMakeCache.txt

HW3/cmake-build-debug/HW3.cbp
HW3/cmake-build-debug/HW3.cbp

HW4/Passenger/.idea/

HW4/Passenger/cmake-build-debug/Makefile

HW4/Passenger/cmake-build-debug/CMakeFiles/

HW4/Passenger/cmake-build-debug/Testing/

HW4/Passenger/cmake-build-debug/cmake_install.cmake

HW4/Passenger/cmake-build-debug/CMakeCache.txt

HW4/Passenger/cmake-build-debug/HW3.cbp

HW4/Driver/.idea/

HW4/Driver/cmake-build-debug/Makefile

HW4/Driver/cmake-build-debug/CMakeFiles/

HW4/Driver/cmake-build-debug/Testing/

HW4/Driver/cmake-build-debug/cmake_install.cmake

HW4/Driver/cmake-build-debug/CMakeCache.txt

HW4/Driver/cmake-build-debug/HW3.cbp
HW4/Driver/cmake-build-debug/Driver.cbp

HW4/Passenger/cmake-build-debug/Passenger.cbp

HW4/Passenger/cmake-build-debug/

HW4/Admin/.idea/

HW4/Admin/cmake-build-debug/

HW4/Driver/cmake-build-debug/Driver.exe
5 changes: 5 additions & 0 deletions HW4/Admin/Admin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//
// Created by egord on 25.04.2021.
//

#include "Admin.h"
17 changes: 17 additions & 0 deletions HW4/Admin/Admin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Created by egord on 25.04.2021.
//

#ifndef ADMIN_ADMIN_H
#define ADMIN_ADMIN_H

#include <iostream>

class Admin {
int id;
std::string login;
std::string password;
};


#endif //ADMIN_ADMIN_H
54 changes: 54 additions & 0 deletions HW4/Admin/AdminGateway.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// Created by egord on 25.04.2021.
//

#include "AdminGateway.h"

Passenger AdminGateway::getPassengerByID(int id) {
auto userStorage = make_storage("../../users.sqlite",
make_unique_index("idx_passengers_login",
indexed_column(&PassengerStorage::login).collate(
"BINARY").desc()),
make_table("users",
make_column("id", &PassengerStorage::id, primary_key()),
make_column("login", &PassengerStorage::login),
make_column("password", &PassengerStorage::password),
make_column("name", &PassengerStorage::name),
make_column("rating", &PassengerStorage::rating),
make_column("paymentMethods", &PassengerStorage::paymentMethods),
make_column("pinnedAddresses", &PassengerStorage::pinnedAddresses),
make_column("canOrder", &PassengerStorage::canOrder)));
userStorage.sync_schema();
auto row = userStorage.select(columns(&PassengerStorage::id,
&PassengerStorage::login,
&PassengerStorage::password,
&PassengerStorage::name,
&PassengerStorage::rating,
&PassengerStorage::paymentMethods,
&PassengerStorage::pinnedAddresses,
&PassengerStorage::canOrder),
where(is_equal(&PassengerStorage::id, id)));
return Passenger{row[0]};
}

void AdminGateway::ListAllPassengers(){
auto userStorage = make_storage("../../users.sqlite",
make_unique_index("idx_passengers_login", indexed_column(&PassengerStorage::login).collate("BINARY").desc()),
make_table("users",
make_column("id", &PassengerStorage::id, primary_key()),
make_column("login", &PassengerStorage::login),
make_column("password", &PassengerStorage::password),
make_column("name", &PassengerStorage::name),
make_column("rating", &PassengerStorage::rating),
make_column("paymentMethods", &PassengerStorage::paymentMethods),
make_column("pinnedAddresses", &PassengerStorage::pinnedAddresses),
make_column("canOrder", &PassengerStorage::canOrder)
));
userStorage.sync_schema();
for (auto i : userStorage.select(columns(&PassengerStorage::id,
&PassengerStorage::login,
&PassengerStorage::password,
&PassengerStorage::name))){
std::cout << get<0>(i) << ' ' << get<1>(i) << ' ' << get<2>(i) << ' ' << get<3>(i)<< '\n';
}
}
19 changes: 19 additions & 0 deletions HW4/Admin/AdminGateway.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Created by egord on 25.04.2021.
//

#ifndef ADMIN_ADMINGATEWAY_H
#define ADMIN_ADMINGATEWAY_H

#include "../Passenger/Passenger.h"
#include "../Driver/PassengerStorage.h"
#include "../Driver/Driver.h"

class AdminGateway {
public:
static Passenger getPassengerByID(int id);
static void ListAllPassengers();
};


#endif //ADMIN_ADMINGATEWAY_H
6 changes: 6 additions & 0 deletions HW4/Admin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.17)
project(Admin)

set(CMAKE_CXX_STANDARD 17)

add_executable(Admin main.cpp Admin.cpp Admin.h AdminGateway.cpp AdminGateway.h)
15 changes: 15 additions & 0 deletions HW4/Admin/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Created by genom10 on 25.04.2021.
//

#include <iostream>
#include "../includes/sqlite_orm.h"

#include "Admin.h"
#include "AdminGateway.h"

int main() {
Admin admin;
AdminGateway::ListAllPassengers();
return 0;
}
6 changes: 6 additions & 0 deletions HW4/Driver/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.17)
project(Driver)

set(CMAKE_CXX_STANDARD 17)

add_executable(Driver main.cpp Car.cpp Car.h ../includes/sqlite_orm.h ../includes/sqlite3.c ../includes/sqlite3.h ../includes/sqlite3ext.h Driver.cpp Driver.h DriverGateway.cpp DriverGateway.h PassengerStorage.h)
13 changes: 13 additions & 0 deletions HW4/Driver/Car.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Created by egord on 13.04.2021.
//

#include "Car.h"

Car::Car(std::string carType) {
this->carType = carType;
}

std::string Car::getCarType() {
return carType;
}
19 changes: 19 additions & 0 deletions HW4/Driver/Car.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Created by egord on 13.04.2021.
//

#ifndef DRIVER_CAR_H
#define DRIVER_CAR_H

#include <string>

class Car {
public:
std::string carType;
std::string getCarType();
Car() = default;
Car(std::string);
};


#endif //DRIVER_CAR_H
165 changes: 165 additions & 0 deletions HW4/Driver/Driver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
//
// Created by egord on 4/14/2021.
//

#include "Driver.h"


Driver::Driver(std::tuple<int, std::string, std::string, std::string, int, std::string, std::string> data) {
//initialization from SQLite
this->id = std::get<0>(data);
this->login = std::get<1>(data);
this->password = std::get<2>(data);
this->name = std::get<3>(data);
this->rating = std::get<4>(data);
this->orderHistory = std::get<5>(data);

std::stringstream ss(std::get<6>(data));
std::string token;
while (std::getline(ss, token, ',')) {
this->cars.push_back(Car(token));
}
}

void Driver::ListAll(){
auto driverStorage = make_storage("../../drivers.sqlite",
make_unique_index("idx_passengers_login", indexed_column(&Driver::login).collate("BINARY").desc()),
make_table("users",
make_column("id", &Driver::id, primary_key()),
make_column("login", &Driver::login),
make_column("password", &Driver::password),
make_column("name", &Driver::name),
make_column("rating", &Driver::rating),
make_column("orderHistory", &Driver::orderHistory),
make_column("carTypes", &Driver::carTypes)));
driverStorage.sync_schema();
for (auto i : driverStorage.select(columns(&Driver::id,
&Driver::login,
&Driver::password,
&Driver::name,
&Driver::orderHistory))){
std::cout << get<0>(i) << ' ' << get<1>(i) << ' ' << get<2>(i) << ' ' << get<3>(i) << ' ' << get<4>(i) << '\n';
}
}

int Driver::getID() const {
return id;
}

int Driver::getRating() const {
return rating;
}

bool Driver::fetchLogin(std::string login) {
auto userStorage = make_storage("../../drivers.sqlite",
make_unique_index("idx_passengers_login", indexed_column(&Driver::login).collate("BINARY").desc()),
make_table("users",
make_column("id", &Driver::id, primary_key()),
make_column("login", &Driver::login),
make_column("password", &Driver::password),
make_column("name", &Driver::name),
make_column("rating", &Driver::rating),
make_column("orderHistory", &Driver::orderHistory),
make_column("carTypes", &Driver::carTypes)));
userStorage.sync_schema();

auto row = userStorage.select(columns(&Driver::id),
where(is_equal(&Driver::login, login)));
return row.empty();
}

Driver Driver::logIn(std::string login, std::string password) {
auto userStorage = make_storage("../../drivers.sqlite",
make_unique_index("idx_passengers_login", indexed_column(&Driver::login).collate("BINARY").desc()),
make_table("users",
make_column("id", &Driver::id, primary_key()),
make_column("login", &Driver::login),
make_column("password", &Driver::password),
make_column("name", &Driver::name),
make_column("rating", &Driver::rating),
make_column("orderHistory", &Driver::orderHistory),
make_column("carTypes", &Driver::carTypes)));
userStorage.sync_schema();

auto row = userStorage.select(columns(&Driver::id,
&Driver::login,
&Driver::password,
&Driver::name,
&Driver::rating,
&Driver::orderHistory,
&Driver::carTypes),
where(is_equal(&Driver::login, login)));
Driver passenger{row[0]};
if (passenger.password == password)
return passenger;
else
throw "password mismatch";
}

Driver Driver::reg(std::string login, std::string password, std::string name, std::string carType) {
auto driverStorage = make_storage("../../drivers.sqlite",
make_unique_index("idx_passengers_login", indexed_column(&Driver::login).collate("BINARY").desc()),
make_table("users",
make_column("id", &Driver::id, primary_key()),
make_column("login", &Driver::login),
make_column("password", &Driver::password),
make_column("name", &Driver::name),
make_column("rating", &Driver::rating),
make_column("orderHistory", &Driver::orderHistory),
make_column("carType", &Driver::carTypes)));
driverStorage.sync_schema();

Driver driver{login, password, name, carType};
int id = driverStorage.insert(driver);
return Driver(driverStorage.select(columns(&Driver::id,
&Driver::login,
&Driver::password,
&Driver::name,
&Driver::rating,
&Driver::orderHistory,
&Driver::carTypes),
where(is_equal(&Driver::id, id)))[0]);
}

void Driver::remove(Driver *passenger) {
auto userStorage = make_storage("../../drivers.sqlite",
make_unique_index("idx_passengers_login", indexed_column(&Driver::login).collate("BINARY").desc()),
make_table("users",
make_column("id", &Driver::id, primary_key()),
make_column("login", &Driver::login),
make_column("password", &Driver::password),
make_column("name", &Driver::name),
make_column("rating", &Driver::rating),
make_column("orderHistory", &Driver::orderHistory),
make_column("carType", &Driver::carTypes)));
userStorage.sync_schema();

userStorage.remove<Driver>(passenger->getID());
//TODO remove from associated rides
passenger = NULL;
}

void Driver::addOrder(int i) {
auto userStorage = make_storage("../../drivers.sqlite",
make_unique_index("idx_passengers_login", indexed_column(&Driver::login).collate("BINARY").desc()),
make_table("users",
make_column("id", &Driver::id, primary_key()),
make_column("login", &Driver::login),
make_column("password", &Driver::password),
make_column("name", &Driver::name),
make_column("rating", &Driver::rating),
make_column("orderHistory", &Driver::orderHistory),
make_column("carType", &Driver::carTypes)));
userStorage.sync_schema();

if (orderHistory == "")
orderHistory = std::to_string(i);
else
orderHistory += "," + std::to_string(i);
userStorage.update(*this);//todo
}

std::vector<Car> Driver::getCars() {
return cars;
}

Loading