def put(self, questionId, answerId): parser = reqparse.RequestParser() """collecting arguments""" parser.add_argument('content', required=True) parser.add_argument('token', location='headers') """getting specific arguments""" argument = parser.parse_args() content = argument['content'] token = argument['token'] if not token: return make_response(jsonify({'message': 'token missing'}), 400) """ implementing token decoding""" decoded = decode_token(token) if decoded["status"] == "Failure": return make_response(jsonify({"message": decoded["message"]}), 401) try: """insert into the database""" db.update_answer_data(answerId, questionId, content) return make_response( jsonify({'message': 'Answer updated successfully'}), 201) except psycopg2.DatabaseError as e: if db.connection: db.connection.rollback() print(e)
def delete(self, questionId): parser = reqparse.RequestParser() parser.add_argument('token', location='headers') argument = parser.parse_args() token = argument['token'] if not token: return make_response(jsonify({'message': 'token missing'}), 400) """ implementing token decoding""" decoded = decode_token(token) if decoded["status"] == "Failure": return make_response(jsonify({"message": decoded["message"]}), 401) owner = decoded["username"] # return owner userid_via_userstable = db.get_by_parameter('userstable', 'username', owner) user_dict = dict() """get a user_id """ user_dict["user_id"] = userid_via_userstable[2] owner_id = user_dict["user_id"] try: if db.delete_question(owner_id, questionId): return {"message": "delete successful"}, 202 return {"message": "delete error"}, 400 except psycopg2.DatabaseError as e: if db.connection: db.connection.rollback() print(e)
def put(self, entryId): parser = reqparse.RequestParser() """collecting args""" parser.add_argument('title', type=str, required=True) parser.add_argument('content', type=str, required=True) parser.add_argument('date', type=str, required=True) parser.add_argument('token', location='headers') """getting specific args""" args = parser.parse_args() title = args['title'] content = args['content'] date = args['date'] token = args['token'] if not token: return make_response(jsonify({'message': 'token missing'}), 400) """ implementing token decoding""" decoded = decode_token(token) if decoded["status"] == "Failure": return make_response(jsonify({"message": decoded["message"]}), 401) try: conn = configconnection() cur = conn.cursor() cur.execute( "UPDATE entries SET title=%s,content=%s,date=%s where id=%s", (title, content, date, entryId)) conn.commit() return make_response( jsonify({'message': 'entry updated successfully'}), 200) except: return make_response(jsonify({'message': 'entry not found'}), 404)
def get(self): parser = reqparse.RequestParser() """collecting args""" parser.add_argument('token', location='headers') """getting specific args""" args = parser.parse_args() token = args['token'] if not token: return make_response(jsonify({'message': 'token missing'}), 400) """ implementing token decoding""" decoded = decode_token(token) if decoded["status"] == "Failure": return make_response(jsonify({"message": decoded["message"]}), 401) conn = configconnection() cur = conn.cursor() cur.execute("SELECT * from entries") result = cur.fetchall() lst = [] for info in result: dic = {} dic["id"] = info[0] dic["title"] = info[1] dic["content"] = info[2] dic["date"] = info[3] lst.append(dic) return lst, 200
def insert_entries(self): parser = reqparse.RequestParser() """collecting args""" parser.add_argument('token', location='headers') parser.add_argument('title', type=str, required=True) parser.add_argument('content', type=str, required=True) parser.add_argument('date', type=str, required=True) """getting specific args""" args = parser.parse_args() title = args['title'] content = args['content'] date = args['date'] token = args['token'] if not token: return make_response(jsonify({'message': 'token missing'}), 400) """ implementing token decoding""" decoded = decode_token(token) if decoded["status"] == "Failure": return make_response(jsonify({"message": decoded["message"]}), 401) conn = configconnection() cur = conn.cursor() cur.execute("INSERT INTO entries (title,content,date) VALUES ('" + title + "','" + content + "','" + date + "')") conn.commit() return make_response( jsonify({'message': 'Created an entry successfully.'}), 201)
def post(self, questionId): parser = reqparse.RequestParser() """collecting arguments""" parser.add_argument('content', required=True) parser.add_argument('token', location='headers') """getting specific arguments""" argument = parser.parse_args() content = argument['content'] token = argument['token'] if not token: return make_response(jsonify({'message': 'token missing'}), 400) """ implementing token decoding""" decoded = decode_token(token) if decoded["status"] == "Failure": return make_response(jsonify({"message": decoded["message"]}), 401) try: """Query database to check whether answer exits""" check_answer = db.get_by_parameter('answerstable', 'content', content) if check_answer: return make_response( jsonify({'message': 'This answer already exists'}), 409) if not content: return make_response( jsonify({ 'message': 'The content is needed for this answer to be posted' }), 400) """get a user via the question """ user_via_question = db.get_by_parameter('questionstable', 'questionid', questionId) question_dict = dict() """get a user id """ question_dict["user_id"] = user_via_question[1] user_id = question_dict["user_id"] """insert into the database""" db.insert_answer_data(user_id, questionId, content) return make_response( jsonify({'message': 'Answer created successfully'}), 201) except psycopg2.DatabaseError as e: if db.connection: db.connection.rollback() print(e)
def post(self): parser = reqparse.RequestParser() """collecting arguments""" parser.add_argument('title', required=True) parser.add_argument('body', type=str, required=True) parser.add_argument('tag', type=str, required=True) parser.add_argument('token', location='headers') """getting specific arguments""" argument = parser.parse_args() title = str(argument['title']).strip() body = argument['body'] tag = argument['tag'] token = argument['token'] if not token: return make_response(jsonify({'message': 'token missing'}), 400) """ implementing token decoding""" decoded = decode_token(token) if decoded["status"] == "Failure": return make_response(jsonify({"message": decoded["message"]}), 401) """Query database to check whether question exits""" check_question = db.get_by_parameter('questionstable', 'title', title) if check_question: return make_response( jsonify({'message': 'This question already exists'}), 409) """checking whether the empty title feild""" if not title: return make_response( jsonify({ 'message': 'The title is needed for this question to be posted' }), 400) """checking whether the empty body feild""" if not body: return make_response( jsonify({ 'message': 'Body is needed for this question to be posted' }), 400) """checking whether the empty tag feild""" if not tag: return make_response( jsonify({ 'message': 'Tag is needed for this question to be posted' }), 400) """ implementing token decoding""" try: """insert into the database""" db.insert_question_data(1, title, body, tag) return make_response( jsonify({'message': 'Question created successfully'}), 201) except psycopg2.DatabaseError as e: if db.connection: db.connection.rollback() print(e)