Spring Boot - API - Guided Exercise: ⚖️ RateMyBurger – Build a Smart Rating Engine (Using @GetMapping + @RequestParam) #188
akash-coded
started this conversation in
Tasks
Replies: 3 comments
-
**
** |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
@RestController } |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
PRASETHA N package com.burgerbyte.rateburger.controller;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/api")
public class RatingController {
private List<String> ratings = new ArrayList<>();
private Map<String, List<Double>> ratingMap = new HashMap<>();
@GetMapping("/rate")
public String rateBurger(
@RequestParam String name,
@RequestParam String burger,
@RequestParam double stars,
@RequestParam(required = false) String comment) {
if (stars < 0 || stars > 5) {
return "Invalid rating. Please rate between 0 and 5.";
}
String response = "Thanks " + name + "! You rated " + burger + " " + stars + " stars!";
if (comment != null && !comment.isEmpty()) {
response += " Comment: " + comment;
}
ratings.add(response);
String burgerKey = burger.toLowerCase();
if (!ratingMap.containsKey(burgerKey)) {
ratingMap.put(burgerKey, new ArrayList<>());
}
ratingMap.get(burgerKey).add(stars);
return response;
}
@GetMapping("/reviews")
public List<String> getReviews(@RequestParam(required = false) String burger) {
if (burger == null || burger.isEmpty()) {
return ratings;
}
List<String> filtered = new ArrayList<>();
String burgerKey = burger.toLowerCase();
for (String rating : ratings) {
if (rating.toLowerCase().contains(burgerKey)) {
filtered.add(rating);
}
}
return filtered;
}
@GetMapping("/summary")
public String getSummary(@RequestParam String burger) {
String burgerKey = burger.toLowerCase();
List<Double> starList = ratingMap.getOrDefault(burgerKey, new ArrayList<>());
if (starList.isEmpty()) {
return "Burger: " + burger + " has no reviews yet.";
}
double sum = 0;
for (Double star : starList) {
sum += star;
}
double avg = sum / starList.size();
return "Burger: " + burger + " | Total Reviews: " + starList.size() + " | Avg Stars: " + String.format("%.1f", avg);
}
@GetMapping("/feedback")
public String getFeedback(@RequestParam int ratingId) {
if (ratingId < 0 || ratingId >= ratings.size()) {
return "Rating not found.";
}
return ratings.get(ratingId);
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
🧪 Exercise Title: ⚖️ RateMyBurger – Build a Smart Rating Engine (Using
@GetMapping+@RequestParam)🎯 Learning Outcomes
By the end of this mission, you will:
✅ Understand how to use
@GetMappingto create GET endpoints✅ Capture query parameters using
@RequestParam✅ Perform conditional logic based on inputs
✅ Return clean responses via HTTP
✅ Build clean controllers using industry-style conventions
✅ Improve API naming, structuring, and parameter handling
🎮 THE STORY
You’ve been hired as a backend dev at BurgerByte, a startup that delivers exotic burgers on Mars.
Your task? Build a Burger Rating API that:
"Thanks Sam! You rated SpicyCheezilla 4.5 stars!"commentburgerName🛠 SETUP
1. Project Setup
Generate project from [start.spring.io](https://start.spring.io)
com.burgerbyterateburgerSpring Web2. Folder Structure
🧩 PHASED MISSION STRUCTURE
🌟 PHASE 1: Your First Rating Endpoint
🎯 Build a GET endpoint:
URL format:
GET /api/rate?name=Sam&burger=SpicyCheezilla&stars=4.5📄
RatingController.java✅ Run and test in Postman.
🧠 Challenge to build this without copy-pasting.
🔍 PHASE 2: Add Rating Validation
Add logic:
stars > 5orstars < 0, return:"⚠️ Invalid rating. Please rate between 0 and 5."✍️ Write only the conditional block and response.
💬 PHASE 3: Accept Optional
comment@RequestParam(required = false) String commentExample URL:
/api/rate?name=Sam&burger=SpicyCheezilla&stars=4.5&comment=Loved it!Response:
Thanks Sam! You rated SpicyCheezilla 4.5 stars! Comment: Loved it!🧪 Challenge: How to write conditional
if (comment != null)logic.🗃️ PHASE 4: Maintain Ratings (In-Memory)
Create a simple
List<String>inside the controller to store rating responses.Every time someone rates, add the final response to the list.
Add a new endpoint:
URL:
/api/reviewsOutput: List of all rating messages
🧠 Question: Why is
List<String>not thread-safe? What could go wrong?🔎 PHASE 5: Add Filtering by Burger Name
Modify
/reviewsto accept optional paramburger.Example:
/api/reviews?burger=SpicyCheezilla✅ Build this filtering logic from scratch.
🔁 PHASE 6: Add Ratings Summary (Bonus)
Create a new endpoint:
URL:
/api/summary?burger=SpicyCheezillaResponse:
Burger: SpicyCheezilla | Total Reviews: 3 | Avg Stars: 4.3🧠 You are required to:
Map<String, List<Double>>of burger → ratingsstream().mapToDouble().average().orElse(0)🎯 API SUMMARY
/api/ratename,burger,stars,comment/api/reviews[burger]/api/summaryburger🧼 BEST PRACTICES TRAINED
@GetMapping@RequestParamrequired = false💡 QUESTIONS TO ANSWER
GETover request body?@RequestParamand@PathVariable?@RequestParamis missing but required?starsis not a number?🧾 STUDENT DELIVERABLES
RatingController.javawith all endpointsGET /api/feedback?ratingId=3)🏁 YOU’VE UNLOCKED:
✅ API design using GET
✅ Input reading using
@RequestParam✅ Building filters, optional fields, conditional logic
✅ Building a mini API with real backend logic
Beta Was this translation helpful? Give feedback.
All reactions