Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
student-personal-details-spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
student-details-microservice
student-personal-details-spring-boot
Commits
7bdabdfd
Commit
7bdabdfd
authored
Apr 12, 2021
by
Darrick Yong
Browse files
Options
Browse Files
Download
Plain Diff
fix merge conflicts
parents
8e1bda87
cbf770ae
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
94 additions
and
69 deletions
+94
-69
build.gradle
build.gradle
+3
-0
DetailsController.java
...va/com/student/details/controllers/DetailsController.java
+2
-1
Student.java
src/main/java/com/student/details/models/Student.java
+7
-65
StudentService.java
...ain/java/com/student/details/services/StudentService.java
+82
-3
No files found.
build.gradle
View file @
7bdabdfd
...
...
@@ -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
{
...
...
src/main/java/com/student/details/controllers/DetailsController.java
View file @
7bdabdfd
...
...
@@ -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"
);
}
...
...
src/main/java/com/student/details/models/Student.java
View file @
7bdabdfd
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
;
}
}
src/main/java/com/student/details/services/StudentService.java
View file @
7bdabdfd
...
...
@@ -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
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment