Skip to content

Commit

Permalink
Adding class validation
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoleNG18 committed Apr 15, 2024
1 parent 1822f64 commit fbeff57
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws
.requestMatchers("/schools/**","/users/roles/**").hasRole(UserRoleEnum.SUPERADMIN.name())

.requestMatchers("/classes/add","/classes/edit/**",
"/classes/edited/**","/classes/delete/**","/students/**").hasRole(UserRoleEnum.ADMIN.name())
"/classes/edited/**","/classes/delete/**","/students/**","/classes/{idTeacher}/{id}","/classes").hasRole(UserRoleEnum.ADMIN.name())

.requestMatchers("/denseBall", "/jump",
"/thirty", "/twoHundred",
"/tTest","/classes/{idTeacher}/{id}").hasRole(UserRoleEnum.USER.name())
"/tTest","/classes/{idTeacher}/{id}","/classes").hasRole(UserRoleEnum.USER.name())

// all other requests are authenticated.
.anyRequest().authenticated()
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/pmgkn/pescores/pescores/service/ClassesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,16 @@ public List<ClassViewDto> getAllClassesInSchool(String userEmail) {

return classesInSchool.stream().map(c -> this.modelMapper.map(c, ClassViewDto.class)).collect(Collectors.toList());
}

public boolean checkIfClassNameRepeats(String name,
String className) {
for (ClassEntity classEntity : this.userService.getUserByEmail(name).getSchool().getClasses()) {
if(classEntity.getClassName().equals(className)){
return true;
}
}

return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public String postAddClass(@Valid ClassAddBindingDto classAddBindingDto,
return "redirect:/classes/add";
}

if (this.classesService.checkIfClassNameRepeats(principal.getName(), classAddBindingDto.getClassName())) {

redirectAttributes.addFlashAttribute("isUnique", false);

return "redirect:/classes/add";
}

UUID classId = this.classesService.saveClass(classAddBindingDto, classAddBindingDto.getTeacher());
UserEntity currentUser = this.userService.getUserByEmail(principal.getName());

Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/i18n/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ add_class_grade_number= Grade number
add_class_err_classNum=Grade number is required
add_class_teacher= Teacher
add_class_submit=Submit
class_add_unique=Class name must be unique in school
student_add=Add student
student_add_name=Student name
Expand All @@ -119,7 +120,7 @@ current_class_t_test=T-test(s)
current_class_200m=200m(s)
current_class_30m=30m(s)
current_class_name=Name
current_class_classes=Classes
current_class_classes=My classes
current_class_submit=Submit
current_class_delete=Delete
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/i18n/messages_bg.properties
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ add_class_grade_number=Номер на клас
add_class_err_classNum=Номерът на класа е задължителен
add_class_teacher=Учител
add_class_submit=Добави
class_add_unique=Класът трябва да е уникален в училището

student_add=Добави студент
student_add_name=Име и фамилия
Expand All @@ -118,7 +119,7 @@ current_class_t_test=T-тест (секунди)
current_class_200m=200м (секунди)
current_class_30m=30м (секунди)
current_class_name=Име
current_class_classes=Класове
current_class_classes=Моите класове
current_class_submit=Добави
current_class_delete=Изтрий

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/templates/add-class.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ <h3 th:text="#{add_class}">Add class</h3>
<div class="invalid-feedback errors-class alert-danger"
th:each="err :${#fields.errors('className')}"
th:text="#{add_class_err_class_name}"></div>
<div class="invalid-feedback errors-class alert-danger"
th:if="${isUnique} == false" th:text="#{class_add_unique}">class name must be unique
</div>

<label for="classNum" th:text="#{add_class_grade_number}">Grade Number</label>
<input type="number"
Expand Down

0 comments on commit fbeff57

Please sign in to comment.