def post(self): data = Chapter.parser.parse_args() chapter = ChapterModel(**data) try: chapter.save_to_db() except: return {"message": "An error occurred inserting the item."}, 500 return chapter.json(), 201
def post(self, course_id): data = parser.parse_args() data['course_id'] = course_id chapter = ChapterModel(**data) try: chapter.save() except: return { "message": "An error occurred while inserting Chapter." }, 500 update_steps_number(course_id) return chapter.json(), 201
def get(self, course_id): chapters = ChapterModel.find_by_course(course_id) if chapters is None: return {"message": "No chapters found for this course."}, 404 return [chapter.json() for chapter in chapters], 200
def get(self, course_id, chapter_id): chapter = ChapterModel.find_by_id(chapter_id) if chapter is None: return {"message": "No chapter found."}, 404 return chapter.json(), 200
def put(self, course_id, chapter_id): data = parser.parse_args() data['course_id'] = course_id chapter = ChapterModel.find_by_id(chapter_id) if chapter is None: new_chapter = ChapterModel(**data) try: new_chapter.save() return new_chapter.json(), 201 except: return { "message": "An error occurred while updating Chapter." }, 500 try: chapter.update(**data) except: return { "message": "An error occurred while updating Chapter." }, 500 return chapter.json(), 200
def delete(self, course_id, chapter_id): chapter = ChapterModel.find_by_id(chapter_id) if chapter is None: return {"message": "No chapter found."}, 404 try: chapter.delete() except: return { "message": "An error occurred while deleting Chapter." }, 500 update_steps_number(course_id) return {"message": "Chapter deleted."}, 200
def post(self): data = Chapter.parser.parse_args() chapter = ChapterModel(data["chapter_name"], data["book_id"], data["chapter_description"], data["chapter_order"]) try: chapter.chapter_creation = datetime.now() chapter.saveToDb() except: return {"message": "An error occured creating the chapter."}, 500 return chapter.json(), 201
def get(self, chapter_id): chapter = ChapterModel.findById(chapter_id) if chapter: return chapter.json(), 200 return {"message": "Chapter not found."}, 404
def delete(self): ChapterModel.delete_all() return {'message': 'All chapters deleted'}
def delete(self, chapter_id): chapter = ChapterModel.find_by_id(chapter_id) if chapter: chapter.delete_from_db() return {'message': 'Chapter deleted'}
def put(self, chapter_id): data = Chapter.parser.parse_args() chapter = ChapterModel.find_by_id(chapter_id) if chapter is None: chapter = ChapterModel(**data) else: chapter.book_id = data['book_id'] chapter.chapter_name = data['chapter_name'] chapter.chapter_description = data['chapter_description'] chapter.chapter_order = data['chapter_order'] chapter.chapter_update = datetime.now() chapter.save_to_db() return chapter.json()
def get(self, chapter_id): chapter = ChapterModel.find_by_id(chapter_id) if chapter: return chapter.json() return {'message': 'Chapter not found'}, 404