Commit 7bdabdfd authored by Darrick Yong's avatar Darrick Yong

fix merge conflicts

parents 8e1bda87 cbf770ae
......@@ -35,6 +35,9 @@ dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap:3.0.2'
implementation 'io.springfox:springfox-boot-starter:3.0.0'
implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
compile 'org.json:json:20210307'
annotationProcessor 'org.projectlombok:lombok'
compileOnly 'org.projectlombok:lombok:1.18.16'
}
dependencyManagement {
......
......@@ -2,6 +2,7 @@ package com.student.details.controllers;
import com.student.details.exceptions.ResourceNotFoundException;
import com.student.details.models.Student;
import com.student.details.services.APIService;
import com.student.details.services.StudentService;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -20,7 +21,7 @@ public class DetailsController {
@GetMapping("/students/demo")
public JSONObject fetchDemo() {
return studentService.fetchService("https://opentdb.com/api.php?amount=10");
return APIService.get("https://opentdb.com/api.php?amount=10");
}
......
package com.student.details.models;
import lombok.Data;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
@Entity
@Table(name = "student_details")
@Data
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
......@@ -14,69 +16,9 @@ public class Student {
@Column(unique = true)
private String email;
private String firstName;
private String lastName;
@NotNull
private String firstName, lastName, course;
@NotNull
private LocalDate dateOfBirth;
private String course;
public Student(Long id, String email, String firstName, String lastName, LocalDate dateOfBirth, String course) {
this.id = id;
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
this.dateOfBirth = dateOfBirth;
this.course = course;
}
public Student() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public LocalDate getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(LocalDate dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
}
......@@ -2,13 +2,20 @@ package com.student.details.services;
import com.student.details.exceptions.ResourceNotFoundException;
import com.student.details.models.Student;
import com.student.details.repositories.StudentRepository;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@Service
public class StudentService {
......@@ -53,4 +60,76 @@ public class StudentService {
response.put("deleted", true);
return response;
}
public Student getStudentAddressFromEmail(Student student){
//get students email for api call
String email = student.getEmail();
String apiUrl = String.format("url/students/%s", email);
//make API call other microservice's Rest API
JSONObject response = getJSONObjectFromAPICall(apiUrl);
//make new address object with JSON response object
/*
Address address;
address.setStreet(res.get("street"));
...
*/
//add it to our student's address field!
/*
student.setAddress(address);
*/
return student;
}
public JSONObject getJSONObjectFromAPICall(String apiUrl){
HttpURLConnection connection;
StringBuffer responseContent = new StringBuffer();
BufferedReader reader;
String line;
try {
URL url = new URL(apiUrl);
connection = (HttpURLConnection) url.openConnection();
//setup
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int status = connection.getResponseCode();
//read and write
if (status > 299) {
reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
} else {
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
}
while((line = reader.readLine()) != null){
responseContent.append(line);
}
reader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
JSONObject res = new JSONObject(responseContent.toString());
return res;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment