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
7556ff7c
Commit
7556ff7c
authored
Apr 05, 2021
by
Darrick Yong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add uniqueness to email in Model and findByEmail
parent
a51c8c01
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
49 additions
and
4 deletions
+49
-4
DetailsController.java
...va/com/student/details/controllers/DetailsController.java
+37
-4
Student.java
src/main/java/com/student/details/models/Student.java
+2
-0
StudentRepository.java
...a/com/student/details/repositories/StudentRepository.java
+6
-0
StudentService.java
...ain/java/com/student/details/services/StudentService.java
+4
-0
No files found.
src/main/java/com/student/details/controllers/DetailsController.java
View file @
7556ff7c
package
com
.
student
.
details
.
controllers
;
import
com.student.details.exceptions.ResourceNotFoundException
;
import
com.student.details.models.Student
;
import
com.student.details.services.StudentService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.CrossOrigin
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
java.util.List
;
import
java.util.Map
;
@CrossOrigin
(
origins
=
"*"
)
@RestController
...
...
@@ -24,4 +24,37 @@ public class DetailsController {
return
studentService
.
findAllStudents
();
}
@PostMapping
(
"/students"
)
public
Student
createStudent
(
@RequestBody
Student
student
)
{
return
studentService
.
addStudent
(
student
);
}
@GetMapping
(
"/students/{id}"
)
public
ResponseEntity
<
Student
>
getStudentById
(
@PathVariable
(
value
=
"id"
)
Long
studentId
)
throws
ResourceNotFoundException
{
Student
student
=
studentService
.
getStudentById
(
studentId
)
.
orElseThrow
(()
->
new
ResourceNotFoundException
(
"Student by Id Not Found"
+
studentId
));
return
ResponseEntity
.
ok
().
body
(
student
);
}
@GetMapping
(
"/students/email/{email:.+}"
)
public
ResponseEntity
<
Student
>
getStudentByEmail
(
@PathVariable
(
value
=
"email"
)
String
email
)
throws
ResourceNotFoundException
{
Student
student
=
studentService
.
getStudentByEmail
(
email
)
.
orElseThrow
(()
->
new
ResourceNotFoundException
(
"Student with email"
+
email
+
"not found."
));
return
ResponseEntity
.
ok
().
body
(
student
);
}
@PutMapping
(
"/students/{id}"
)
public
Student
updateStudent
(
@PathVariable
(
value
=
"id"
)
Long
studentId
,
@RequestBody
Student
student
)
throws
ResourceNotFoundException
{
Student
studentToUpdate
=
studentService
.
getStudentById
(
studentId
)
.
orElseThrow
(()
->
new
ResourceNotFoundException
(
"Student by Id Not Found"
+
studentId
));
return
studentService
.
updateStudent
(
studentId
,
student
);
}
@DeleteMapping
(
"/students/{id}"
)
public
Map
<
String
,
Boolean
>
deleteEmployee
(
@PathVariable
(
value
=
"id"
)
Long
studentId
)
throws
ResourceNotFoundException
{
Student
student
=
studentService
.
getStudentById
(
studentId
)
.
orElseThrow
(()
->
new
ResourceNotFoundException
(
"Student by Id Not Found"
+
studentId
));
return
studentService
.
deleteStudent
(
studentId
);
}
}
src/main/java/com/student/details/models/Student.java
View file @
7556ff7c
...
...
@@ -11,7 +11,9 @@ public class Student {
@GeneratedValue
(
strategy
=
GenerationType
.
IDENTITY
)
private
Long
id
;
@Column
(
unique
=
true
)
private
String
email
;
private
String
firstName
;
private
String
lastName
;
private
LocalDate
dateOfBirth
;
...
...
src/main/java/com/student/details/repositories/StudentRepository.java
View file @
7556ff7c
...
...
@@ -2,10 +2,16 @@ package com.student.details.repositories;
import
com.student.details.models.Student
;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
org.springframework.data.jpa.repository.Query
;
import
org.springframework.stereotype.Repository
;
import
java.util.Optional
;
@Repository
public
interface
StudentRepository
extends
JpaRepository
<
Student
,
Long
>
{
@Query
(
"from Student s where s.email = ?1"
)
Optional
<
Student
>
findStudentByEmail
(
String
email
);
}
src/main/java/com/student/details/services/StudentService.java
View file @
7556ff7c
...
...
@@ -30,6 +30,10 @@ public class StudentService {
return
studentRepository
.
findById
(
studentId
);
}
public
Optional
<
Student
>
getStudentByEmail
(
String
email
)
{
return
studentRepository
.
findStudentByEmail
(
email
);
}
public
Student
updateStudent
(
Long
studentId
,
Student
studentDetails
)
throws
ResourceNotFoundException
{
Student
student
=
studentRepository
.
findById
(
studentId
)
.
orElseThrow
(()
->
new
ResourceNotFoundException
(
"Student id: "
+
studentId
+
" not found"
));
...
...
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