You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.4 KiB
Python
80 lines
2.4 KiB
Python
7 years ago
|
from apistar import Include, Route, http, Command
|
||
7 years ago
|
from apistar.backends import sqlalchemy_backend
|
||
|
from apistar.backends.sqlalchemy_backend import Session
|
||
7 years ago
|
from apistar.frameworks.wsgi import WSGIApp as App
|
||
|
from apistar.handlers import docs_urls, static_urls
|
||
7 years ago
|
from apistar.interfaces import Router
|
||
7 years ago
|
|
||
7 years ago
|
from render import JSONRenderer
|
||
|
from commands import commands
|
||
|
from models.schema import Base, Cookie
|
||
7 years ago
|
|
||
|
|
||
|
def get_cookies(session: Session):
|
||
|
cookies = session.query(Cookie).all()
|
||
7 years ago
|
# result = [{"id": cookie.id,
|
||
|
# "created_date": cookie.created_date,
|
||
|
# "modified_date": cookie.modified_date,
|
||
|
# "name": cookie.name,
|
||
|
# "recipe_url": cookie.recipe_url,
|
||
|
# "sku": cookie.sku,
|
||
|
# "qoh": cookie.qoh,
|
||
|
# "unit_cost": cookie.unit_cost}
|
||
|
# for cookie in cookies]
|
||
|
result = [cookie.to_dict() for cookie in cookies]
|
||
7 years ago
|
|
||
|
return result
|
||
|
|
||
7 years ago
|
|
||
|
def get_cookie(session: Session, id):
|
||
|
cookie = session.query(Cookie).filter_by(id=id).one_or_none()
|
||
|
if cookie is None:
|
||
|
msg = {"error": "404 Not Found"}
|
||
|
return http.Response(msg, status=404)
|
||
|
return cookie.to_dict()
|
||
|
|
||
|
|
||
7 years ago
|
# @annotate(authentication=[JWTAuthentication()])
|
||
7 years ago
|
def create_cookie(session: Session, json_data: http.RequestData, route: Router):
|
||
|
cookie = Cookie(name=json_data['name'],
|
||
|
recipe_url=json_data['recipe_url'],
|
||
|
sku=json_data['sku'],
|
||
|
qoh=json_data['qoh'],
|
||
|
unit_cost=json_data['unit_cost'])
|
||
|
session.add(cookie)
|
||
7 years ago
|
session.commit()
|
||
7 years ago
|
headers = {'Location': route.reverse_url('get_cookie', dict(id=cookie.id))}
|
||
|
return http.Response(cookie.to_dict(), status=201, headers=headers)
|
||
|
|
||
|
|
||
7 years ago
|
routes = [
|
||
|
Route('/cookies', 'GET', get_cookies),
|
||
7 years ago
|
Route('/cookies', 'POST', create_cookie),
|
||
|
Route('/cookies/{id}', 'GET', get_cookie),
|
||
7 years ago
|
Include('/docs', docs_urls),
|
||
|
Include('/static', static_urls)
|
||
|
]
|
||
|
|
||
|
settings = {
|
||
|
'DATABASE': {
|
||
|
'URL': 'postgresql://:@localhost/apistar',
|
||
|
'METADATA': Base.metadata
|
||
7 years ago
|
},
|
||
7 years ago
|
'RENDERERS': [JSONRenderer()],
|
||
|
'JWT': {
|
||
|
'SECRET': 'thisisasecret'
|
||
|
},
|
||
|
'BCRYPT_LOG_ROUNDS': 13
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
commands = sqlalchemy_backend.commands + commands
|
||
|
|
||
7 years ago
|
app = App(routes=routes,
|
||
|
settings=settings,
|
||
7 years ago
|
commands=commands,
|
||
7 years ago
|
components=sqlalchemy_backend.components
|
||
|
)
|
||
7 years ago
|
|
||
|
if __name__ == "__main__":
|
||
7 years ago
|
app.main()
|