generated from wessel/boilerplate
feat(database): Add query to fetch all grades
This commit is contained in:
@@ -207,4 +207,32 @@ void DatabaseManager::insert_sample_data() {
|
||||
}
|
||||
}
|
||||
|
||||
int DatabaseManager::get_final_course_grade(const StudentCourse& sc) {
|
||||
if (!is_connected()) return -1;
|
||||
|
||||
try {
|
||||
pqxx::work txn(*conn_);
|
||||
|
||||
auto result = txn.exec_params(SQL_SELECT_STUDENT_COURSE_RESULTS, sc.student_name, sc.course_name);
|
||||
|
||||
if (result.size() == 1) {
|
||||
int exam_count = result[0][0].as<int>();
|
||||
if (exam_count == 0) {
|
||||
return -1; // No exams taken
|
||||
}
|
||||
int avg_grade = static_cast<int>(result[0][1].as<double>() + 0.5);
|
||||
return avg_grade;
|
||||
} else {
|
||||
return -1; // No results found
|
||||
}
|
||||
|
||||
} catch (const pqxx::sql_error &e) {
|
||||
RCLCPP_ERROR(logger_, "[DBS] get final course grade failed: %s", e.what());
|
||||
return -1;
|
||||
} catch (const std::exception &e) {
|
||||
RCLCPP_ERROR(logger_, "[DBS] database error: %s", e.what());
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace assignments::one::exam_result_generator
|
||||
|
||||
@@ -36,6 +36,9 @@ public:
|
||||
bool store_exam_result(const std::string& student_name, const std::string& course_name, int grade);
|
||||
bool enroll_student_into_course(const StudentCourse& sc);
|
||||
bool store_final_course_result(const StudentCourse& sc, int exam_count, int final_grade);
|
||||
|
||||
int get_final_course_grade(const StudentCourse& sc);
|
||||
|
||||
private:
|
||||
rclcpp::Logger logger_;
|
||||
|
||||
|
||||
@@ -51,6 +51,12 @@ static const std::string SQL_SELECT_MISSING_RESULTS = R"(
|
||||
WHERE fcr.id IS NULL
|
||||
)";
|
||||
|
||||
static const std::string SQL_SELECT_STUDENT_COURSE_RESULTS = R"(
|
||||
SELECT COUNT(*), AVG(exam_grade)
|
||||
FROM exam_results
|
||||
WHERE student_name = $1 AND course_name = $2
|
||||
)";
|
||||
|
||||
static const std::string SQL_INSERT_EXAM_RESULT = R"(
|
||||
INSERT INTO exam_results (student_name, course_name, exam_grade)
|
||||
VALUES ($1, $2, $3)
|
||||
|
||||
Reference in New Issue
Block a user