def post(self, note_id): args = parser.parse_args() title = args['title'] content = args['content'] lecturer = args['lecturer'] link = args['link'] course_id = args['course_id'] course_code = args['course_code'] english = args['english'] term_id = args['term_id'] note = Note().where('id', '=', note_id).first() if note.exists() is False: return response({ 'message': 'That note does not exist' }, 401) note.update({ 'title': title, 'content': content, 'lecturer': lecturer, 'link': link, 'course_id': course_id, 'course_code': course_code, 'english': english, 'term_id': term_id, 'slug': note.generateSlug(name=title) }) return response({ 'message': 'Note successfully updated!' }, 200)
def get(self): notes = Note().where().orderBy().get().data() for note in notes: user = User().where('id', note['user_id']).first() note['user'] = user.data() return response({ 'notes': notes }, 200)
def post(self, note_id): note = Note().where('id', '=', note_id).first() if note.exists(): note.delete() return response({ 'message': 'Note deleted successfully' }, 202) return response({ 'message': 'Note does not exist' }, 404)
def get(self, note_slug): note = Note().where('slug', note_slug).first().data() user = User().where('id', note['user_id']).first() note['user'] = user.data() comments = Comment().where([['type', '=', 'notes'], ['type_id', '=', note['id']]]).get() for comment in comments.data(): comment['user'] = User().where('id', comment['user_id']).first().data() note['comments'] = comments.data() return response({ 'notes': note }, 200)
def post(self, user_id): user_id = get_jwt_identity()['id'] user = User().where('id', user_id).first() if user.exists() is True: Comment().where('user_id', user_id).get().delete() GradeDistribution().where('user_id', user_id).get().delete() Lecturer().where('user_id', user_id).get().delete() Note().where('user_id', user_id).get().delete() user.delete() return response({'message': 'User deleted with success'}, 200) return response({'errors': ['User could not found!']}, 401)
def post(self, note_id): user_id = get_jwt_identity()['id'] note = Note().where([['id', '=', note_id], ['user_id', '=', user_id]]).first() if note.exists(): note.delete() return response({ 'message': 'Note deleted successfully' }, 202) return response({ 'message': 'Note does not exist or it is not yours to delete' }, 404)
def post(self, term_id): term = Term().where('id', '=', term_id).first() notes = Note().where('term_id', term_id).get() for note in notes: note.delete() grade_dists = GradeDistribution().where('term_id', term_id).get() for grade_dist in grade_dists: grade_dist.delete() if term.exists(): term.delete() return response({ 'message': 'Term deleted successfully' }, 202) return response({ 'message': 'Term does not exist' }, 404)
def post(self, course_id): course = Course().where('id', '=', course_id).first() notes = Note().where('course_id', course_id).get() for note in notes: note.delete() grade_dists = GradeDistribution().where('course_id', course_id).get() for grade_dist in grade_dists: grade_dist.delete() if course.exists(): course.delete() return response({ 'message': 'Course deleted successfully' }, 202) return response({ 'message': 'Course does not exist' }, 404)
def validate(self): user = User().where('id', self.ATTRIBUTES['user_id']).first() if user.exists() is False: self.setError("User not found in the database!") if self.ATTRIBUTES['type'] == 'lecturers': lecturer = Lecturer().where('id', self.ATTRIBUTES['type_id']).first() if lecturer.exists() is False: self.setError("Lecturer not found in the database!") elif self.ATTRIBUTES['type'] == 'notes': note = Note().where('id', self.ATTRIBUTES['type_id']).first() if note.exists() is False: self.setError("Note not found in the database!") else: self.setError("Comment type is not valid") if self.getErrors(): return False return True
def post(self, user_id): user = User().where('id', '=', user_id).first() print(user_id, file=sys.stderr) if user.exists(): Comment().where('user_id', user_id).get().delete() Event().where('user_id', user_id).get().delete() GradeDistribution().where('user_id', user_id).get().delete() lecturers = Lecturer().where('user_id', user_id).get() for lecturer in lecturers.data(): Comment().where([['type', '=', 'lecturers'], ['type_id', '=', lecturer['id']]]).get().delete() GradeDistribution().where('lecturer_id', '=', lecturer['id']).get().delete() lecturers.delete() notes = Note().where('user_id', user_id).get() for note in notes.data(): Comment().where([['type', '=', 'notes'], ['type_id', '=', note['id']]]).get().delete() notes.delete() user.delete() return response({ 'message': 'User deleted successfully' }, 202) return response({ 'message': 'User does not exist' }, 404)
def post(self): args = parser.parse_args() title = args['title'] content = args['content'] lecturer = args['lecturer'] link = args['link'] course_id = args['course_id'] course_code = args['course_code'] english = args['english'] term_id = args['term_id'] user_id = get_jwt_identity()['id'] note = Note() note.create({ 'title': title, 'content': content, 'lecturer': lecturer, 'link': link, 'course_id': course_id, 'course_code': course_code, 'english': english, 'term_id': term_id, 'user_id': user_id, 'slug': note.generateSlug(name=title) }) if note.validate() is False: return response({ 'errors': note.getErrors() }, 401) user = User().where('id', user_id).first() note.save() return response({ 'note': note.plus('user', user.data()).data() }, 200)