-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestaurantController.java
203 lines (167 loc) · 7.05 KB
/
RestaurantController.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
package com.snackoverflow.yelproulette;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import javax.servlet.http.HttpServletResponse;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Duration;
import java.util.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
@SpringBootApplication
@RestController
@CrossOrigin
@ServletComponentScan
@RequestMapping("/api")
public class RestaurantController {
String[] restaurantData = { "Taco Bell", "Wendy's", "Chipotle", "McDonalds", "Raising Canes" };
boolean onRefreshPage = false;
long durationInSeconds = 0;
private static final String API_KEY = "aUUmAi731yi0X2gIQR2Y8ACDhPJSuPP6Y9Zsbn9stSBmluwa0vHYDYKh-HDYIcg4yPWhZ9FAwnYiXOCY2iI43ODZb7YFH5Ul6Mp1FB1GSWaPBvHfxQub3XGbi6BYY3Yx";
Restaurant input = new Restaurant();
private static final float MILESTOMETERS = 1609.344f;
private static MongoClient mongoClient;
private static DB database;
private static DBCollection test;
private static MongoCollection collection;
private static DBCursor cursor;
MongoDBUser mongoDBUser = new MongoDBUser("", "", "", "");
MongoDBService mDBS = new MongoDBService();
public static void main(String[] args) {
SpringApplication.run(RestaurantController.class, args);
}
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
@GetMapping("/randomRestaurant")
public String randomRestaurant() {
Random r = new Random();
return "You landed on " + restaurantData[r.nextInt(restaurantData.length)] + "!";
}
@PostMapping("/register")
public void postRegister(@RequestBody MongoDBUser mongoDBUser) {
this.mongoDBUser.setFirstName(mongoDBUser.getFirstName());
this.mongoDBUser.setLastName(mongoDBUser.getLastName());
this.mongoDBUser.setEmail(mongoDBUser.getEmail());
this.mongoDBUser.setPassword(mongoDBUser.getPassword());
}
@GetMapping("/register")
public String postRegister(){
return "User: " + mongoDBUser.getEmail() + " | " + mongoDBUser.getPassword();
}
@GetMapping("/userExists")
public boolean userExists(){
boolean exists = mDBS.searchDocumentForUser("Email", mongoDBUser.getEmail());
//if user doesn't exist, add them
if(!exists){
mDBS.addUserToDatabase(mongoDBUser.getFirstName(), mongoDBUser.getLastName(), mongoDBUser.getEmail(), mongoDBUser.getPassword());
}
return exists;
}
@GetMapping("/testUserExists")
public boolean testUserExists(@RequestParam(value = "user", defaultValue = "Bob") String user){
return mDBS.searchDocumentForUser("Email", user);
}
@GetMapping("/test")
public boolean bool() {
return mDBS.searchDocumentForUser("firstName", "Dummy");
}
@GetMapping("/getUser")
public String getUser() {
//System.out.println(mDBS.getUserWithString("Email", mongoDBUser.getEmail()).toString());
return mDBS.getUserWithString("Email", mongoDBUser.getEmail()).get("Password").toString();
}
@GetMapping("/getPassword")
public String getPassword() {
return "";
}
@GetMapping("/testYelpFusion")
public String testYelpFusion() throws URISyntaxException, JsonProcessingException {
URI location = new URI(
"https://api.yelp.com/v3/businesses/search?term=restaurants&location=3801+W+Temple+Ave%2C+Pomona%2C+CA+91768");
RestTemplate restTemplate = new RestTemplate();
RequestEntity<?> request = RequestEntity.get(location).header("Authorization", "Bearer " + API_KEY).build();
ResponseEntity<String> response = restTemplate.exchange(request, String.class);
ObjectMapper mapper = new ObjectMapper();
Business business = mapper.readValue(response.getBody(), Business.class);
return response.getBody();
}
@GetMapping("/getURL")
public String getURL() throws URISyntaxException, JsonProcessingException {
String url = "https://api.yelp.com/v3/businesses/search?term=restaurants%2C+food";
if (!input.getAddress().equals(""))
url += "&location=" + URLEncoder.encode(input.getAddress(), StandardCharsets.UTF_8);
if (input.getRadius() != 0)
url += "&radius=" + Math.round(input.getRadius() * MILESTOMETERS);
else if (input.getRadius() >= 25)
url += "&radius=40000";
if (!input.getPrice().equals(""))
url += "&price=" + input.getPrice();
if (input.getOpenNow())
url += "&open_now=true";
else
url += "&open_now=false";
url += "&limit=50";
URI location = new URI(url);
RestTemplate restTemplate = new RestTemplate();
RequestEntity<?> request = RequestEntity.get(location).header("Authorization", "Bearer " + API_KEY).build();
ResponseEntity<String> response = restTemplate.exchange(request, String.class);
ObjectMapper mapper = new ObjectMapper();
Business business = mapper.readValue(response.getBody(), Business.class);
System.out.println("URL: " + url);
return response.getBody();
}
// Test API post (get data from frontend)
@PostMapping("/input")
public void postInput(@RequestBody Restaurant input) {
this.input.setAddress(input.getAddress());
this.input.setRadius(input.getRadius());
this.input.setPrice(input.getPrice());
this.input.setOpenNow(input.getOpenNow());
}
@GetMapping("/input")
public String postInput() {
return "Address: " + input.getAddress() +
" | Radius: " + input.getRadius() +
" | Price: " + input.getPrice() +
" | Open Now: " + input.getOpenNow();
}
@PostMapping("/postID")
public void postID(@RequestBody Restaurant input) {
this.input.setID(input.getID());
}
@GetMapping("/postID")
public String postID() {
return "ID: " + input.getID();
}
@GetMapping("/getID")
public String getBusinessData() throws URISyntaxException, JsonProcessingException {
String url = "https://api.yelp.com/v3/businesses/";
url += input.getID();
URI location = new URI(url);
RestTemplate restTemplate = new RestTemplate();
RequestEntity<?> request = RequestEntity.get(location).header("Authorization", "Bearer " + API_KEY).build();
ResponseEntity<String> response = restTemplate.exchange(request, String.class);
System.out.println("URL: " + url);
return response.getBody();
}
}