0
0
Flaskframework~30 mins

Repository pattern for data access in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Repository Pattern for Data Access in Flask
📖 Scenario: You are building a simple Flask web app to manage books in a library. To keep your code clean and organized, you want to separate how you access data from the rest of your app. This is where the repository pattern helps.Think of the repository as a friendly librarian who knows exactly how to find, add, or remove books from the library database. Your app will ask the librarian instead of searching the shelves directly.
🎯 Goal: Build a Flask app that uses the repository pattern to manage book data. You will create a list of books, set up a repository class to handle data access, implement methods to get all books and add a new book, and finally connect this repository to a Flask route to display the books.
📋 What You'll Learn
Create a list of book dictionaries with exact titles and authors
Create a repository class called BookRepository to manage book data
Implement a method get_all_books that returns all books
Implement a method add_book that adds a new book to the list
Create a Flask route /books that uses the repository to show all books
💡 Why This Matters
🌍 Real World
The repository pattern helps keep your Flask app organized by separating data access from business logic. This makes your code easier to maintain and test.
💼 Career
Many companies use the repository pattern in web development to build scalable and clean applications. Knowing this pattern is valuable for backend and full-stack developer roles.
Progress0 / 4 steps
1
DATA SETUP: Create the initial book list
Create a list called books with these exact dictionaries: {'title': '1984', 'author': 'George Orwell'}, {'title': 'To Kill a Mockingbird', 'author': 'Harper Lee'}, and {'title': 'The Great Gatsby', 'author': 'F. Scott Fitzgerald'}.
Flask
Need a hint?

Remember to create a list named books with exactly three dictionaries inside it.

2
CONFIGURATION: Define the BookRepository class
Create a class called BookRepository with an __init__ method that takes a parameter books and assigns it to self.books.
Flask
Need a hint?

Define a class named BookRepository with an __init__ method that stores the passed books list in self.books.

3
CORE LOGIC: Add methods to get and add books
Inside the BookRepository class, add a method get_all_books that returns self.books. Also add a method add_book that takes a parameter book and appends it to self.books.
Flask
Need a hint?

Define two methods inside BookRepository: one to return all books, and one to add a new book to the list.

4
COMPLETION: Create Flask app and route using the repository
Import Flask and create an app instance called app. Instantiate BookRepository with the books list as repo. Create a route /books that returns a string listing all book titles from repo.get_all_books() separated by commas.
Flask
Need a hint?

Import Flask, create the app, instantiate the repository, and define a route that returns all book titles joined by commas.