add skills seeds and implement api
This commit is contained in:
parent
802f227676
commit
5321144dc0
@ -13,6 +13,7 @@
|
||||
cp env.dev .env
|
||||
pipenv install
|
||||
pipenv shell
|
||||
export FLASK_APP=app.py
|
||||
flask db upgrade
|
||||
flask run
|
||||
```
|
||||
|
2
app.py
2
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
|
||||
|
||||
|
9
ki/commands.py
Normal file
9
ki/commands.py
Normal 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()
|
@ -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"
|
||||
|
@ -1 +1 @@
|
||||
from ki import models, routes
|
||||
from ki import models, commands, routes
|
||||
|
29
ki/routes.py
29
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
|
||||
|
Loading…
Reference in New Issue
Block a user