diff --git a/week8/1-The-Last-HR/Database.sql b/week8/1-The-Last-HR/Database.sql new file mode 100644 index 0000000..42e0b09 --- /dev/null +++ b/week8/1-The-Last-HR/Database.sql @@ -0,0 +1,24 @@ +DROP TABLE IF EXISTS Students; + +CREATE TABLE Students( + student_id INTEGER PRIMARY KEY, + student_name TEXT, + student_github TEXT +); + +DROP TABLE IF EXISTS Courses; + +CREATE TABLE Courses( + course_id INTEGER PRIMARY KEY, + course_name TEXT +); + +DROP TABLE IF EXISTS Student_To_Course; + +CREATE TABLE Student_To_Course( + student_id INTEGER, + course_id INTEGER, + course_group INTEGER, + FOREIGN KEY(student_id) REFERENCES Students(student_id), + FOREIGN KEY(course_id) REFERENCES Courses(course_id) +); diff --git a/week8/1-The-Last-HR/Fill_Database.py b/week8/1-The-Last-HR/Fill_Database.py new file mode 100644 index 0000000..58f7f09 --- /dev/null +++ b/week8/1-The-Last-HR/Fill_Database.py @@ -0,0 +1,71 @@ +from Settings import DB_NAME, SQL_FILE +import sqlite3 +import requests + + +def connect(): + connection = sqlite3.connect(DB_NAME) + + with open(SQL_FILE, "r") as f: + connection.executescript(f.read()) + connection.commit() + + return connection + + +def get_students(cursor, information): + get_students = [dict(student) for student in information] + for student in get_students: + cursor.execute(""" + INSERT INTO Students(student_name, student_github) VALUES(?, ?) + """, (student['name'], student['github'])) + return + + +def get_courses(cursor, information): + courses_tuple = set() + + get_courses = [course['courses'][0]['name'] + for course in information if len(course['courses']) > 0] + for course in get_courses: + courses_tuple.add(course) + + for course in courses_tuple: + cursor.execute(""" + INSERT INTO Courses(course_name) VALUES(?) + """, (course,)) + return + + +def student_to_course(cursor, information): + for student_id in information: + student_id = information.index(student_id) + get_course = information[student_id]['courses'] + if len(get_course) > 0: + for course in get_course: + course_group = course['group'] + course_id = cursor.execute(""" + SELECT course_id FROM courses WHERE course_name = ? + """, (course['name'],)) + course_id = course_id.fetchone() + if course_id is not None: + cursor.execute(""" + INSERT INTO student_to_course(student_id, course_id, course_group) + VALUES(?, ?, ?)""", (student_id, course_id[0], course_group)) + return + + +def main(): + database = connect() + cursor = database.cursor() + + request = requests.get('https://hackbulgaria.com/api/students/') + get_students(cursor, request.json()) + get_courses(cursor, request.json()) + student_to_course(cursor, request.json()) + + database.commit() + + +if __name__ == '__main__': + main() diff --git a/week8/1-The-Last-HR/Queries.py b/week8/1-The-Last-HR/Queries.py new file mode 100644 index 0000000..118d07f --- /dev/null +++ b/week8/1-The-Last-HR/Queries.py @@ -0,0 +1,26 @@ +import sqlite3 + +connection = sqlite3.connect("Academy.db") +cursor = connection.cursor() + + +def list_students_github(): + result = cursor.execute( + """SELECT student_name, student_github FROM students""") + return result + + +def list_courses(): + result = cursor.execute("""SELECT course_name FROM courses""") + return result + + +def student_course(): + result = cursor.execute(""" + SELECT student_name,course_name + FROM students,courses + JOIN student_to_course + ON courses.course_id = student_to_course.course_id + AND students.student_id = student_to_course.student_id + """) + return result diff --git a/week8/1-The-Last-HR/Settings.py b/week8/1-The-Last-HR/Settings.py new file mode 100644 index 0000000..45b1ce5 --- /dev/null +++ b/week8/1-The-Last-HR/Settings.py @@ -0,0 +1,2 @@ +DB_NAME = "Academy.db" +SQL_FILE = "Database.sql"