// Copyright 2021 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 com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// Delivers a list of strings representing today's trending searches worldwide
@WebServlet("/trending")
public class TrendingServlet extends HttpServlet {


  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Gson gson = new Gson();
    response.setContentType("application/json ");
    response.getWriter().println(gson.toJson(getTrendingSearches()));
  }

  /**
   * Return lists of trending seraches from API.
   */
  public List<String> getTrendingSearches() {
    List<String> trendingSearches = new ArrayList<>();
    // fetch trending searches from db - hard coded data for now
    trendingSearches.add("Giraffes");
    trendingSearches.add("Chicken nuggets");

    return trendingSearches;
  }
}
@@ -899,16 +899,53 @@
 esponse;
+%0Aimport com.google.cloud.datastore.*;
 %0A%0A// Del
@@ -1094,17 +1094,16 @@
 vlet %7B%0A%0A
-%0A
   @Overr
@@ -1562,117 +1562,566 @@
 m db
- - hard coded data for now%0A    trendingSearches.add(%22Giraffes%22);%0A    trendingSearches.add(%22Chicken nuggets%22);
+%0A    // Instantiates a client%0A    Datastore datastore = DatastoreOptions.getDefaultInstance().getService();%0A%0A    //fetch test data to confirm working%0A    Query%3CEntity%3E query = Query.newEntityQueryBuilder().setKind(%22TopTrends%22).build();%0A    QueryResults%3CEntity%3E trends = datastore.run(query);%0A%0A    Entity trend;%0A    String trendingSearch;%0A    while (trends.hasNext()) %7B%0A      trend = trends.next();%0A      for (int i = 1; i %3C= 10; i++) %7B%0A        trendingSearch = trend.getString(Integer.toString(i));%0A        trendingSearches.add(trendingSearch);%0A      %7D%0A    %7D
 %0A%0A  

Update trending servlet to return ranked data from datastore