void MainWindow::on_button_remove_scientist_clicked() { int currentlySelectedScientistIndex = ui->table_scientists->currentIndex().row(); Scientist currentlySelectedScientist = currentlyDisplayedScientists.at(currentlySelectedScientistIndex); int idToRemove = currentlySelectedScientist.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 = sciService.remove(stringIdToRemove); if (success) { ui->input_filter_scientists->setText(""); displayAllScientists(); ui->statusBar->showMessage("Successfully removed scientist", 2500); ui->button_remove_scientist->setEnabled(false); } else { QMessageBox::warning(this, "FAIL", "Failed to remove scientist"); } }
std::vector<Computer> ScientistRepository::queryComputersByScientist(Scientist scientist) { vector<Computer> computers; db.open(); if (!db.isOpen()) { return computers; } QSqlQuery query(db); stringstream sqlQuery; sqlQuery << "SELECT s.* FROM ScientistComputerConnections scc "; sqlQuery << "JOIN Computers c "; sqlQuery << "ON c.id = scc.computerId "; sqlQuery << "WHERE scc.scientistId = " << scientist.getId(); query.exec(QString::fromStdString(sqlQuery.str())); while (query.next()) { int id = query.value("id").toUInt(); string name = query.value("name").toString().toStdString(); enum computerType type = utils::intToComputerType(query.value("type").toInt()); int yearBuilt = query.value("yearBuilt").toInt(); computers.push_back(Computer(id, name, type, yearBuilt)); } return computers; }
void MainWindow::on_button_see_connections_scientist_clicked() { int currentlySelectedScientistIndex = ui->scientist_list_scientist_connections->currentIndex().row(); Scientist currentlySelectedScientist = currentlyDisplayedScientists.at(currentlySelectedScientistIndex); int idOfScientist = currentlySelectedScientist.getId(); string stringIdOfScientist = static_cast<ostringstream*>(&(ostringstream() << idOfScientist) )->str(); vector<Computer> computers = compService.getComputersByScientistId(stringIdOfScientist); displayComputersForScientistConnections(computers); ui->button_see_connections_scientist->setEnabled(false); }
bool ScientistRepository::removeScientist(Scientist scientist) { db.open(); QSqlQuery query(db); stringstream sqlQuery; sqlQuery << "DELETE FROM Scientists WHERE id = " << scientist.getId(); bool success = query.exec(QString::fromStdString(sqlQuery.str())); db.close(); return success; }
bool Service::updateScientist(Scientist scientistUpdate) { string bufferFirstName = scientistUpdate.getF(), bufferLastName = ""; seperateFirstLast(bufferFirstName, bufferLastName); Scientist nameUpdated(scientistUpdate.getId(), bufferFirstName, bufferLastName, scientistUpdate.getGender(), scientistUpdate.getYearBorn(), scientistUpdate.getYearDied(), scientistUpdate.getKnownFor()); return scientistRepository.updateScientist(nameUpdated); }
bool Scientistrepository::updateScientist(Scientist scientistUpdate) { QSqlQuery query; int id = scientistUpdate.getId(); QString fName = QString::fromStdString((scientistUpdate.getF())); QString lName = QString::fromStdString((scientistUpdate.getL())); bool gender = scientistUpdate.getGender(); int born = scientistUpdate.getYearBorn(); int died = scientistUpdate.getYearDied(); QString known = QString::fromStdString((scientistUpdate.getKnownFor())); query.prepare("UPDATE Scientists SET FirstName=:FirstName, LastName=:LastName, " "Gender=:Gender, Born=:Born, Died=:Died, KnownFor=:KnownFor WHERE id=:id"); query.bindValue(":id", id); query.bindValue(":FirstName", fName); query.bindValue(":LastName", lName); query.bindValue(":Gender", gender); query.bindValue(":Born", born); query.bindValue(":Died", died); query.bindValue(":KnownFor", known); return query.exec(); }
vector<Computer>Service::getAssociatedComputers(Scientist scientistDetails) { int idScientistToMatch = scientistDetails.getId(); return computerRepository.getAssociatedComputers(idScientistToMatch); }