Lesson Tuesday

You can comment out the lines about exceptions for now and come back to them later. The relevant lessons are later in the week. If you'd like to look at them now, you can find them here and here.

Now that we have created a lot of the functionality we need to see for our app, all that is really left, as far as our MVP goes, is to set up the routing for our many-to-many relationship in the frontend. Let's add that now.

Building our frontend routes for Many-to-Many

Once our tests all pass, building our frontend routes is up next.

Let's pop over to our App.java and implement them so we can test with Postman.

First, we'll need a route that associates our foodtypes with restaurants. Then, we'll need additional routes to show the data we are looking to see.

Let's set up a post route that associates a foodtype with a restaurant first.

You may notice no findById for Foodtype exists yet. Oops! We should quickly implement this method - we can use the other methods we have created as our blueprint.

Go ahead and implement that method now if it does not yet exist, then add the routes shown below.

post("/restaurants/:restaurantId/foodtype/:foodtypeId", "application/json", (req, res) -> {
        int restaurantId = Integer.parseInt(req.params("restaurantId"));
        int foodtypeId = Integer.parseInt(req.params("foodtypeId"));
        Restaurant restaurant = restaurantDao.findById(restaurantId);
        Foodtype foodtype = foodtypeDao.findById(foodtypeId);

        if (restaurant != null && foodtype != null){
            //both exist and can be associated - we should probably not connect things that are not here.
            foodtypeDao.addFoodtypeToRestaurant(foodtype, restaurant);
            return gson.toJson(String.format("Restaurant '%s' and Foodtype '%s' have been associated",foodtype.getName(), restaurant.getName()));
        else {
            throw new ApiException(404, String.format("Restaurant or Foodtype does not exist"));

Go through this route and make sure you understand what is happening here. Test it with Postman to make sure it works.

Next, we'll retrieve information.

    get("/restaurants/:id/foodtypes", "application/json", (req, res) -> {
    int restaurantId = Integer.parseInt(req.params("id"));
    Restaurant restaurantToFind = restaurantDao.findById(restaurantId);
    if (restaurantToFind == null){
        throw new ApiException(404, String.format("No restaurant with the id: \"%s\" exists", req.params("id")));
    else if (restaurantDao.Restaurant(restaurantId).size()==0){
        return "{\"message\":\"I'm sorry, but no foodtypes are listed for this restaurant.\"}";
    else {
        return gson.toJson(restaurantDao.getAllFoodtypesByRestaurant(restaurantId));

get("/foodtypes/:id/restaurants", "application/json", (req, res) -> {
    int foodtypeId = Integer.parseInt(req.params("id"));
    Foodtype foodtypeToFind = foodtypeDao.findById(foodtypeId);
    if (foodtypeToFind == null){
        throw new ApiException(404, String.format("No foodtype with the id: \"%s\" exists", req.params("id")));
    else if (foodtypeDao.getAllRestaurantsForAFoodtype(foodtypeId).size()==0){
        return "{\"message\":\"I'm sorry, but no restaurants are listed for this foodtype.\"}";
    else {
        return gson.toJson(foodtypeDao.getAllRestaurantsForAFoodtype(foodtypeId));

Great! Make sure you test all of these routes thoroughly. We now have the ability to serve our API clients all kinds of interesting and useful data. Great stuff!

Example GitHub Repo for Jadle Code at this stage