add skills seeds and implement api

This commit is contained in:
weeman 2021-06-07 17:52:14 +02:00
parent 802f227676
commit 5321144dc0
Signed by untrusted user: weeman
GPG Key ID: 34F0524D4DA694A1
6 changed files with 43 additions and 3 deletions

View File

@ -13,6 +13,7 @@
cp env.dev .env cp env.dev .env
pipenv install pipenv install
pipenv shell pipenv shell
export FLASK_APP=app.py
flask db upgrade flask db upgrade
flask run flask run
``` ```

2
app.py
View File

@ -1,9 +1,7 @@
import os import os
from datetime import datetime
from dotenv import load_dotenv, find_dotenv from dotenv import load_dotenv, find_dotenv
from flask import Flask from flask import Flask
from flask import request
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate from flask_migrate import Migrate

9
ki/commands.py Normal file
View File

@ -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()

View File

@ -79,6 +79,9 @@ class Skill(db.Model):
secondary=user_skill_table, secondary=user_skill_table,
back_populates="skills") back_populates="skills")
def to_dict(self):
return {"id": self.id, "name": self.name}
class Language(db.Model): class Language(db.Model):
__tablename__ = "language" __tablename__ = "language"

View File

@ -1 +1 @@
from ki import models, routes from ki import models, commands, routes

View File

@ -1,5 +1,34 @@
from flask import request
from ki.models import Skill
from app import app 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("/") @app.route("/")
def hello_world(): def hello_world():
return "KI" 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