From 1a4de6cc8abe86519bac7f8ab080fe08611179e6 Mon Sep 17 00:00:00 2001 From: Alexandre Tullot Date: Tue, 23 May 2023 22:17:52 +0200 Subject: [PATCH] Add routes logic for users and articles (#108) * Add article logic Add basis for the article routes The functions are not implemented yet * Add user logic Add basis (functions not implemented) for user routes * Fix missing id parameter * Add missing fixme --- backend/app.py | 31 +++++------------ backend/controllers/articleController.py | 42 ++++++++++++++++++++++++ backend/controllers/dummy_data.py | 20 +++++++++++ backend/controllers/userController.py | 20 +++++++++++ backend/routes/articles.py | 18 ++++++++++ backend/routes/users.py | 11 +++++++ 6 files changed, 119 insertions(+), 23 deletions(-) create mode 100644 backend/controllers/articleController.py create mode 100644 backend/controllers/dummy_data.py create mode 100644 backend/controllers/userController.py create mode 100644 backend/routes/articles.py create mode 100644 backend/routes/users.py diff --git a/backend/app.py b/backend/app.py index bba16b1..81845de 100644 --- a/backend/app.py +++ b/backend/app.py @@ -1,29 +1,14 @@ -from flask import Flask, jsonify +from flask import Flask +from routes.articles import articles +from routes.users import users app = Flask(__name__) -dummy_article = { - 'author':'ctmbl', - 'title':'My dummy article', - 'body':'This is a dummy article hardcoded to build the backend', - '_id':"1" - } -dummy_article_2 = { - 'author':'ctmbl', - 'title':'My dummy article 2', - 'body':'This is a dummy article 2', - '_id':"2" - } +@app.route("/") +def get_main(): + return "iscsc.fr backend is running" -@app.route('/') -def root(): - return 'iscsc.fr backend is running' -@app.route('/api/articles', methods=['GET']) -def fetch_all_articles(): - return jsonify([dummy_article, dummy_article_2]) - -@app.route('/api/articles/', methods=['GET']) -def fetch_article(id): - return jsonify(dummy_article) \ No newline at end of file +app.register_blueprint(articles, url_prefix="/api/articles") +app.register_blueprint(users, url_prefix="/api/users") diff --git a/backend/controllers/articleController.py b/backend/controllers/articleController.py new file mode 100644 index 0000000..0f049ed --- /dev/null +++ b/backend/controllers/articleController.py @@ -0,0 +1,42 @@ +from flask import jsonify +from .dummy_data import * + +# TODO: Implement auth middleware logic to check if user is logged in + + +# TODO: fetch one article from database +def get_article(id): + if id == "1": + return jsonify(dummy_article), 200 + elif id == "2": + return jsonify(dummy_article_2), 200 + else: + return "", 404 + + +# TODO: create one article +def post_article(): + return jsonify({"success": True}), 200 + + +# TODO: modify one article in the database +def put_article(): + return jsonify({"success": True}), 200 + + +# TODO: delete one article from database +def delete_article(id): + return jsonify({"success": True}), 200 + + +# TODO: fetch all articles from database +def get_all_articles(): + return jsonify([dummy_article, dummy_article_2]), 200 + + +# TODO: fetch one article from database by author +def get_article_by_author(author): + if author == "ctmbl": + return jsonify([dummy_article, dummy_article_2]), 200 + else: + return "", 404 diff --git a/backend/controllers/dummy_data.py b/backend/controllers/dummy_data.py new file mode 100644 index 0000000..92bbc2a --- /dev/null +++ b/backend/controllers/dummy_data.py @@ -0,0 +1,20 @@ +dummy_article = { + "author": "ctmbl", + "title": "My dummy article", + "body": "This is a dummy article hardcoded to build the backend", + "_id": "1", +} + +dummy_article_2 = { + "author": "ctmbl", + "title": "My dummy article 2", + "body": "This is a dummy article 2", + "_id": "2", +} + +dummy_user = { + "name": "atxr", + "email": "atxr@iscsc.fr", + "password": "123456", + "_id": "1", +} diff --git a/backend/controllers/userController.py b/backend/controllers/userController.py new file mode 100644 index 0000000..02b7f81 --- /dev/null +++ b/backend/controllers/userController.py @@ -0,0 +1,20 @@ +from flask import jsonify +from flask import request + + +# TODO: log existing user +def login(): + if ( + "name" in request.form + and "password" in request.form + and request.form["name"] == "atxr" + and request.form["password"] == "123456" + ): + return jsonify({"success": request.form["name"] == "atxr"}), 200 + else: + return jsonify({"success": False}), 401 + + +# TODO: create new user +def signup(): + return jsonify({"success": True}), 200 diff --git a/backend/routes/articles.py b/backend/routes/articles.py new file mode 100644 index 0000000..5f8fabe --- /dev/null +++ b/backend/routes/articles.py @@ -0,0 +1,18 @@ +from flask import Blueprint +from controllers.articleController import * + +articles = Blueprint("articles", "backend") # FIXME: don't hardcode backend + +articles.add_url_rule("/", "get_all_articles", get_all_articles, methods=["GET"]) +articles.add_url_rule("/", "get_article", get_article, methods=["GET"]) +articles.add_url_rule("/", "post_article", post_article, methods=["POST"]) +articles.add_url_rule("/", "delete_article", delete_article, methods=["DELETE"]) +articles.add_url_rule( + "/by-author/", + "get_article_by_author", + get_article_by_author, + methods=["GET"], +) + +# Not implemented yet in the frontend +# articles.add_url_rule(put_article, "/", methods=["PUT"]) diff --git a/backend/routes/users.py b/backend/routes/users.py new file mode 100644 index 0000000..770f150 --- /dev/null +++ b/backend/routes/users.py @@ -0,0 +1,11 @@ +from flask import Blueprint +from controllers.userController import * + +users = Blueprint("users", "backend") # FIXME: don't hardcode backend + +users.add_url_rule("/login", "login", login, methods=["POST"]) +users.add_url_rule("/signup", "signup", signup, methods=["POST"]) + +# Not implemented yet in the frontend +# users.add_url_rule(get_user, "/", methods=["GET"]) +# users.add_url_rule(put_user, "/", methods=["PUT"])