athrainsky 1 ano atrás
pai
commit
5424b1887d

+ 9 - 22
src/main/kotlin/com/server/alb/controllers/BugController.kt

@@ -7,7 +7,6 @@ import com.server.alb.services.BugValidationService.Companion.isValid
 import com.server.alb.services.ProjectService
 import com.server.alb.services.UserService
 import jakarta.servlet.http.HttpServletRequest
-import jakarta.validation.ConstraintViolation
 import jakarta.validation.Valid
 import jakarta.validation.Validator
 import org.springframework.security.authentication.AnonymousAuthenticationToken
@@ -70,39 +69,28 @@ class BugController(
         val userRole = UserRole()
         model.addAttribute("userRole", userRole)
         model.addAttribute("role", getRole())
-        //println(1)
         return "reg"
     }
 
     @PostMapping(value = ["/reg"])
     fun saveRegister(
-        @Valid @ModelAttribute("userRole") userRole: UserRole, model: Model, bindingResult: BindingResult,
-        redirectAttributes: RedirectAttributes,
-    ): String {//println(2)
-        //todo validasi user kosong
+        @Valid @ModelAttribute("userRole") userRole: UserRole, bindingResult: BindingResult,
+        redirectAttributes: RedirectAttributes, model: Model,
+    ): String {
         val newUser = User()
         newUser.username = userRole.username
         newUser.password = passwordEncoder.encode(userRole.password)
         newUser.roles = userRole.role
         newUser.isEnabled = true
         println(newUser)
-        val result: Set<ConstraintViolation<UserRole>> = validator.validate(userRole)
-        return if (result.isNotEmpty()) {
+        return if (bindingResult.hasErrors()) {
             model.addAttribute("role", getRole())
             "reg"
         } else {
             userRepository.save(newUser)
-            redirectAttributes.addFlashAttribute("successMessage", "Success save user")
-            return "redirect:/login"
+            redirectAttributes.addFlashAttribute("successMessage", "Success save User")
+            "redirect:/login"
         }
-//        return if (bindingResult.hasErrors()) {
-//            model.addAttribute("role", getRole())
-//            "reg"
-//        } else {
-//            userRepository.save(newUser)
-//            redirectAttributes.addFlashAttribute("successMessage", "Success save User")
-//            "redirect:/login"
-//        }
     }
 
     @RequestMapping(value = ["/userList"])
@@ -126,13 +114,12 @@ class BugController(
 
     @PostMapping(value = ["/projNew"])
     fun saveProject(
-        @Valid @ModelAttribute("project") project: Project, model: Model, bindingResult: BindingResult,
+        @Valid @ModelAttribute("project") project: Project,
+        bindingResult: BindingResult,
         redirectAttributes: RedirectAttributes,
+        model: Model,
     ): String {
-        //todo validasi save project
-        println(bindingResult)
         return if (bindingResult.hasErrors()) {
-            model.addAttribute("project", project)
             println(project)
             "add_proj"
         } else {

+ 5 - 5
src/main/kotlin/com/server/alb/models/Project.kt

@@ -1,21 +1,21 @@
 package com.server.alb.models
 
 import jakarta.persistence.*
-import jakarta.validation.constraints.NotBlank
+import jakarta.validation.constraints.NotEmpty
 
 @Entity
 @Table(name = "projects")
-class Project {
+class Project
+ {
     @Id
     @Column(name = "project_id")
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     var id: Long = 0
 
-//    @Max(value = 2, message = "Maksimum 2 karakter")
-    @NotBlank(message = "Project Name tidak boleh kosong")
+    @NotEmpty(message = "Project Name tidak boleh kosong")
     var name: String? = null
 
-    @NotBlank(message = "Project Description tidak boleh kosong")
+    @NotEmpty(message = "Project Description tidak boleh kosong")
     var description: String? = null
 
     @OneToMany(mappedBy = "project", cascade = [CascadeType.ALL], orphanRemoval = true)