From 5321144dc0496e39848170c5f3a0bd223d691adf Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Mon, 7 Jun 2021 17:52:14 +0200 Subject: [PATCH] add skills seeds and implement api --- README.md | 1 + app.py | 2 -- ki/commands.py | 9 +++++++++ ki/models.py | 3 +++ ki/module.py | 2 +- ki/routes.py | 29 +++++++++++++++++++++++++++++ 6 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 ki/commands.py diff --git a/README.md b/README.md index e6ff4b5..0e4fab7 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ cp env.dev .env pipenv install pipenv shell +export FLASK_APP=app.py flask db upgrade flask run ``` diff --git a/app.py b/app.py index 2400afa..2bf8db0 100644 --- a/app.py +++ b/app.py @@ -1,9 +1,7 @@ import os -from datetime import datetime from dotenv import load_dotenv, find_dotenv from flask import Flask -from flask import request from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate diff --git a/ki/commands.py b/ki/commands.py new file mode 100644 index 0000000..6d1fa98 --- /dev/null +++ b/ki/commands.py @@ -0,0 +1,9 @@ +from ki.models import Skill +from app import app, db + + +@app.cli.command("seed") +def seed(): + db.session.add(Skill(id=1, name="PHP")) + db.session.add(Skill(id=2, name="Vue.js")) + db.session.commit() diff --git a/ki/models.py b/ki/models.py index f43df08..4bfc209 100644 --- a/ki/models.py +++ b/ki/models.py @@ -79,6 +79,9 @@ class Skill(db.Model): secondary=user_skill_table, back_populates="skills") + def to_dict(self): + return {"id": self.id, "name": self.name} + class Language(db.Model): __tablename__ = "language" diff --git a/ki/module.py b/ki/module.py index 8758679..7aa8b9a 100644 --- a/ki/module.py +++ b/ki/module.py @@ -1 +1 @@ -from ki import models, routes +from ki import models, commands, routes diff --git a/ki/routes.py b/ki/routes.py index 23cc546..616250f 100644 --- a/ki/routes.py +++ b/ki/routes.py @@ -1,5 +1,34 @@ +from flask import request + +from ki.models import Skill from app import app + +def models_to_list(models): + models_list = [] + + for model in models: + models_list.append(model.to_dict()) + + return models_list + + @app.route("/") def hello_world(): return "KI" + + +@app.route("/skills") +def get_skills(): + skills_query = Skill.query + + if "search" in request.args: + skills_query = skills_query.filter(Skill.name.startswith(request.args.get("search"))) + + skills = skills_query.order_by(Skill.name) \ + .limit(10) \ + .all() + + api_skills = models_to_list(skills) + response_data = {"skills": api_skills} + return response_data