In this chapter we will look more into how to build a content rating implementation and how to program a simple content rating algorithm that can be easily extended.
First of all we implement how to store the user's ratings. We have a simple Content class that has a message attribute that represents a String content, a microblog text message for example.
We have a few different ways to model the content rating, one is to add the rating straight to the Content class and calculate the total points there. The other one is to crete a new Rating class and keep the separated user ratings there. For performance it is better to implement these both. Rating points are added to the total sum and Rating is stored to remember which user has rated which content. This prevents the user to rate the content multiple times. And to note that this has to be done inside a database transaction.
The total points are calculated straight as an integer inside the Content class. And the user's ratings are kept separated in a different class called Rating.
Image of Content and Rating relation.
Next we have an Java Spring boot implementation of a rating algorith as a microservice.