vector<int> removeRelation(vector<Person>& p, vector<Computer>& c) { QTextStream in(stdin); vector<int> relation; unsigned int x, y; QString value; displayPerson(p); do{ cout <<"Select a person: "; value = in.readLine(); x = value.toInt(); if(x > p.size() || x < 1) cout << "Please choose a number from 1 to " << p.size() << endl; else relation.push_back(x - 1); } while(x > p.size() || x < 1); Person pers = p[x - 1]; vector<Computer> comps; for(int i = 0; i < pers.getSize(); i++){ for(unsigned int j = 0; j < c.size(); j++){ if(pers.getComputer(i) == c[j].getId()){ comps.push_back(c[j]); } } } displayComputer(comps); do{ cout << "Select a connected computer to remove from chosen person: "; value = in.readLine(); y = value.toInt(); if(y > comps.size() || y < 1) cout << "Please choose a number from 1 to " << comps.size() << endl; else{ Computer comp = comps[y - 1]; int z = -1; while( c[z].getId() != comp.getId() ) { z++; if( c[z].getId() == comp.getId() ) relation.push_back(z); } } } while( y > c.size() || y < 1 ); return relation; }
int SQLITEHandler::removeEntry( Computer c ) { // Remove computer from database if( !status ) // If not connected, fail return 1; q.prepare( "DELETE FROM computers WHERE id = (:id)" ); // Delete computer q.bindValue( ":id", c.getId() ); if( !q.exec() ) return 2; q.prepare( "DELETE FROM relation WHERE computer = (:id)" ); // Delete relations q.bindValue( ":id", c.getId() ); if( !q.exec() ) // Attempt to execute query return 3; return 0; // Return Success }
bool Person::isRelated( Computer c ) { bool related = false; for( unsigned int x = 0; x < computers.size(); x++ ) { if( computers[x] == c.getId() ) related = true; } return related; }
void MainWindow::on_button_see_connections_computer_clicked() { int currentlySelectedComputerIndex = ui->computer_list_computer_connections->currentIndex().row(); Computer currentlySelectedComputer = currentlyDisplayedComputers.at(currentlySelectedComputerIndex); int idOfComputer = currentlySelectedComputer.getId(); string StringIdOfComputer = static_cast<ostringstream*>(&(ostringstream() << idOfComputer) )->str(); vector<Scientist> scientists = sciService.getScientistsByComputerId(StringIdOfComputer); displayScientistsForComputerConnections(scientists); ui->button_see_connections_computer->setEnabled(false); }
int SQLITEHandler::deleteRelation( Person p, Computer c ) { // Delete relation between Person and Computer in database if( !status ) // If not connected, fail return 1; q.prepare( "DELETE FROM relation WHERE person = (:person) AND computer = (:computer)" ); // Delete relation q.bindValue( ":person", p.getId() ); q.bindValue( ":computer", c.getId() ); if( !q.exec() ) // Attempt to execute query return 2; return 0; // Return Success }
void Scientist::setComputers(std::vector<Computer> newComputers) { destroyComputers(); for (unsigned int i = 0; i < newComputers.size(); i++) { Computer currentComputer = newComputers.at(i); this->computers.push_back(new Computer(currentComputer.getId(), currentComputer.getName(), currentComputer.getType(), currentComputer.getYearBuilt())); } }
int SQLITEHandler::addRelation( Person p, Computer c ) { // Add relation between Person and Computer to database if( !status ) // If not connected, fail return 1; if( p.isRelated( c ) ) // Check if relation already exists return 2; q.prepare( "INSERT INTO relation (person, computer) VALUES (:person, :computer)" ); // Delete relation q.bindValue( ":person", p.getId() ); q.bindValue( ":computer", c.getId() ); if( !q.exec() ) // Attempt to execute query return 3; return 0; // Return Success }
int SQLITEHandler::modifyEntry( Computer c ) { // Modify computer in database if( !status ) // If not connected, fail return 1; q.prepare( "UPDATE computers SET name = (:name), creation = (:creation), type = (:type), constructed = (:constructed) WHERE id = (:id)" ); q.bindValue( ":id", c.getId() ); q.bindValue( ":name", c.getName() ); q.bindValue( ":creation", c.getYear() ); q.bindValue( ":type", c.getType() ); q.bindValue( ":constructed", c.getWasBuilt() ); if( !q.exec() ) // Attempt to execute query return 2; return 0; // Return Success }
bool Computerrepository::updateComputer(Computer computerUpdate) { QSqlQuery query; int id = computerUpdate.getId(); QString name = QString::fromStdString((computerUpdate.getName())); int builtY = computerUpdate.getBuildYear(); QString type = QString::fromStdString((computerUpdate.getType())); bool builtOrNot = computerUpdate.getBuild(); query.prepare("UPDATE Computers SET Name=:Name, YearBuilt=:YearBuilt," " Type=:Type, BuiltOrNot=:BuiltOrNot WHERE id=:id"); query.bindValue(":id", id); query.bindValue(":Name", name); query.bindValue(":YearBuilt", builtY); query.bindValue(":Type", type); query.bindValue(":BuiltOrNot", builtOrNot); return query.exec(); }
bool DbManager::editComputer(Computer comp) { QString was_built = ""; switch (comp.getBuilt()) { case 0: was_built = "0"; break; case 1: was_built = "1"; break; } return execQuery("UPDATE Computers " "SET name = '" + comp.getName() + "', " "year = " + comp.getYear() + ", " "type = '" + comp.getType() + "', " "built = " + was_built + " " "WHERE cID = " + comp.getId()); }
void MainWindow::on_button_remove_computer_clicked() { int currentlySelectedComputerIndex = ui->table_computers->currentIndex().row(); Computer currentlySelectedComputer = currentlyDisplayedComputers.at(currentlySelectedComputerIndex); int idToRemove = currentlySelectedComputer.getId(); string stringIdToRemove = static_cast<ostringstream*>( &(ostringstream() << idToRemove) )->str(); int answer = QMessageBox::question(this, "Confirm", "Are you sure?"); if (answer == QMessageBox::No) { return; } bool success = compService.remove(stringIdToRemove); if (success) { ui->input_filter_computers->setText(""); displayAllComputers(); ui->statusBar->showMessage("Successfully removed computer", 2500); ui->button_remove_computer->setEnabled(false); } else { int answer = QMessageBox::warning(this, "FAIL", "Failed to remove computer"); } }
vector<Scientist>Service::getAssociatedScientist(Computer computerDetails) { int idComputerToMatch = computerDetails.getId(); return scientistRepository.getAssociatedScientists(idComputerToMatch); }