-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRestaurantServiceImpl.java
249 lines (168 loc) · 8.38 KB
/
RestaurantServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
*
* * Copyright (c) Crio.Do 2019. All rights reserved
*
*/
package com.crio.qeats.services;
import com.crio.qeats.dto.Restaurant;
import com.crio.qeats.exchanges.GetRestaurantsRequest;
import com.crio.qeats.exchanges.GetRestaurantsResponse;
import com.crio.qeats.models.RestaurantEntity;
import com.crio.qeats.repositoryservices.RestaurantRepositoryService;
import com.crio.qeats.repositoryservices.RestaurantRepositoryServiceDummyImpl;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Log4j2
public class RestaurantServiceImpl implements RestaurantService {
private final Double peakHoursServingRadiusInKms = 3.0;
private final Double normalHoursServingRadiusInKms = 5.0;
@Autowired
private RestaurantRepositoryService restaurantRepositoryService;
// CRIO_TASK_MODULE_RESTAURANTSAPI - Implement findAllRestaurantsCloseby.
// Check RestaurantService.java file for the interface contract.
@Override
public GetRestaurantsResponse findAllRestaurantsCloseBy(GetRestaurantsRequest
getRestaurantsRequest, LocalTime currentTime) {
List<Restaurant> restaurant;
int h = currentTime.getHour();
int m = currentTime.getMinute();
if ((h >= 8 && h <= 9) || (h == 10 && m == 0) || (h == 13) || (h == 14 && m == 0)
|| (h >= 19 && h <= 20) || (h == 21 && m == 0)) {
restaurant = restaurantRepositoryService.findAllRestaurantsCloseBy(
getRestaurantsRequest.getLatitude(), getRestaurantsRequest.getLongitude(),
currentTime, peakHoursServingRadiusInKms);
} else {
restaurant = restaurantRepositoryService.findAllRestaurantsCloseBy(
getRestaurantsRequest.getLatitude(), getRestaurantsRequest.getLongitude(),
currentTime, normalHoursServingRadiusInKms);
}
GetRestaurantsResponse response = new GetRestaurantsResponse(restaurant);
//log.info(response);
return response;
}
// TODO: CRIO_TASK_MODULE_RESTAURANTSEARCH
// Implement findRestaurantsBySearchQuery. The request object has the search string.
// We have to combine results from multiple sources:
// 1. Restaurants by name (exact and inexact)
// 2. Restaurants by cuisines (also called attributes)
// 3. Restaurants by food items it serves
// 4. Restaurants by food item attributes (spicy, sweet, etc)
// Remember, a restaurant must be present only once in the resulting list.
// Check RestaurantService.java file for the interface contract.
/**
* Get the restaurants by processing the query.
* -Ordering rules
* 1) Restaurant name
* - exact matches first
* - partial matches second
* 2) Restaurant attributes
* - partial and full matches in any order
* 3) Item name
* - exact matches first
* - partial matches second
* 4) Item attributes
* - partial and full matches in any order
* - For peak hours: 8AM - 10AM, 1PM-2PM, 7PM-9PM
* - service radius is 3KMs.
* - All other times, serving radius is 5KMs.
* - If there are no restaurants, return empty list of restaurants.
* @param getRestaurantsRequest valid lat/long and searchFor
* @return GetRestaurantsResponse object containing a list of open restaurants or an
* empty list if none fits the criteria.
*/
@Override
public GetRestaurantsResponse findRestaurantsBySearchQuery(GetRestaurantsRequest getRestaurantsRequest,
LocalTime currentTime) {
// TODO Auto-generated method stub
List<Restaurant> restaurant = new ArrayList<>();
List<Restaurant> restaurantList ;
// List<Restaurant> availableRestaurant;
HashSet<Restaurant> set=new HashSet();
Double latitude = getRestaurantsRequest.getLatitude();
Double longitude = getRestaurantsRequest.getLongitude();
String searchString = getRestaurantsRequest.getSearchFor();
// GetRestaurantsResponse getRestaurantsResponse = findAllRestaurantsCloseBy(getRestaurantsRequest,currentTime);
// availableRestaurant = getRestaurantsResponse.getRestaurants();
// for(Restaurant restaurants : availableRestaurant){
// set.add(restaurants);
// }
if(searchString.equals("")){
return new GetRestaurantsResponse(restaurant);
}
int h = currentTime.getHour();
int m = currentTime.getMinute();
if ((h >= 8 && h <= 9) || (h == 10 && m == 0) || (h == 13) || (h == 14 && m == 0)
|| (h >= 19 && h <= 20) || (h == 21 && m == 0)) {
restaurantList = restaurantRepositoryService.findRestaurantsByName(latitude, longitude, searchString, currentTime, peakHoursServingRadiusInKms);
// System.out.println("chk1");
// System.out.println(restaurantList.get(0));
for(Restaurant restaurants : restaurantList){
set.add(restaurants);
}
restaurantList = restaurantRepositoryService.findRestaurantsByNameNotExact(latitude, longitude, searchString, currentTime, peakHoursServingRadiusInKms);
for(Restaurant restaurants : restaurantList){
set.add(restaurants);
}
restaurantList = restaurantRepositoryService.findRestaurantsByAttributes(latitude, longitude, searchString, currentTime, peakHoursServingRadiusInKms);
for(Restaurant restaurants : restaurantList){
set.add(restaurants);
}
restaurantList = restaurantRepositoryService.findRestaurantsByItemName(latitude, longitude, searchString, currentTime, peakHoursServingRadiusInKms);
for(Restaurant restaurants : restaurantList){
set.add(restaurants);
}
restaurantList = restaurantRepositoryService.findRestaurantsByItemAttributes(latitude, longitude, searchString, currentTime, peakHoursServingRadiusInKms);
for(Restaurant restaurants : restaurantList){
set.add(restaurants);
}
} else {
restaurantList = restaurantRepositoryService.findRestaurantsByName(latitude, longitude, searchString, currentTime, normalHoursServingRadiusInKms);
// System.out.println("chk1");
// System.out.println(restaurantList.get(0));
for(Restaurant restaurants : restaurantList){
set.add(restaurants);
}
restaurantList = restaurantRepositoryService.findRestaurantsByNameNotExact(latitude, longitude, searchString, currentTime, normalHoursServingRadiusInKms);
for(Restaurant restaurants : restaurantList){
set.add(restaurants);
}
restaurantList = restaurantRepositoryService.findRestaurantsByAttributes(latitude, longitude, searchString, currentTime, normalHoursServingRadiusInKms);
for(Restaurant restaurants : restaurantList){
set.add(restaurants);
}
restaurantList = restaurantRepositoryService.findRestaurantsByItemName(latitude, longitude, searchString, currentTime, normalHoursServingRadiusInKms);
for(Restaurant restaurants : restaurantList){
set.add(restaurants);
}
restaurantList = restaurantRepositoryService.findRestaurantsByItemAttributes(latitude, longitude, searchString, currentTime, normalHoursServingRadiusInKms);
for(Restaurant restaurants : restaurantList){
set.add(restaurants);
}
}
Iterator<Restaurant> i=set.iterator();
while(i.hasNext())
{
System.out.println("i.hahnaext()....");
System.out.println(i.next());
restaurant.add(i.next());
}
// System.out.println("chk2");
// System.out.println(restaurant.get(0));
GetRestaurantsResponse response = new GetRestaurantsResponse(restaurant);
// System.out.println("chk3");
// System.out.println("response.getRestaurants().get(2).getRestaurantId() " + response.getRestaurants().get(0).getRestaurantId());
return response;
}
}