Skip to content

Commit

Permalink
refactor: rename api/languages to langs
Browse files Browse the repository at this point in the history
  • Loading branch information
20cents committed Oct 25, 2024
1 parent d4f2e99 commit f3beef6
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions backend/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,42 +623,41 @@ def returnAllcommunes():
return jsonify(communes), 200


@api.route("/api/languages", methods=["GET"])
def returnAllLanguages():
get_all_languages = models.Lang.query.all()
languages = models.LangSchema(many=True).dump(get_all_languages)
return jsonify(languages), 200
@api.route("/api/langs", methods=["GET"])
def returnAllLangs():
get_all_langs = models.Lang.query.all()
langs = models.LangSchema(many=True).dump(get_all_langs)
return jsonify(langs), 200


@api.route("/api/languages", methods=["POST"])
@api.route("/api/langs", methods=["POST"])
@fnauth.check_auth(6)
def add_languages():
data = request.get_json()
def add_langs():
lang = request.get_json()
try:
get_all_existing_languages = models.Lang.query.all()
languages = {t.id: t for t in get_all_existing_languages}
for lang in data:
if lang["id"] not in languages:
lang_obj = models.Lang(
id=lang["id"],
label=lang["label"],
is_published=lang["is_published"],
is_default=lang["is_default"],
)
db.session.add(lang_obj)
get_all_existing_langs = models.Lang.query.all()
langs = {t.id: t for t in get_all_existing_langs}
if lang["id"] not in langs:
lang_obj = models.Lang(
id=lang["id"],
label=lang["label"],
is_published=lang["is_published"],
is_default=lang["is_default"],
)
db.session.add(lang_obj)

db.session.commit()

except Exception as exception:
db.session.rollback()
return jsonify({"error": str(exception)}), 400

return jsonify("languages added")
return jsonify("langs added")


@api.route("/api/language/<string:id>", methods=["PATCH"])
@api.route("/api/langs/<string:id>", methods=["PATCH"])
@fnauth.check_auth(2)
def update_language(id):
def update_lang(id):
data = request.get_json()
try:
models.Lang.query.filter_by(id=id).update(data)
Expand All @@ -667,20 +666,20 @@ def update_language(id):
db.session.rollback()
return jsonify({"error": str(exception)}), 400

return jsonify("language updated"), 200
return jsonify("lang updated"), 200


@api.route("/api/language/<string:id>", methods=["DELETE"])
@api.route("/api/langs/<string:id>", methods=["DELETE"])
@fnauth.check_auth(6)
def delete_language(id):
def delete_lang(id):
try:
models.Lang.query.filter_by(id=id).delete()
db.session.commit()
except Exception as exception:
db.session.rollback()
return jsonify({"error": str(exception)}), 400

return jsonify("language deleted"), 200
return jsonify("lang deleted"), 200


@api.route("/api/logout", methods=["GET"])
Expand Down

0 comments on commit f3beef6

Please sign in to comment.