Managing Product Reviews in Go with Data Aggregation
Introduction
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!
Starter Task: Structures and Functions
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 (text,rating, andflaggedfields) for the review specified byreviewIdunder the givenproductId. The second return value indicates if the review was found. -
deleteReview(productId string, reviewId string) bool— Deletes the review specified byreviewIdunder the givenproductId. Returnstrueif the review was deleted,falseotherwise.
Starter Task Implementation
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 getReview function retrieves the review details for a specific product, returning a boolean to indicate whether the product or review exists. The deleteReview function handles the removal of specific reviews, and if no other reviews remain for a product, the product itself is removed from the list.
Now, let's extend this with new features.
