| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- import fastapi
- import fastapi.staticfiles
- import fastapi.responses
- import pathlib
- import os
- from . import *
- from .requests import *
- class server(fastapi.FastAPI):
- def __init__(self, config_file: str) -> None:
- super().__init__()
- location = pathlib.Path(config_file)
-
- self.__resources = config_loader(app_config) \
- .load(location) \
- .resources
-
- self.__static()
- self.__route_product()
- self.__route_users()
- self.__route_reservations()
- @property
- def resources(self) -> app_resources:
- return self.__resources
- @property
- def product_app(self) -> product_app:
- return self.__resources.product_app
- @property
- def users_app(self) -> users_app:
- return self.__resources.users_app
- @property
- def reservation_app(self) -> reservation_app:
- return self.__resources.reservation_app
- def __static(self) -> None:
- @self.get("/")
- async def root() -> str:
- with pathlib.Path("static/core.html").open() as core:
- return fastapi.responses.HTMLResponse(
- content = core.read(),
- status_code = 200
- )
-
- app_directory = fastapi.staticfiles.StaticFiles(
- directory = pathlib.Path("static/")
- )
- covers_directory = fastapi.staticfiles.StaticFiles(
- directory = self.resources.config.covers_path
- )
- self.mount(
- "/app",
- app_directory,
- name = "app_frontend"
- )
- self.mount(
- directory_image.server_path(),
- covers_directory,
- name = "covers"
- )
- def __route_reservations(self) -> None:
- @self.post("/reservations/user/email/{email}")
- async def reservations_email(
- email: str,
- reservation: reservation_request
- ) -> dict:
- body = reservation.dict()
- body["email"] = email
- return self.reservation_app.get_by_user(body)
- @self.post("/reservations/user/phone_number/{phone_number}")
- async def reservations_email(
- phone_number: str,
- reservation: reservation_request
- ) -> dict:
- body = reservation.dict()
- body["phone_number"] = phone_number
- return self.reservation_app.get_by_user(body)
- @self.post("/reservations/product/barcode/{barcode}")
- async def reservations_barcode(
- barcode: int,
- reservation: reservation_request
- ) -> dict:
- body = reservation.dict()
- body["target_barcode"] = barcode
- return self.reservation_app.get_by_product(body)
- @self.post("/reservations/product/name/{name}")
- async def reservations_name(
- name: str,
- reservation: reservation_request
- ) -> dict:
- body = reservation.dict()
- body["target_name"] = name
- return self.reservation_app.get_by_product(body)
- @self.post("/rent/product/barcode/{barcode}")
- async def rent_barcode(
- barcode: int,
- reservation: reservation_request
- ) -> dict:
- body = reservation.dict()
- body["target_barcode"] = barcode
-
- return self.reservation_app.rent_product(body)
- @self.post("/rent/product/name/{name}")
- async def rent_name(
- name: str,
- reservation: reservation_request
- ) -> dict:
- body = reservation.dict()
- body["target_name"] = name
-
- return self.reservation_app.rent_product(body)
- @self.post("/give_back/product/barcode/{barcode}")
- async def give_back_barcode(
- barcode: int,
- reservation: reservation_request
- ) -> dict:
- body = reservation.dict()
- body["target_barcode"] = barcode
- return self.reservation_app.give_back_product(body)
- @self.post("/give_back/product/name/{name}")
- async def give_back_barcode(
- name: str,
- reservation: reservation_request
- ) -> dict:
- body = reservation.dict()
- body["target_name"] = name
- return self.reservation_app.give_back_product(body)
- def __route_product(self) -> None:
- @self.get("/products")
- async def products() -> dict:
- return self.product_app.all()
- @self.get("/product/get/barcode/{barcode}")
- async def product_get_barcode(barcode: int) -> dict:
- return self.product_app.get_barcode(str(barcode))
- @self.get("/product/get/name/{name}")
- async def product_get_name(name: str) -> dict:
- return self.product_app.get_name(name)
- @self.get("/product/search/name/{name}")
- async def product_search_name(name: str) -> dict:
- return self.product_app.search_name(name)
- @self.get("/product/search/author/{author}")
- async def product_search_author(author: str) -> dict:
- return self.product_app.search_author(author)
- @self.get("/product/check/barcode/{barcode}")
- async def product_check_barcode(barcode: int) -> dict:
- return self.product_app.check_barcode(str(barcode))
- @self.get("/product/check/name/{name}")
- async def product_check_name(name: str) -> dict:
- return self.product_app.check_name(name)
- @self.post("/product/update/barcode/{barcode}")
- async def product_barcode_update(
- barcode: str,
- product: product_update_request
- ) -> dict:
- body = product.dict()
- body["target_barcode"] = barcode
- return self.product_app.update(body)
- @self.post("/product/update/name/{name}")
- async def product_name_update(
- name: str,
- product: product_update_request
- ) -> dict:
- body = product.dict()
- body["target_name"] = name
- return self.product_app.update(body)
- @self.post("/product/update/image/barcode/{barcode}")
- async def product_image_barcode_update(
- barcode: str,
- product_image: product_update_image_request
- ) -> dict:
- body = product_image.dict()
- body["target_barcode"] = barcode
- return self.product_app.update_image(body)
-
- @self.post("/product/update/image/name/{name}")
- async def product_image_barcode_update(
- name: str,
- product_image: product_update_image_request
- ) -> dict:
- body = product_image.dict()
- body["target_name"] = name
- return self.product_app.update_image(body)
- @self.delete("/product/barcode/{barcode}")
- async def product_barcode_delete(
- barcode: str,
- request: apikey_request
- ) -> dict:
- body = request.dict()
- body["target_barcode"] = barcode
- return self.product_app.delete(body)
- @self.delete("/product/name/{name}")
- async def product_name_delete(
- name: str,
- request: apikey_request
- ) -> dict:
- body = request.dict()
- body["target_name"] = name
- return self.product_app.delete(body)
- @self.post("/product/create")
- async def product_create(product: product_create_request) -> dict:
- return self.product_app.create(product.dict())
- def __route_users(self) -> None:
- @self.post("/user/login")
- async def user_login(user: user_login_request) -> dict:
- return self.users_app.login(user.nick, user.password)
-
- @self.post("/user")
- async def user_get(body: user_get_request) -> dict:
- return self.users_app.get(body.apikey)
-
- instance = server(os.environ["config_file"])
|