Managing Product Reviews and Data Aggregation
Introduction
Hello, and welcome to the lesson! In this session, we’ll dive into managing product reviews and applying data aggregation in practice. We’ll start with a simple setup task to establish our foundation, then build toward a more complex solution involving data aggregation. Let’s get started!
Starter Task: Methods and Their Definitions
For our starter task, we’ll set up the basic operations needed to manage product reviews. Here are the essential methods we’ll implement:
- add_review(product_id, review_id, review_text, rating) — Adds a review for a specified
product_id. If a review withreview_idalready exists, it updates the review. Returnstrueif it adds or updates a review successfully; otherwise, it fails if the rating is not between 1 and 5. - get_review(product_id, review_id) — Returns the details of a specific review (review_text, rating, and flagged status) for a given
product_id. If the review or product doesn’t exist, returnsnil. - delete_review(product_id, review_id) — Deletes a specific review under the given
product_id. Returnstrueif successfully deleted,falseotherwise.
Implementing Basic Review Management
To start, we’ll create a ReviewManager class that manages all product reviews within a hash structure, @products, where each key is a product_id, and each value is a collection of reviews associated with that product.
In this initial setup, the initialize method creates an empty hash, @products, to store all product reviews in the system.
Adding and Updating Reviews with add_review
The first method, add_review, will allow us to add a new review or update an existing one for a specific product. This method ensures that each review contains a valid rating between 1 and 5.
In add_review, we start by validating the rating to ensure it’s between 1 and 5. If the rating is valid, we initialize the product in @products (if it doesn’t already exist) and then add or update the review under the specified review_id. This method returns true if the review is successfully added or updated.
