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
78e7b802
Commit
78e7b802
authored
Apr 12, 2021
by
Alex Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix UserService#updateStudent to copy all non-null fields from request body
parent
cbf770ae
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
6 deletions
+17
-6
Student.java
src/main/java/com/student/details/models/Student.java
+15
-0
StudentService.java
...ain/java/com/student/details/services/StudentService.java
+2
-6
No files found.
src/main/java/com/student/details/models/Student.java
View file @
78e7b802
...
@@ -3,6 +3,7 @@ package com.student.details.models;
...
@@ -3,6 +3,7 @@ package com.student.details.models;
import
lombok.Data
;
import
lombok.Data
;
import
javax.persistence.*
;
import
javax.persistence.*
;
import
javax.validation.constraints.NotNull
;
import
javax.validation.constraints.NotNull
;
import
java.lang.reflect.Field
;
import
java.time.LocalDate
;
import
java.time.LocalDate
;
@Entity
@Entity
...
@@ -21,4 +22,18 @@ public class Student {
...
@@ -21,4 +22,18 @@ public class Student {
@NotNull
@NotNull
private
LocalDate
dateOfBirth
;
private
LocalDate
dateOfBirth
;
public
void
copyFields
(
Student
sourceStudent
)
{
try
{
for
(
Field
field
:
sourceStudent
.
getClass
().
getDeclaredFields
())
{
String
fieldName
=
field
.
getName
();
Object
sourceValue
=
field
.
get
(
sourceStudent
);
if
(!
fieldName
.
equals
(
"id"
)
&&
null
!=
sourceValue
)
{
Field
destField
=
this
.
getClass
().
getDeclaredField
(
fieldName
);
destField
.
set
(
this
,
sourceValue
);
}
}
}
catch
(
Exception
ignore
)
{
}
}
}
}
src/main/java/com/student/details/services/StudentService.java
View file @
78e7b802
...
@@ -44,12 +44,8 @@ public class StudentService {
...
@@ -44,12 +44,8 @@ public class StudentService {
public
Student
updateStudent
(
Long
studentId
,
Student
studentDetails
)
throws
ResourceNotFoundException
{
public
Student
updateStudent
(
Long
studentId
,
Student
studentDetails
)
throws
ResourceNotFoundException
{
Student
student
=
studentRepository
.
findById
(
studentId
)
Student
student
=
studentRepository
.
findById
(
studentId
)
.
orElseThrow
(()
->
new
ResourceNotFoundException
(
"Student id: "
+
studentId
+
" not found"
));
.
orElseThrow
(()
->
new
ResourceNotFoundException
(
"Student id: "
+
studentId
+
" not found"
));
student
.
copyFields
(
studentDetails
);
student
.
setFirstName
(
studentDetails
.
getFirstName
());
return
studentRepository
.
save
(
student
);
student
.
setLastName
(
studentDetails
.
getLastName
());
Student
updatedStudent
=
studentRepository
.
save
(
student
);
return
updatedStudent
;
}
}
...
...
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