async def delete(self, answer_id): try: self.application.db.delete(self.object) self.application.db.commit() except SQLAlchemyError as error: self.application.db.rollback() raise InternalServerError('Unable to delete the answer.', error) self.set_status(204) self.finish()
async def put(self, answer_id): # Update basic properties data = json.loads(self.request.body.decode('utf-8')) update_by_property_list(['body'], data, self.object) # Commit in DB try: self.application.db.commit() except SQLAlchemyError as error: self.application.db.rollback() raise InternalServerError('Unable to update the answer.', error) # Returns response self.set_status(200) self.write({'data': self.object.to_dict()}) self.finish()
async def post(self): # Create data data = json.loads(self.request.body.decode('utf-8')) title = check_param(data, name='title', type_param='string', required=True) body = check_param(data, name='body', type_param='string', required=True) tags = check_param(data, name='tags', type_param='list', required=True) if len(tags) < 1: raise BadRequestError('At least one tag is required') # Add tag tags_to_add = [] for tag_str in tags: tag = self.application.db.query(Tag).filter_by( label=tag_str).first() if tag is None: tag = Tag(label=tag_str) self.application.db.add(tag) tags_to_add.append(tag) question = Question(title=title, body=body, user=self.user, tags=tags_to_add) self.application.db.add(question) # Commit in DB try: self.application.db.commit() except SQLAlchemyError as error: self.application.db.rollback() raise InternalServerError('Unable to create the question.', error) # Returns response self.set_status(201) self.write({'data': question.to_dict()}) self.finish()
async def get(self, question_id): # Get answers question = self.object.to_dict() answers = [answer.to_dict() for answer in self.object.answers] # Update view counter in DB try: # Increment view counter (Naive way) # TO IMPROVE self.object.view_counter = self.object.view_counter + 1 self.application.db.commit() except SQLAlchemyError as error: self.application.db.rollback() raise InternalServerError('Unable to create the question.', error) # Returns response self.set_status(200) self.write({'question': question, 'answers': answers}) self.finish()
async def post(self, answer_id): # Get data up_down = self.get_data() vote = self.get_vote('answer', answer_id) # Commit in DB try: # Update count value = self.get_count_offset(vote, up_down) self.update_vote('answer', answer_id, vote, up_down) self.answer.vote_counter = self.answer.vote_counter + value self.application.db.commit() except SQLAlchemyError as error: self.application.db.rollback() raise InternalServerError('Unable to toggle the vote.', error) # Returns response self.write({'data': self.answer.to_dict()}) self.set_status(200) self.finish()
async def post(self, question_id): # Create data data = json.loads(self.request.body.decode('utf-8')) body = check_param(data, name='body', type_param='string', required=True) answer = Answer(body=body, question_id=question_id, user=self.user) # Commit in DB try: self.application.db.add(answer) self.application.db.commit() except SQLAlchemyError as error: self.application.db.rollback() raise InternalServerError('Unable to create the answer.', error) # Returns response self.set_status(201) self.write({'data': answer.to_dict()}) self.finish()
async def put(self, question_id): # Update basic properties data = json.loads(self.request.body.decode('utf-8')) update_by_property_list(['title', 'body'], data, self.object) # Update tags if 'tags' in data: tags = check_param(data, name='tags', type_param='list', required=True) if len(tags) < 1: raise BadRequestError('At least one tag is required') # Add tag tags_to_add = [] for tag_str in tags: tag = self.application.db.query(Tag).filter_by( label=tag_str).first() if tag is None: tag = Tag(label=tag_str) self.application.db.add(tag) tags_to_add.append(tag) # Update property in question self.object.tags = tags_to_add # Commit in DB try: self.application.db.commit() except SQLAlchemyError as error: self.application.db.rollback() raise InternalServerError('Unable to update the question.', error) # Returns response self.set_status(200) self.write({'data': self.object.to_dict()}) self.finish()
async def post(self): # Create data data = json.loads(self.request.body.decode('utf-8')) firstname = check_param(data, name='firstname', type_param='string', required=True) lastname = check_param(data, name='lastname', type_param='string', required=True) email = check_param(data, name='email', type_param='string', required=True) password = check_param(data, name='password', type_param='string', required=True) hashed_password = hashlib.md5(password.encode('utf-8')).hexdigest() user = User(firstname=firstname, lastname=lastname, email=email, password=hashed_password) self.application.db.add(user) # Commit in DB try: self.application.db.commit() except SQLAlchemyError as error: self.application.db.rollback() raise InternalServerError('Unable to create the user.', error) # Returns response self.set_status(201) self.write({'data': user.to_dict()}) self.finish()