Skip to content

Commit

Permalink
Add routes logic for users and articles (#108)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
atxr authored May 23, 2023
1 parent 33e6c97 commit 1a4de6c
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 23 deletions.
31 changes: 8 additions & 23 deletions backend/app.py
Original file line number Diff line number Diff line change
@@ -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/<id>', methods=['GET'])
def fetch_article(id):
return jsonify(dummy_article)
app.register_blueprint(articles, url_prefix="/api/articles")
app.register_blueprint(users, url_prefix="/api/users")
42 changes: 42 additions & 0 deletions backend/controllers/articleController.py
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions backend/controllers/dummy_data.py
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"password": "123456",
"_id": "1",
}
20 changes: 20 additions & 0 deletions backend/controllers/userController.py
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions backend/routes/articles.py
Original file line number Diff line number Diff line change
@@ -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("/<id>", "get_article", get_article, methods=["GET"])
articles.add_url_rule("/", "post_article", post_article, methods=["POST"])
articles.add_url_rule("/<id>", "delete_article", delete_article, methods=["DELETE"])
articles.add_url_rule(
"/by-author/<author>",
"get_article_by_author",
get_article_by_author,
methods=["GET"],
)

# Not implemented yet in the frontend
# articles.add_url_rule(put_article, "/<id>", methods=["PUT"])
11 changes: 11 additions & 0 deletions backend/routes/users.py
Original file line number Diff line number Diff line change
@@ -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, "/<id>", methods=["GET"])
# users.add_url_rule(put_user, "/<id>", methods=["PUT"])

0 comments on commit 1a4de6c

Please sign in to comment.