-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
6 changed files
with
119 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) |