// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.sps.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.google.sps.data.Meal;
import java.util.HashMap;

@WebServlet("/meal/*")
public class MealServlet extends HttpServlet {

    private final HashMap < Long, Meal > dishes = new HashMap < Long, Meal > ();

    @Override
    public void init() {
        // TODO(sandatsian): implement uploading of dataset from website here
        dishes.put(0L, new Meal(0L, "fried potato", "fried potato with mushrooms and onion",
            new ArrayList < > (Arrays.asList("potato", "onion", "mushrooms", "oil")), "Main"));
        dishes.put(1L, new Meal(1L, "Italian pizza", "Pizza with pineaple, sausage and tomato",
            new ArrayList < > (Arrays.asList("flour", "water", "sausage", "tomato", "pineaple", "cheese")), "pizza"));
        dishes.put(2L, new Meal(2L, "vegetable soup", "vegetable soup with onion",
            new ArrayList < > (Arrays.asList("potato", "onion", "cabbage", "mushrooms", "water", "carrot", "pumpkin")), "soup"));
        dishes.put(3L, new Meal(3L, "chocolate cake", "chocolate cake with butter cream and strawberry",
            new ArrayList < > (Arrays.asList("flour", "water", "butter", "cacao", "chocolate", "sugar", "strawberry", "eggs")), "dessert"));
        dishes.put(4L, new Meal(4L, "hot chocolate", "hot chocolate with sugar and caramel",
            new ArrayList < > (Arrays.asList("cacao", "sugar", "milk", "caramel", "vanil")), "drinks"));
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String pathInfo = request.getPathInfo();

        // Get meal
        // GET/meal/
        if (pathInfo == null || pathInfo.equals("/")) {
            searchMeal(request, response);
            return;
        }

        // GET/meal/*/*
        if (pathInfo.split("/").length != 2) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }

        // GET/meal/meal_id
        if (pathInfo.split("/").length == 2) {
            getMealById(request, response);
            return;
        }

        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

    private void searchMeal(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String gson = new Gson().toJson(dishes);
        response.setContentType("application/json");
        response.getWriter().println(gson);
        return;
    }

    private void getMealById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String pathInfo = request.getPathInfo();
        String idString = pathInfo.replaceAll("/", "");

        Long id = null;
        try {
            id = Long.parseLong(idString);
        } catch (NumberFormatException e) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        Meal meal = dishes.get(id);

        if (meal != null) {
            String gson = new Gson().toJson(meal);
            response.setContentType("application/json");
            response.getWriter().println(gson);
            return;
        }

        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }


    private String getParameter(HttpServletRequest request, String name, String defaultValue) {
        String value = request.getParameter(name);
        if (value == null) {
            return defaultValue;
        }
        return value;
    }
}
@@ -1139,33 +1139,32 @@
  final HashMap %3C
- 
 Long, Meal %3E dis
@@ -1157,17 +1157,16 @@
 ng, Meal
- 
 %3E dishes
@@ -1181,17 +1181,16 @@
 ashMap %3C
- 
 Long, Me
@@ -1191,17 +1191,16 @@
 ng, Meal
- 
 %3E ();%0A%0A 
@@ -1427,33 +1427,32 @@
  new ArrayList %3C
- 
 %3E (Arrays.asList
@@ -1618,33 +1618,32 @@
  new ArrayList %3C
- 
 %3E (Arrays.asList
@@ -1819,33 +1819,32 @@
  new ArrayList %3C
- 
 %3E (Arrays.asList
@@ -2053,33 +2053,32 @@
  new ArrayList %3C
- 
 %3E (Arrays.asList
@@ -2294,17 +2294,16 @@
 ayList %3C
- 
 %3E (Array
@@ -2573,25 +2573,25 @@
       // GET
-/
+ 
 meal/%0A      
@@ -2732,17 +2732,21 @@
  GET
-/
+ 
 meal/
-*/*
+meal_id
 %0A   
@@ -2781,17 +2781,17 @@
 .length 
-!
+=
 = 2) %7B%0A 
@@ -2805,61 +2805,37 @@
     
-response.sendError(HttpServletResponse.SC_BAD_REQUEST
+getMealById(request, response
 );%0A 
@@ -2856,32 +2856,66 @@
 urn;%0A        %7D%0A%0A
+        // Invalid request format%0A
         // GET/m
@@ -2916,21 +2916,17 @@
  GET
-/
+ 
 meal/
-meal_id
+*/*
 %0A   
@@ -2953,33 +2953,33 @@
 lit(%22/%22).length 
-=
+!
 = 2) %7B%0A         
@@ -2985,37 +2985,61 @@
     
-getMealById(request, response
+response.sendError(HttpServletResponse.SC_BAD_REQUEST
 );%0A 
@@ -3252,32 +3252,145 @@
 s IOException %7B%0A
+        // Fuction returns hardcoded result%0A        // TODO(sandatsian): implement search algorithm here for MVP%0A
         String g

Edit comments