Controller of PWeb CRUD Example
Land all the request from browser to this controller and it’s action.
Form Controller
from boot.service.person_service import PersonService
from pweb import Blueprint
person_url_prefix = "/person"
person_controller = Blueprint(
"person_controller",
__name__,
url_prefix=person_url_prefix
)
person_service = PersonService()
@person_controller.route("/create", methods=['POST', 'GET'])
def create():
return person_service.create()
@person_controller.route("/details/<int:id>", methods=['GET'])
def details(id: int):
return person_service.details(id)
@person_controller.route("/update/<int:id>", methods=['POST', 'GET'])
def update(id: int = None):
return person_service.update(id)
@person_controller.route("/delete/<int:id>", methods=['GET'])
def delete(id: int):
return person_service.delete(id)
@person_controller.route("/", methods=['GET'])
@person_controller.route("/list", methods=['GET'])
def list():
return person_service.list()
API Controller
from boot.dto.person_dto import PersonCreateDTO, PersonDetailsDTO, PersonUpdateDTO
from boot.service.person_api_service import PersonApiService
from pweb import Blueprint
from pweb_form_rest import pweb_endpoint, pweb_paginate_endpoint
person_api_url_prefix = "/api/v1/person"
person_api_controller = Blueprint(
"person_api_controller",
__name__,
url_prefix=person_api_url_prefix
)
person_api_service = PersonApiService()
@person_api_controller.route("/create", methods=['POST'])
@pweb_endpoint(request_obj=PersonCreateDTO, pweb_message_response=True)
def create():
return person_api_service.create()
@person_api_controller.route("/details/<int:id>", methods=['GET'])
@pweb_endpoint(response_obj=PersonDetailsDTO)
def details(id: int):
return person_api_service.details(id)
@person_api_controller.route("/update", methods=['POST'])
@pweb_endpoint(request_obj=PersonUpdateDTO, pweb_message_response=True)
def update():
return person_api_service.update()
@person_api_controller.route("/delete/<int:id>", methods=['DELETE'])
@pweb_endpoint()
def delete(id: int):
return person_api_service.delete(id)
@person_api_controller.route("/list", methods=['GET'])
@pweb_paginate_endpoint(response_obj=PersonDetailsDTO)
def list():
return person_api_service.list()