PWeb Service
A Service is a component that encapsulates business logic and operations, typically providing specific functionalities that can be reused across different parts of an application. Services are often used to perform tasks such as data processing, calculations, external API calls, or interacting with databases.
Service for REST
class PersonRESTService:
rest_data_crud = RESTDataCRUD(model=Person)
def create(self):
return self.rest_data_crud.create(request_dto=PersonCreateDTO())
def details(self, model_id: int):
return self.rest_data_crud.details(model_id=model_id, response_dto=PersonDetailsDTO())
def update(self):
return self.rest_data_crud.update(request_dto=PersonUpdateDTO())
def list(self):
search_fields = []
return self.rest_data_crud.paginated_list(response_dto=PersonDetailsDTO(), search_fields=search_fields)
def delete(self, model_id: int):
return self.rest_data_crud.delete(model_id=model_id)
Service for SSR
class FormService:
form_data_crud: FormDataCRUD = FormDataCRUD(model=Member)
def create(self):
params = {"button": "Create", "action": url_for("form_controller.create")}
return self.form_data_crud.create(view_name="form/form", form=MemberCreateForm(), redirect_url=url_for("form_controller.list"), params=params, upload_path=AssetConfig.formFile)
def update(self, model_id: int):
params = {"button": "Update", "action": url_for("form_controller.update", id=model_id)}
return self.form_data_crud.update(view_name="form/form", model_id=model_id, update_form=MemberUpdateForm(), redirect_url=url_for("form_controller.list"), params=params, upload_path=AssetConfig.formFile)
def details(self, model_id: int):
return self.form_data_crud.details("form/details", model_id=model_id, redirect_url=url_for("form_controller.list"), display_from=MemberDetailsForm())
def delete(self, model_id: int):
return self.form_data_crud.delete(model_id=model_id, redirect_url=url_for("form_controller.list"))
def list(self):
search_fields: list = ["firstname", "lastname", "email", "technology"]
return self.form_data_crud.paginated_list(view_name="form/list", search_fields=search_fields)