-
Notifications
You must be signed in to change notification settings - Fork 230
-
So I'm using this template for my project - for context, I'm an experienced C# dev, but relatively new to Python.
I'm trying to add more complex functionality to my app than basic CRUD operations - for example, with the standard boilerplate, imagine if you wanted to make it so that when the POST /{username}/post endpoint is called (calling the write_post function) that the post is written to the database as per the boilerplate, but that the system then calls an external API to ask the external API to check whether there's any posts in their data which have similar text.
Currently, I know that I could put this in the write_post function in posts.py, but my C# background is telling me that this should really be in a separate class (single responsibility principle!) - for example a post_service class. However, crud_posts would seem to currently fill the niche that post_service would set in the project, so I was wondering if there's an elegant way to extend the FastCRUD operations?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
Hey @Harry-Tom!
There are a few ways you could approach this, depending on whether you want to modify the endpoint generation or just extend the database interactions:
1. Use a Custom EndpointCreator
- You can define a custom
EndpointCreatorto handle extra logic before or after database interactions. This way, FastCRUD handles the database side, while your custom endpoints take care of things like calling external APIs.
2. Create a PostService Class
- A dedicated
PostServiceclass could encapsulate your extra logic while still using FastCRUD for database interactions.
3. Extend FastCRUD with a PostCRUD Subclass
- You can subclass FastCRUD to create a
PostCRUDclass and override methods to inject your own logic. FastCRUD supports method chaining, so you can dynamically extend or modify queries.
Currently there isn't a more elegant way to do this, but it's on our list for some time.
Beta Was this translation helpful? Give feedback.