def delete_cake(version, cake_id): """ Controller for API Function that gets a cake by ID @param cake_id: cake id @return: Response and HTTP code """ # API Version 1.X if math.floor(version) == 1: cake = Cake.query.filter_by(id=cake_id).first() if cake is None: return jsonify( errors.error_object_not_found()), statuscodes.HTTP_NOT_FOUND db.session.delete(cake) try: db.session.commit() except IntegrityError as ex: return jsonify( errors.error_commit_error(ex)), statuscodes.HTTP_INTERNAL_ERROR return jsonify( responses.create_single_object_response( 'success', CakeSerializer(cake).data, "cake")), statuscodes.HTTP_OK # Unsupported Versions else: return jsonify(errors.error_incorrect_version( version)), statuscodes.HTTP_VERSION_UNSUPPORTED
def insert_student(version): """ Controller for API Function that inserts new cakes in the database @return: Response and HTTP code """ # GET POST DATA s_fname = request.form['fname'] s_lname = request.form['lname'] s_org_id = request.form['organization_id'] s_assign_id = request.form['assign_id'] s_reputation = request.form['reputation'] # API Version 1.X if math.floor(version) == 1: student = Student(fname=s_fname, lname=s_lname, org_id=s_org_id, assign_id=s_assign_id, reputation=s_reputation) db.session.add(student) try: db.session.commit() except IntegrityError as ex: return jsonify(errors.error_commit_error(ex)), statuscodes.HTTP_INTERNAL_ERROR student_schema = StudentSchema() result = student_schema.dump(student) return jsonify( responses.create_multiple_object_response('success', result.data, 'student')), statuscodes.HTTP_OK # Unsupported Versions else: return jsonify(errors.error_incorrect_version(version)), statuscodes.HTTP_VERSION_UNSUPPORTED
def insert_cake(version): """ Controller for API Function that inserts new cakes in the database @return: Response and HTTP code """ # GET POST DATA cakename = request.form['cakename'] baker = request.form['baker'] price = request.form['price'] # API Version 1.X if math.floor(version) == 1: cake = Cake(cakename, baker, price) db.session.add(cake) try: db.session.commit() except IntegrityError as ex: return jsonify( errors.error_commit_error(ex)), statuscodes.HTTP_INTERNAL_ERROR return jsonify( responses.create_multiple_object_response( 'success', CakeSerializer(cake).data, 'cakes')), statuscodes.HTTP_OK # Unsupported Versions else: return jsonify(errors.error_incorrect_version( version)), statuscodes.HTTP_VERSION_UNSUPPORTED
def delete_student(version, student_id): """ Controller for API Function that deletes a student by ID @param student_id: student id @return: Response and HTTP code """ # API Version 1.X if math.floor(version) == 1: student = Student.query.filter_by(id=student_id).first() if student is None: return jsonify(errors.error_object_not_found()), statuscodes.HTTP_NOT_FOUND db.session.delete(student) try: db.session.commit() except IntegrityError as ex: return jsonify(errors.error_commit_error(ex)), statuscodes.HTTP_INTERNAL_ERROR student_schema = StudentSchema() result = student_schema.dump(student) return jsonify( responses.create_single_object_response('success', result.data, "student")), statuscodes.HTTP_OK # Unsupported Versions else: return jsonify(errors.error_incorrect_version(version)), statuscodes.HTTP_VERSION_UNSUPPORTED
def insert_assignment(version): """ Controller for API Function that inserts new cakes in the database @return: Response and HTTP code """ # GET POST DATA assign_name = request.form['name'] # API Version 1.X if math.floor(version) == 1: assignment = Assignment(name=assign_name) db.session.add(assignment) try: db.session.commit() except IntegrityError as ex: return jsonify( errors.error_commit_error(ex)), statuscodes.HTTP_INTERNAL_ERROR assign_schema = AssignmentSchema() result = assign_schema.dump(assignment) return jsonify( responses.create_multiple_object_response( 'success', result.data, 'assignment')), statuscodes.HTTP_OK # Unsupported Versions else: return jsonify(errors.error_incorrect_version( version)), statuscodes.HTTP_VERSION_UNSUPPORTED
def delete_org(version, org_id): """ Controller for API Function that gets an organization by ID @param org_id: org id @return: Response and HTTP code """ # API Version 1.X if math.floor(version) == 1: org = Organization.query.filter_by(id=org_id).first() if org is None: return jsonify( errors.error_object_not_found()), statuscodes.HTTP_NOT_FOUND db.session.delete(org) try: db.session.commit() except IntegrityError as ex: return jsonify( errors.error_commit_error(ex)), statuscodes.HTTP_INTERNAL_ERROR org_schema = OrganizationSchema() result = org_schema.dump(org) return jsonify( responses.create_single_object_response( 'success', result.data, "organization")), statuscodes.HTTP_OK # Unsupported Versions else: return jsonify(errors.error_incorrect_version( version)), statuscodes.HTTP_VERSION_UNSUPPORTED
def insert_org(version): """ Controller for API Function that inserts new organization in the database @return: Response and HTTP code """ # GET POST DATA org_name = request.form['name'] org_address = request.form['address'] # API Version 1.X if math.floor(version) == 1: org = Organization(name=org_name, address=org_address) db.session.add(org) try: db.session.commit() except IntegrityError as ex: return jsonify( errors.error_commit_error(ex)), statuscodes.HTTP_INTERNAL_ERROR org_schema = OrganizationSchema() result = org_schema.dump(org) return jsonify( responses.create_multiple_object_response( 'success', result.data, 'organization')), statuscodes.HTTP_OK # Unsupported Versions else: return jsonify(errors.error_incorrect_version( version)), statuscodes.HTTP_VERSION_UNSUPPORTED
def insert_score(version): """ Controller for API Function that inserts new score in the database @return: Response and HTTP code """ # GET POST DATA student_id = request.form['student_id'] assignment_id = request.form['assignment_id'] organization_id = request.form['organization_id'] task_id = request.form['task_id'] review_type = request.form['review_type'] score = request.form['score'] max_score = request.form['max_score'] # API Version 1.X if math.floor(version) == 1: score = Score(student_id=student_id, assignment_id=assignment_id, organization_id=organization_id, task_id=task_id, review_type=review_type, score=score, max_score=max_score) db.session.add(score) try: db.session.commit() except IntegrityError as ex: return jsonify( errors.error_commit_error(ex)), statuscodes.HTTP_INTERNAL_ERROR score_schema = ScoreSchema() result = score_schema.dump(score) return jsonify( responses.create_multiple_object_response( 'success', result.data, 'score')), statuscodes.HTTP_OK # Unsupported Versions else: return jsonify(errors.error_incorrect_version( version)), statuscodes.HTTP_VERSION_UNSUPPORTED