diff --git a/src/main/java/pmgkn/pescores/pescores/config/SecurityConfiguration.java b/src/main/java/pmgkn/pescores/pescores/config/SecurityConfiguration.java index b59a2a0..4a56f22 100644 --- a/src/main/java/pmgkn/pescores/pescores/config/SecurityConfiguration.java +++ b/src/main/java/pmgkn/pescores/pescores/config/SecurityConfiguration.java @@ -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() diff --git a/src/main/java/pmgkn/pescores/pescores/service/ClassesService.java b/src/main/java/pmgkn/pescores/pescores/service/ClassesService.java index 0571e88..bda4204 100644 --- a/src/main/java/pmgkn/pescores/pescores/service/ClassesService.java +++ b/src/main/java/pmgkn/pescores/pescores/service/ClassesService.java @@ -192,4 +192,16 @@ public List 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; + } + } diff --git a/src/main/java/pmgkn/pescores/pescores/web/ClassesController.java b/src/main/java/pmgkn/pescores/pescores/web/ClassesController.java index a300f6f..c7225c4 100644 --- a/src/main/java/pmgkn/pescores/pescores/web/ClassesController.java +++ b/src/main/java/pmgkn/pescores/pescores/web/ClassesController.java @@ -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()); diff --git a/src/main/resources/i18n/messages.properties b/src/main/resources/i18n/messages.properties index c9115e8..e25ba83 100644 --- a/src/main/resources/i18n/messages.properties +++ b/src/main/resources/i18n/messages.properties @@ -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 @@ -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 diff --git a/src/main/resources/i18n/messages_bg.properties b/src/main/resources/i18n/messages_bg.properties index 3da7c88..9b95643 100644 --- a/src/main/resources/i18n/messages_bg.properties +++ b/src/main/resources/i18n/messages_bg.properties @@ -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=Име и фамилия @@ -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=Изтрий diff --git a/src/main/resources/templates/add-class.html b/src/main/resources/templates/add-class.html index 9ce64ec..20bf429 100644 --- a/src/main/resources/templates/add-class.html +++ b/src/main/resources/templates/add-class.html @@ -82,6 +82,9 @@

Add class

+
class name must be unique +