Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Paysafe Interns Valentin-Martin
Student Managment
david-krastev-student-management-2023
Commits
f16775f1
Commit
f16775f1
authored
1 year ago
by
David Krastev
Browse files
Options
Download
Email Patches
Plain Diff
refactoring
parent
01bfb429
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
student-managment-app/api/src/main/java/student/managment/app/app/GlobalExceptionHandler.java
+29
-9
...ava/student/managment/app/app/GlobalExceptionHandler.java
student-managment-app/api/src/main/java/student/managment/app/app/controllers/CourseController.java
+2
-8
...udent/managment/app/app/controllers/CourseController.java
student-managment-app/api/src/main/java/student/managment/app/app/controllers/StudentController.java
+7
-10
...dent/managment/app/app/controllers/StudentController.java
student-managment-app/api/src/main/java/student/managment/app/app/controllers/TeacherController.java
+1
-4
...dent/managment/app/app/controllers/TeacherController.java
student-managment-app/data-model/src/main/java/student/managment/app/entities/Teacher.java
+1
-2
...src/main/java/student/managment/app/entities/Teacher.java
student-managment-app/data-model/src/main/java/student/managment/app/entities/dtos/create_dtos/CreateTeacher.java
+6
-4
...anagment/app/entities/dtos/create_dtos/CreateTeacher.java
student-managment-app/data-model/src/main/java/student/managment/app/entities/enums/DegreeType.java
+0
-8
...java/student/managment/app/entities/enums/DegreeType.java
student-managment-app/services/src/main/java/student/managment/app/services/course/CourseGroupService.java
+3
-0
...ent/managment/app/services/course/CourseGroupService.java
student-managment-app/services/src/main/java/student/managment/app/services/course/CourseService.java
+5
-7
.../student/managment/app/services/course/CourseService.java
student-managment-app/services/src/main/java/student/managment/app/services/grade/GradeService.java
+5
-1
...va/student/managment/app/services/grade/GradeService.java
student-managment-app/services/src/main/java/student/managment/app/services/student/StudentService.java
+6
-23
...tudent/managment/app/services/student/StudentService.java
student-managment-app/services/src/main/java/student/managment/app/services/teacher/TeacherService.java
+1
-0
...tudent/managment/app/services/teacher/TeacherService.java
with
66 additions
and
76 deletions
+66
-76
student-managment-app/api/src/main/java/student/managment/app/app/GlobalExceptionHandler.java
+
29
-
9
View file @
f16775f1
package
student.managment.app.app
;
import
jakarta.persistence.EntityNotFoundException
;
import
org.springframework.
http.HttpHeaders
;
import
org.springframework.
context.support.DefaultMessageSourceResolvable
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
import
org.springframework.web.context.request.WebRequest
;
import
org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
;
import
student.managment.app.entities.dtos.response_dtos.CustomExceptionResponse
;
import
student.managment.app.entities.exception.DuplicateEntityException
;
import
java.sql.Timestamp
;
import
java.time.LocalDateTime
;
import
java.util.NoSuchElementException
;
import
java.util.stream.Collectors
;
@ControllerAdvice
public
class
GlobalExceptionHandler
extends
ResponseEntityExceptionHandler
{
public
class
GlobalExceptionHandler
{
@ResponseStatus
(
HttpStatus
.
CONFLICT
)
@ExceptionHandler
({
DuplicateEntityException
.
class
})
public
ResponseEntity
<
Object
>
handle
(
DuplicateEntityException
e
,
WebRequest
request
)
{
CustomExceptionResponse
customExceptionResponse
=
new
CustomExceptionResponse
();
customExceptionResponse
.
setTimestamp
(
String
.
valueOf
(
Timestamp
.
valueOf
(
LocalDateTime
.
now
())));
customExceptionResponse
.
setStatus
(
HttpStatus
.
CONFLICT
);
customExceptionResponse
.
setError
((
e
.
getMessage
()));
return
this
.
handleExceptionInternal
(
e
,
customExceptionResponse
,
new
HttpHeaders
(),
HttpStatus
.
CONFLICT
,
request
);
return
new
ResponseEntity
<>(
customExceptionResponse
,
HttpStatus
.
CONFLICT
);
}
@ResponseStatus
(
HttpStatus
.
BAD_REQUEST
)
@ExceptionHandler
({
IllegalArgumentException
.
class
})
public
ResponseEntity
<
Object
>
handle
(
IllegalArgumentException
e
,
WebRequest
request
)
{
CustomExceptionResponse
customExceptionResponse
=
new
CustomExceptionResponse
();
customExceptionResponse
.
setTimestamp
(
String
.
valueOf
(
Timestamp
.
valueOf
(
LocalDateTime
.
now
())));
customExceptionResponse
.
setStatus
(
HttpStatus
.
BAD_REQUEST
);
customExceptionResponse
.
setError
((
e
.
getMessage
()));
return
this
.
handleExceptionInternal
(
e
,
customExceptionResponse
,
new
HttpHeaders
(),
HttpStatus
.
BAD_REQUEST
,
request
);
return
new
ResponseEntity
<>(
customExceptionResponse
,
HttpStatus
.
BAD_REQUEST
);
}
@ResponseStatus
(
HttpStatus
.
BAD_REQUEST
)
@ExceptionHandler
({
NoSuchElementException
.
class
})
public
ResponseEntity
<
Object
>
handle
(
NoSuchElementException
e
,
WebRequest
request
)
{
CustomExceptionResponse
customExceptionResponse
=
new
CustomExceptionResponse
();
customExceptionResponse
.
setTimestamp
(
String
.
valueOf
(
Timestamp
.
valueOf
(
LocalDateTime
.
now
())));
customExceptionResponse
.
setStatus
(
HttpStatus
.
CONFLIC
T
);
customExceptionResponse
.
setError
(
(
e
.
getMessage
())
);
return
this
.
handleExceptionInternal
(
e
,
customExceptionResponse
,
new
HttpHeaders
(),
HttpStatus
.
CONFLICT
,
request
);
customExceptionResponse
.
setStatus
(
HttpStatus
.
BAD_REQUES
T
);
customExceptionResponse
.
setError
(
"No entity found with given parameters"
);
return
new
ResponseEntity
<>(
customExceptionResponse
,
HttpStatus
.
BAD_REQUEST
);
}
@ResponseStatus
(
HttpStatus
.
CONFLICT
)
@ExceptionHandler
({
EntityNotFoundException
.
class
})
public
ResponseEntity
<
Object
>
handle
(
EntityNotFoundException
e
,
WebRequest
request
)
{
CustomExceptionResponse
customExceptionResponse
=
new
CustomExceptionResponse
();
customExceptionResponse
.
setTimestamp
(
String
.
valueOf
(
Timestamp
.
valueOf
(
LocalDateTime
.
now
())));
customExceptionResponse
.
setStatus
(
HttpStatus
.
CONFLICT
);
customExceptionResponse
.
setError
((
e
.
getMessage
()));
return
this
.
handleExceptionInternal
(
e
,
customExceptionResponse
,
new
HttpHeaders
(),
HttpStatus
.
CONFLICT
,
request
);
return
new
ResponseEntity
<>(
customExceptionResponse
,
HttpStatus
.
CONFLICT
);
}
@ResponseStatus
(
HttpStatus
.
BAD_REQUEST
)
@ExceptionHandler
({
MethodArgumentNotValidException
.
class
})
public
ResponseEntity
<
Object
>
handle
(
MethodArgumentNotValidException
e
)
{
CustomExceptionResponse
customExceptionResponse
=
new
CustomExceptionResponse
();
customExceptionResponse
.
setTimestamp
(
String
.
valueOf
(
Timestamp
.
valueOf
(
LocalDateTime
.
now
())));
customExceptionResponse
.
setStatus
(
HttpStatus
.
CONFLICT
);
BindingResult
bindingResult
=
e
.
getBindingResult
();
customExceptionResponse
.
setError
(
bindingResult
.
getFieldErrors
().
stream
()
.
map
(
DefaultMessageSourceResolvable:
:
getDefaultMessage
).
collect
(
Collectors
.
toSet
()).
toString
());
return
new
ResponseEntity
<>(
customExceptionResponse
,
HttpStatus
.
BAD_REQUEST
);
}
}
This diff is collapsed.
Click to expand it.
student-managment-app/api/src/main/java/student/managment/app/app/controllers/CourseController.java
+
2
-
8
View file @
f16775f1
...
...
@@ -30,10 +30,7 @@ public class CourseController {
}
@PostMapping
public
ResponseEntity
<
CreateCourse
>
add
(
@Valid
@RequestBody
CreateCourse
createCourse
,
BindingResult
bindingResult
)
{
if
(
bindingResult
.
hasErrors
())
{
throw
new
IllegalArgumentException
(
bindingResult
.
getFieldError
().
getDefaultMessage
().
toUpperCase
());
}
public
ResponseEntity
<
CreateCourse
>
add
(
@Valid
@RequestBody
CreateCourse
createCourse
)
{
return
new
ResponseEntity
<>(
courseService
.
add
(
createCourse
),
HttpStatus
.
CREATED
);
}
...
...
@@ -48,10 +45,7 @@ public class CourseController {
}
@PutMapping
(
"/add-grade"
)
public
ResponseEntity
<
GradeResponse
>
addGrade
(
@Valid
@RequestBody
AddGrade
addGrade
,
BindingResult
bindingResult
)
{
if
(
bindingResult
.
hasErrors
())
{
throw
new
IllegalArgumentException
(
bindingResult
.
getFieldError
().
getDefaultMessage
().
toUpperCase
());
}
public
ResponseEntity
<
GradeResponse
>
addGrade
(
@Valid
@RequestBody
AddGrade
addGrade
)
{
return
new
ResponseEntity
<>(
courseService
.
addGradeTo
(
addGrade
.
getCourseName
(),
addGrade
.
getFacultyNumber
(),
addGrade
.
getScore
()),
HttpStatus
.
CREATED
);
}
...
...
This diff is collapsed.
Click to expand it.
student-managment-app/api/src/main/java/student/managment/app/app/controllers/StudentController.java
+
7
-
10
View file @
f16775f1
...
...
@@ -24,21 +24,18 @@ public class StudentController {
this
.
myMapper
=
myMapper
;
}
@GetMapping
(
"/{
id
}"
)
public
ResponseEntity
<
StudentResponse
>
get
(
@PathVariable
Long
id
)
{
return
new
ResponseEntity
<>(
myMapper
.
fromStudentToStudentResponse
(
studentService
.
findBy
Id
(
id
).
get
()),
HttpStatus
.
OK
);
@GetMapping
(
"/{
facultyNumber
}"
)
public
ResponseEntity
<
StudentResponse
>
get
(
@PathVariable
Integer
facultyNumber
)
{
return
new
ResponseEntity
<>(
myMapper
.
fromStudentToStudentResponse
(
studentService
.
findBy
Number
(
facultyNumber
).
get
()),
HttpStatus
.
OK
);
}
@PostMapping
public
ResponseEntity
<
CreateStudent
>
add
(
@Valid
@RequestBody
CreateStudent
createStudent
,
BindingResult
bindingResult
)
{
if
(
bindingResult
.
hasErrors
())
{
throw
new
IllegalArgumentException
(
bindingResult
.
getFieldError
().
getDefaultMessage
().
toUpperCase
());
}
public
ResponseEntity
<
CreateStudent
>
add
(
@Valid
@RequestBody
CreateStudent
createStudent
)
{
return
new
ResponseEntity
<>(
studentService
.
add
(
createStudent
),
HttpStatus
.
CREATED
);
}
@GetMapping
(
"/average/{
id
}"
)
public
ResponseEntity
<
StudentGrade
>
getAverageGradeFor
(
@PathVariable
Long
id
)
{
return
new
ResponseEntity
<>(
studentService
.
getAverageGradeFor
(
id
),
HttpStatus
.
OK
);
@GetMapping
(
"/average/{
facultyNumber
}"
)
public
ResponseEntity
<
StudentGrade
>
getAverageGradeFor
(
@PathVariable
Integer
facultyNumber
)
{
return
new
ResponseEntity
<>(
studentService
.
getAverageGradeFor
(
facultyNumber
),
HttpStatus
.
OK
);
}
}
This diff is collapsed.
Click to expand it.
student-managment-app/api/src/main/java/student/managment/app/app/controllers/TeacherController.java
+
1
-
4
View file @
f16775f1
...
...
@@ -25,10 +25,7 @@ public class TeacherController {
}
@PostMapping
public
ResponseEntity
<
CreateTeacher
>
add
(
@Valid
@RequestBody
CreateTeacher
createTeacher
,
BindingResult
bindingResult
)
{
if
(
bindingResult
.
hasErrors
())
{
throw
new
IllegalArgumentException
(
bindingResult
.
getFieldError
().
getDefaultMessage
().
toUpperCase
());
}
public
ResponseEntity
<
CreateTeacher
>
add
(
@Valid
@RequestBody
CreateTeacher
createTeacher
)
{
return
new
ResponseEntity
<>(
teacherService
.
add
(
createTeacher
),
HttpStatus
.
CREATED
);
}
...
...
This diff is collapsed.
Click to expand it.
student-managment-app/data-model/src/main/java/student/managment/app/entities/Teacher.java
+
1
-
2
View file @
f16775f1
...
...
@@ -33,8 +33,7 @@ public class Teacher {
@OneToMany
(
mappedBy
=
"teacher"
,
fetch
=
FetchType
.
EAGER
,
cascade
=
CascadeType
.
PERSIST
,
orphanRemoval
=
true
)
private
Set
<
Course
>
courses
=
new
HashSet
<>();
public
Teacher
()
{
}
public
Teacher
()
{}
public
Integer
getEmployeeNumber
()
{
return
employeeNumber
;
...
...
This diff is collapsed.
Click to expand it.
student-managment-app/data-model/src/main/java/student/managment/app/entities/dtos/create_dtos/CreateTeacher.java
+
6
-
4
View file @
f16775f1
package
student.managment.app.entities.dtos.create_dtos
;
import
jakarta.validation.constraints.Min
;
import
jakarta.validation.constraints.NotBlank
;
import
jakarta.validation.constraints.NotNull
;
import
jakarta.validation.constraints.Size
;
import
jakarta.persistence.EnumType
;
import
jakarta.persistence.Enumerated
;
import
jakarta.validation.constraints.*
;
import
lombok.AllArgsConstructor
;
import
lombok.Getter
;
import
lombok.NoArgsConstructor
;
import
lombok.Setter
;
import
student.managment.app.entities.enums.DegreeType
;
@Getter
@Setter
...
...
@@ -21,8 +21,10 @@ public class CreateTeacher {
private
String
name
;
@NotNull
(
message
=
"Degree can't be null"
)
@Enumerated
(
value
=
EnumType
.
STRING
)
private
String
degree
;
@NotNull
(
message
=
"Employee number can't be null"
)
@Positive
(
message
=
"Employee number must be positive"
)
private
Integer
employeeNumber
;
}
This diff is collapsed.
Click to expand it.
student-managment-app/data-model/src/main/java/student/managment/app/entities/enums/DegreeType.java
+
0
-
8
View file @
f16775f1
...
...
@@ -12,12 +12,4 @@ public enum DegreeType {
public
String
getLabel
()
{
return
label
;
}
public
static
DegreeType
getTypeByLabel
(
String
label
)
{
try
{
return
Enum
.
valueOf
(
DegreeType
.
class
,
label
.
toUpperCase
());
}
catch
(
IllegalArgumentException
|
NullPointerException
e
)
{
return
null
;
}
}
}
This diff is collapsed.
Click to expand it.
student-managment-app/services/src/main/java/student/managment/app/services/course/CourseGroupService.java
+
3
-
0
View file @
f16775f1
...
...
@@ -6,6 +6,9 @@ import student.managment.app.entities.Course;
import
student.managment.app.entities.Student
;
import
student.managment.app.entities.dtos.course_dtos.CourseGroup
;
import
student.managment.app.services.grade.GradeService
;
import
java.math.BigDecimal
;
import
java.math.RoundingMode
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.stream.Collectors
;
...
...
This diff is collapsed.
Click to expand it.
student-managment-app/services/src/main/java/student/managment/app/services/course/CourseService.java
+
5
-
7
View file @
f16775f1
...
...
@@ -16,6 +16,9 @@ import student.managment.app.services.grade.GradeService;
import
student.managment.app.services.mappers.MyMapper
;
import
student.managment.app.services.student.StudentService
;
import
student.managment.app.services.teacher.TeacherService
;
import
java.math.BigDecimal
;
import
java.math.RoundingMode
;
import
java.util.*
;
import
static
student
.
managment
.
app
.
services
.
Constants
.*;
...
...
@@ -120,7 +123,6 @@ public class CourseService {
course
.
addStudent
(
student
);
courseRepository
.
saveAndFlush
(
course
);
studentService
.
addCourse
(
facultyNumber
,
course
);
return
myMapper
.
courseToCourseResponse
(
course
);
}
...
...
@@ -161,11 +163,6 @@ public class CourseService {
.
toList
();
}
private
boolean
hasGradeInCourse
(
Student
student
,
Course
course
)
{
return
course
.
getGrades
().
stream
()
.
anyMatch
(
grade
->
grade
.
getStudent
().
equals
(
student
));
}
public
CourseAverageGrade
getAverageGradeForCourse
(
String
courseName
)
{
Optional
<
Course
>
course
=
findByName
(
courseName
);
...
...
@@ -174,8 +171,9 @@ public class CourseService {
}
Double
averageGrade
=
courseRepository
.
findAverageGradeForCourse
(
course
.
get
()).
orElse
(
0.00
);
BigDecimal
result
=
BigDecimal
.
valueOf
(
averageGrade
).
setScale
(
2
,
RoundingMode
.
HALF_EVEN
);
return
new
CourseAverageGrade
(
courseName
,
averageGrade
);
return
new
CourseAverageGrade
(
courseName
,
result
.
doubleValue
()
);
}
public
List
<
Course
>
getAll
()
{
...
...
This diff is collapsed.
Click to expand it.
student-managment-app/services/src/main/java/student/managment/app/services/grade/GradeService.java
+
5
-
1
View file @
f16775f1
...
...
@@ -6,6 +6,9 @@ import student.managment.app.entities.Course;
import
student.managment.app.entities.Grade
;
import
student.managment.app.entities.Student
;
import
student.managment.app.repositories.contracts.GradeRepository
;
import
java.math.BigDecimal
;
import
java.math.RoundingMode
;
import
java.util.Optional
;
@Service
...
...
@@ -28,6 +31,7 @@ public class GradeService {
}
public
double
findAverageGradeForStudentInCourse
(
Student
student
,
Course
course
)
{
return
gradeRepository
.
findAverageGradeForStudentInCourse
(
student
,
course
);
BigDecimal
result
=
BigDecimal
.
valueOf
(
gradeRepository
.
findAverageGradeForStudentInCourse
(
student
,
course
)).
setScale
(
2
,
RoundingMode
.
HALF_EVEN
);
return
result
.
doubleValue
();
}
}
This diff is collapsed.
Click to expand it.
student-managment-app/services/src/main/java/student/managment/app/services/student/StudentService.java
+
6
-
23
View file @
f16775f1
...
...
@@ -12,6 +12,8 @@ import student.managment.app.repositories.contracts.StudentRepository;
import
student.managment.app.services.grade.GradeService
;
import
student.managment.app.services.mappers.MyMapper
;
import
java.math.BigDecimal
;
import
java.math.RoundingMode
;
import
java.util.Optional
;
@Service
...
...
@@ -57,28 +59,8 @@ public class StudentService {
return
studentRepository
.
findStudentByFacultyNumber
(
facultyNumber
);
}
public
void
addCourse
(
Integer
facultyNumber
,
Course
course
)
{
Optional
<
Student
>
studentOptional
=
findByNumber
(
facultyNumber
);
if
(
studentOptional
.
isEmpty
())
{
throw
new
IllegalArgumentException
();
}
Student
student
=
studentOptional
.
get
();
if
(
student
.
getCourses
().
contains
(
course
))
{
throw
new
IllegalArgumentException
(
"Course is already associated with the student."
);
}
student
.
add
(
course
);
// studentRepository.saveAndFlush(student);
// why this works
}
public
StudentGrade
getAverageGradeFor
(
Long
id
)
{
Optional
<
Student
>
student
=
findById
(
id
);
public
StudentGrade
getAverageGradeFor
(
Integer
facultyNumber
)
{
Optional
<
Student
>
student
=
findByNumber
(
facultyNumber
);
if
(
student
.
isEmpty
())
{
throw
new
EntityNotFoundException
(
"No student with given faculty number"
);
...
...
@@ -86,8 +68,9 @@ public class StudentService {
Optional
<
Double
>
grade
=
gradeService
.
getAverageGradeForStudent
(
student
.
get
());
BigDecimal
result
=
BigDecimal
.
valueOf
(
grade
.
orElse
(
0.0
)).
setScale
(
2
,
RoundingMode
.
HALF_EVEN
);
return
myMapper
.
studentAndGradeToStudentGrade
(
student
.
get
(),
grade
.
orElse
(
0.0
));
return
myMapper
.
studentAndGradeToStudentGrade
(
student
.
get
(),
result
.
doubleValue
(
));
}
}
This diff is collapsed.
Click to expand it.
student-managment-app/services/src/main/java/student/managment/app/services/teacher/TeacherService.java
+
1
-
0
View file @
f16775f1
...
...
@@ -5,6 +5,7 @@ import org.springframework.stereotype.Service;
import
student.managment.app.entities.Course
;
import
student.managment.app.entities.Teacher
;
import
student.managment.app.entities.dtos.create_dtos.CreateTeacher
;
import
student.managment.app.entities.enums.DegreeType
;
import
student.managment.app.entities.exception.DuplicateEntityException
;
import
student.managment.app.repositories.contracts.TeacherRepository
;
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
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