def get_courses_by_teacher_id(teacher_id): sql = "SELECT * FROM courses WHERE teacher_id=%s" cursor = connection.cursor() cursor.execute(sql, [teacher_id]) records = cursor.fetchall() courses_list = [] for record in records: course = Courses(record[0], record[1], record[2]) courses_list.append(course.json()) return courses_list
def all_courses(self): # Retrieve all Course from courses and return sql = "SELECT * FROM courses" cursor = connection.cursor() cursor.execute(sql) records = cursor.fetchall() courses_list = [] for record in records: course = Courses(record[0], record[1], record[2]) courses_list.append(course.json()) return courses_list
def get_courses( self, empid ): # Retrieve a single Course for an employee by passing an employee ID sql = "SELECT * FROM courses WHERE empid=%s " cursor = connection.cursor() cursor.execute(sql, [empid]) record = cursor.fetchall() courses_list = [] for records in record: course = Courses(records[0], records[1], records[2]) courses_list.append(course.json()) return courses_list
def post_employee_course(empid): try: course_ = Courses.json_parse(request.json) CourseService.create_courses(course_, empid) return jsonify(course_.json()), 201 except ValueError as e: return "Not a valid ID or No such user exist with this ID", 400 # Bad Request
def initialiseCourses(): allCourses = Courses.query.filter_by().all() if allCourses: if len(allCourses) > 500: print("Already Initialised The CourseList.JSON file") return with open("courseList.json", "r") as file: courseString = file.readlines() for i in courseString: for char in ["\"", "[", "]", ","]: i = i.replace(char, "") stringToList = i.split(' - ') courseCode, courseName = stringToList[0], stringToList[1] course = Courses(id=str(uuid4().hex), code=courseCode, name=courseName[:-1], university=unswID) db.session.add(course) db.session.commit() print(''' |||||||||||||||||||||||||||||||||||||| ||||||||| All courses added |||||||||| |||||||||||||||||||||||||||||||||||||| ''') return True
def update_courses(self, change, courseid): # Update Courses Table sql = "UPDATE courses SET coursename=%s WHERE courseid=%s " cursor = connection.cursor() cursor.execute(sql, change.coursename) connection.commit() record = cursor.fetchone() return Courses(int(record[0]), record[1], int(record[2]))
def get_course_by_id(course_id): sql = "SELECT * FROM courses WHERE id=%s" cursor = connection.cursor() cursor.execute(sql, [course_id]) record = cursor.fetchone() course = Courses(record[0], record[1], record[2]) return course
def create_courses(self, course, empid): # Create new Course # check if employee exist before creating a course or raise an error (404) try: exist_empid = EmployeeDAOImpl() exist_empid.get_employee(empid) except ResourceNotFound as r: raise r sql = "INSERT INTO courses VALUES(DEFAULT, %s, %s) RETURNING *" cursor = connection.cursor() cursor.execute(sql, ( course.coursename, course.empid, )) connection.commit() record = cursor.fetchone() return Courses(Courses(record[0], record[1], record[2]))
def makeCourse(self, data, **kwargs): if (not University.query.filter_by(id=data['university']).first()): return None return Courses( id=str(uuid4().hex), code=data['code'], name=data['name'], university=data['university'], information=data['information'] if 'information' in data else None, description=data['description'] if 'description' in data else None)
def get_all_courses(): sql = "SELECT * FROM courses" cursor = connection.cursor() cursor.execute(sql) records = cursor.fetchall() course_list = [] for record in records: courses = Courses(record[0], record[1], record[2]) course_list.append(courses) return course_list
def get_courseID( self, courseid ): # Retrieve a single Course for an employee by passing an course ID sql = "SELECT * FROM courses WHERE courseid=%s" cursor = connection.cursor() cursor.execute(sql, [courseid]) record = cursor.fetchone() if record: return Courses(record[0], record[1], record[2]) else: return f'Course ID {courseid} - Not found'
def update_course(empid, courseid): # try: emp = EmployeeService.get_employee(int(empid)) if emp: # print(emp) get_course = CourseService.get_course_ID(int(courseid)) if get_course: all_course = Courses.json_parse(request.json) CourseService.update_course(all_course, courseid) return jsonify(all_course.json()), 200 else: return f'Course ID {courseid} - not found', 404 else: return f'Employee ID {emp} - not found', 404
def creation_of_courses(data): """ {"data":{ "course_name" : "a123", "username":"******"}} To create the courses """ role_detail = role_details.find_user_role(data["username"]) if role_detail["role_name"].lower() == 'admin': course_name = data["course_name"] obj = Courses(name=course_name, created_datetime=datetime.now(), is_active=True, student_enrolled=0, modified_datetime=datetime.now()) db.session.add(obj) db.session.commit() course_detail = db.session.query(Courses).filter_by( name=course_name).first() return {"id": course_detail.id} else: return {"error": "don't have access to create course"}
import unittest from models.courses import Courses from daos.courses_dao import CoursesDAO from daos.daos_impl.courses_dao_impl import CoursesDaoImpl courses_dao = CoursesDaoImpl() test_course = Courses(1, 'Physical Science', 6) courses = [test_course] class CourseTest(unittest.TestCase): def test_get_course_by_id(self): retrieved_course = courses_dao.get_course_by_id(test_course.courses_id) assert test_course.name == retrieved_course.name def test_get_course_by_teacher_id(self): retrieved_course = courses_dao.get_courses_by_teacher_id( test_course.teacher_id) assert courses[0].name == test_course.name
def tuition_calculation(cls, cost_limit, amount_awarded, amount_avail, amount_pending): course_object = Courses() courses = course_object.coursename if courses == "University": cost_limit = 1000
from service.courses_service import CoursesService import unittest from unittest.mock import MagicMock from models.courses import Courses from daos.courses_dao import CoursesDAO from daos.daos_impl.courses_dao_impl import CoursesDaoImpl from service.courses_service import CoursesService course = Courses(1, 'Physical Science', 6) course2 = Courses(2, 'Physical Science', 6) course3 = Courses(3, 'Physical Science', 6) courses = [course, course2, course3] mock_courses_dao = CoursesDaoImpl() mock_courses_dao.get_all_courses = MagicMock(return_value=courses) courses_service = CoursesService() class CourseTest(unittest.TestCase): def test_service_all_courses(self): courses = courses_service.all_courses() assert all(courses) def test_service_get_course_by_id(self): retrieved_course = courses_service.get_course_by_id(course.courses_id) assert course.name == retrieved_course.name def test_service_get_courses_by_teacher_id(self): retrieved_course = courses_service.get_course_by_teacher_id( course.teacher_id)