Hello, and welcome to today's lesson! Today, we are going to dive into the world of managing product reviews and applying data aggregation in practice. We will start with a relatively simple Starter Task to set up our codebase, and then gradually build up to a more complex solution involving data aggregation. Let's jump in!
For our starter task, we'll lay the foundation by implementing basic operations for managing product reviews. The Review struct encapsulates the details of a product review. Here's a breakdown of the fields:
text string— The textual content of the review.rating int— The rating score of the review, ranging from 1 to 5.flagged bool— A boolean indicating whether the review is flagged as inappropriate.
The individual reviews are managed via a ReviewManager. These are the functions we'll implement in our ReviewManager for the starter task:
-
addReview(productId string, reviewId string, reviewText string, rating int) bool— Adds a review to the product specified byproductId. If a review withreviewIdalready exists, it updates the existing review. Returnstrueif the review was added or updated successfully,falseotherwise. Newly added reviews have theflaggedattribute set tofalseinitially. -
getReview(productId string, reviewId string) (Review, bool)— Returns the review details (, , and fields) for the review specified by under the given . The second return value indicates if the review was found.
Let's look at the code that implements these functionalities:
This code establishes the foundational functions needed for managing product reviews within a ReviewManager. The addReview function allows for adding a new review or updating an existing one, ensuring each review contains valid rating values between 1 and 5. The function retrieves the review details for a specific product, returning a boolean to indicate whether the product or review exists. The function handles the removal of specific reviews, and if no other reviews remain for a product, the product itself is removed from the list.
With our basic review management system in place, we will now introduce new functions to handle more complex operations, such as flagging inappropriate reviews and aggregating review data for a specific product.
Here are the new functions we’ll add:
-
flagReview(productId string, reviewId string) bool— This function flags a specific review as inappropriate for a given product. Returnstrueif the review was successfully flagged,falseotherwise. -
aggregateReviews(productId string) (AggregatedData, bool)— This function aggregates review data for a given product, providing statistics such as the total number of reviews, the number of flagged reviews, average rating, and review texts excluding flagged ones. The second return value is a boolean indicating if there was data to aggregate.
First, let's add functionality to flag a review:
In this step, we are adding the flagReview function to our ReviewManager. This function allows users to mark a specific review as inappropriate. It checks whether the product and review exist in our data structures, and if they do, it sets the flagged attribute of the review to true. Flagging is important for maintaining review quality.
Next, we will implement the function to aggregate reviews:
Let's breakdown this code:
- The
aggregateReviewsfunction is added toReviewManagerto collect review statistics for a product. - It utilizes the
AggregatedDatastruct to store total reviews, flagged reviews, average rating, and non-flagged review texts. - The function ensures the product exists and has reviews to aggregate.
- It iterates over reviews, updating total and flagged review counts, and calculates the total rating for non-flagged reviews.
Great job! Today, you have learned how to manage product reviews and apply data aggregation using Go. We started with basic operations for adding, retrieving, and deleting reviews. Then, we extended our functionality to include flagging reviews and aggregating review data. This gradual build-up demonstrates how to enhance features incrementally and handle more complex data aggregation tasks.
Feel free to practice solving similar challenges to strengthen your skills further. Keep coding, and see you in the next lesson!
