Prechádzať zdrojové kódy

1.generation type identity
2.jsonproperty write only

athrainsky 1 rok pred
rodič
commit
150d2e9f76

+ 14 - 14
src/main/kotlin/com/swagger/rest/controllers/UserController.kt

@@ -3,18 +3,17 @@ package com.swagger.rest.controllers
 import com.swagger.rest.models.User
 import com.swagger.rest.models.UserInput
 import com.swagger.rest.repositories.UserRepository
-import org.springframework.beans.factory.annotation.Autowired
 import org.springframework.http.HttpStatus
 import org.springframework.http.ResponseEntity
 import org.springframework.security.crypto.password.PasswordEncoder
 import org.springframework.web.bind.annotation.*
-import java.util.*
 
 @RestController
 @RequestMapping("/api/v1")
-class UserController(private val userRepository: UserRepository) {
-    @Autowired
-    private val passwordEncoder: PasswordEncoder? = null
+class UserController(
+    private val userRepository: UserRepository,
+    private val passwordEncoder: PasswordEncoder
+) {
 
     @GetMapping("/users")
     fun getUser(@RequestParam(required = false) username: String?): Any {
@@ -36,7 +35,7 @@ class UserController(private val userRepository: UserRepository) {
 
     @GetMapping("/users/{id}")
     fun getUserById(@PathVariable("id") id: Long): ResponseEntity<User> {
-        val userData: Optional<User?> = userRepository.findById(id)
+        val userData = userRepository.findById(id)
         return if (userData.isPresent) {
             ResponseEntity<User>(userData.get(), HttpStatus.OK)
         } else {
@@ -45,19 +44,20 @@ class UserController(private val userRepository: UserRepository) {
     }
 
     @PostMapping("/users")
-    fun addUser(@RequestBody user: User): ResponseEntity<User> {
+    fun addUser(@RequestBody user: UserInput): ResponseEntity<User> {
         return try {
             val found = userRepository.findByUsernameContaining(user.username).size
-            if (user.username.isNotBlank()) {
-                if (user.username.length > 100 || user.password.length > 100 || user.name.length > 255) {
+            if (user.username!!.isNotBlank()) {
+                if (user.username!!.length > 100 || user.password!!.length > 100 || user.name!!.length > 255) {
                     ResponseEntity<User>(HttpStatus.PAYLOAD_TOO_LARGE)
                 } else if (found > 0) {
                     ResponseEntity<User>(HttpStatus.CONFLICT)
                 } else {
-                    user.username = user.username.trim()
-                    user.password = passwordEncoder!!.encode(user.password)
-                    val saveUser = userRepository.save(user)
-                    ResponseEntity<User>(saveUser, HttpStatus.CREATED)
+                    val saveUser = User()
+                    saveUser.username = user.username!!.trim()
+                    saveUser.password = passwordEncoder.encode(user.password)
+                    saveUser.name = user.name!!
+                    ResponseEntity<User>(userRepository.save(saveUser), HttpStatus.CREATED)
                 }
             } else {
                 ResponseEntity<User>(HttpStatus.BAD_REQUEST)
@@ -113,7 +113,7 @@ class UserController(private val userRepository: UserRepository) {
             if (userInput.newPassword!!.length > 100) {
                 ResponseEntity<User>(HttpStatus.PAYLOAD_TOO_LARGE)
             } else if (userData.isPresent) {
-                if (passwordEncoder!!.matches(userInput.oldPassword, userData.get().password)) {
+                if (passwordEncoder.matches(userInput.oldPassword, userData.get().password)) {
                     val saveUser = userData.get()
                     saveUser.password = passwordEncoder.encode(userInput.newPassword)
                     ResponseEntity<Any?>(userRepository.save(saveUser), HttpStatus.OK)

+ 1 - 1
src/main/kotlin/com/swagger/rest/models/Platform.kt

@@ -6,7 +6,7 @@ import jakarta.persistence.*
 @Table(name = "platform")
 class Platform {
     @Id
-    @Column(name = "platform_id")
+    @Column(name = "platform_id", updatable = false, nullable = false)
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     var id: Long = 0
 

+ 1 - 1
src/main/kotlin/com/swagger/rest/models/Project.kt

@@ -8,7 +8,7 @@ import jakarta.persistence.*
 @Table(name = "project")
 class Project {
     @Id
-    @Column(name = "project_id")
+    @Column(name = "project_id", updatable = false, nullable = false)
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     var id: Long = 0
 

+ 4 - 1
src/main/kotlin/com/swagger/rest/models/User.kt

@@ -1,18 +1,21 @@
 package com.swagger.rest.models
 
+import com.fasterxml.jackson.annotation.JsonProperty
 import jakarta.persistence.*
 
+
 @Entity
 @Table(name = "user")
 class User {
     @Id
-    @Column(name = "user_id")
+    @Column(name = "user_id", updatable = false, nullable = false)
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     var id: Long = 0
 
     @Column(name = "username", length = 100)
     var username: String = ""
 
+    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
     @Column(name = "password", length = 100)
     var password: String = ""
 

+ 6 - 0
src/main/kotlin/com/swagger/rest/models/UserInput.kt

@@ -5,4 +5,10 @@ class UserInput {
     var oldPassword: String? = ""
 
     var newPassword: String? = ""
+
+    var username: String? = ""
+
+    var password: String? = ""
+
+    var name: String? = ""
 }

+ 3 - 5
src/main/resources/application.properties

@@ -1,6 +1,4 @@
-spring.datasource.url= jdbc:mysql://localhost:3306/swagger?useSSL=false
+spring.jpa.hibernate.ddl-auto=update
+spring.datasource.url= jdbc:mysql://localhost:3306/swagger
 spring.datasource.username= root
-spring.datasource.password= root
-
-spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQLDialect
-spring.jpa.hibernate.ddl-auto= update
+spring.datasource.password= root