Ver código fonte

1.image-url tidak bisa kosong, padahal tidak required
2.content comment bisa kosong padahal required
3.get bug dari project/platform

athrainsky 10 meses atrás
pai
commit
d027c10a18

+ 159 - 4
src/main/kotlin/com/swagger/rest/controllers/BugController.kt

@@ -1,8 +1,6 @@
 package com.swagger.rest.controllers
 
-import com.swagger.rest.models.Bug
-import com.swagger.rest.models.BugInput
-import com.swagger.rest.models.BugOutput
+import com.swagger.rest.models.*
 import com.swagger.rest.models.Enum
 import com.swagger.rest.repositories.*
 import jakarta.persistence.criteria.Predicate
@@ -130,6 +128,163 @@ class BugController(
         }
     }
 
+    @GetMapping("/projects/{id}/bug")
+    fun getBugByProjectId(
+        @PathVariable("id") id: Long,
+        @RequestParam(defaultValue = 0.toString()) page: Int,
+        @RequestParam(defaultValue = 3.toString()) limit: Int,
+        @RequestParam(defaultValue = "bug_id, desc") sort: Array<String>
+    ): Any {
+        return try {
+            val orders: MutableList<Sort.Order> = ArrayList()
+            val column = listOf(
+                "bug_id",
+                "created",
+                "description",
+                "qc",
+                "dev",
+                "platform",
+                "goodday_url",
+                "image_url",
+                "level",
+                "status",
+                "dev_status"
+            )
+            val sort2 = if (!sort.contains(",")) {
+                sort + ",desc"
+            } else {
+                sort
+            }
+            if (!column.contains(sort2[0])) {
+                ResponseEntity<Bug>(HttpStatus.BAD_REQUEST)
+            } else {
+                if (sort2[0].contains(",")) {
+                    // will sort more than 2 fields
+                    // sortOrder="field, direction"
+                    for (sortOrder in sort2) {
+                        val _sort = sortOrder.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
+                        orders.add(Sort.Order(getSortDirection(_sort[1]), _sort[0]))
+                    }
+                } else {
+                    // sort=[field, direction]
+                    orders.add(Sort.Order(getSortDirection(sort2[1]), sort2[0]))
+                }
+                val pagingSort: Pageable = PageRequest.of(page, limit, Sort.by(orders))
+                val bugs = bugRepository.findByProject(id.toString(), pagingSort)
+                val output = bugs.map {
+                    BugOutput(
+                        id = it.id,
+                        created = SimpleDateFormat("dd-MMM-yy HH:mm:ss").format(it.created),
+                        description = it.description,
+                        qc = it.qc!!.name,
+                        dev = it.dev!!.name,
+                        platform = it.platform!!.name,
+                        goodday_url = it.goodday_url,
+                        image_url = it.image_url,
+                        level = Enum.Level.values()[it.level],
+                        status = Enum.Status.values()[it.status],
+                        dev_status = Enum.Dev_Status.values()[it.dev_status]
+                    )
+                }
+                val ret = output.content
+                val response: MutableMap<String, Any> = HashMap()
+                response["currentPage"] = output.number
+                response["totalRecord"] = output.totalElements
+                response["totalPage"] = output.totalPages
+                response["results"] = ret
+                if (ret.isNotEmpty()) {
+                    ResponseEntity(response, HttpStatus.OK)
+                } else {
+                    arrayOf<String>()
+                }
+            }
+        } catch (e: Exception) {
+            e.printStackTrace()
+            ResponseEntity(e, HttpStatus.INTERNAL_SERVER_ERROR)
+        }
+    }
+
+    @GetMapping("/platforms/{id}/bug")
+    fun getBugByPlatformId(
+        @PathVariable("id") id: Long,
+        @RequestParam(defaultValue = 0.toString()) page: Int,
+        @RequestParam(defaultValue = 3.toString()) limit: Int,
+        @RequestParam(defaultValue = "id, desc") sort: Array<String>
+    ): Any {
+        return try {
+            val orders: MutableList<Sort.Order> = ArrayList()
+            val column = listOf(
+                "id",
+                "created",
+                "description",
+                "qc",
+                "dev",
+                "platform",
+                "goodday_url",
+                "image_url",
+                "level",
+                "status",
+                "dev_status"
+            )
+            val sort2 = if (!sort.contains(",")) {
+                sort + ",desc"
+            } else {
+                sort
+            }
+            if (!column.contains(sort2[0])) {
+                ResponseEntity<Bug>(HttpStatus.BAD_REQUEST)
+            } else {
+                if (sort2[0].contains(",")) {
+                    // will sort more than 2 fields
+                    // sortOrder="field, direction"
+                    for (sortOrder in sort2) {
+                        val _sort = sortOrder.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
+                        orders.add(Sort.Order(getSortDirection(_sort[1]), _sort[0]))
+                    }
+                } else {
+                    // sort=[field, direction]
+                    orders.add(Sort.Order(getSortDirection(sort2[1]), sort2[0]))
+                }
+                val pagingSort: Pageable = PageRequest.of(page, limit, Sort.by(orders))
+                val spec = Specification<Bug> { root, query, builder ->
+                    val list: MutableList<Predicate> = mutableListOf()
+                    list.add(builder.equal(root.get<Platform>("platform").get<Long>("id"), id))
+                    builder.and(*list.toTypedArray())
+                }
+                val bugs = bugRepository.findAll(spec, pagingSort)
+                val output = bugs.map {
+                    BugOutput(
+                        id = it.id,
+                        created = SimpleDateFormat("dd-MMM-yy HH:mm:ss").format(it.created),
+                        description = it.description,
+                        qc = it.qc!!.name,
+                        dev = it.dev!!.name,
+                        platform = it.platform!!.name,
+                        goodday_url = it.goodday_url,
+                        image_url = it.image_url,
+                        level = Enum.Level.values()[it.level],
+                        status = Enum.Status.values()[it.status],
+                        dev_status = Enum.Dev_Status.values()[it.dev_status]
+                    )
+                }
+                val ret = output.content
+                val response: MutableMap<String, Any> = HashMap()
+                response["currentPage"] = output.number
+                response["totalRecord"] = output.totalElements
+                response["totalPage"] = output.totalPages
+                response["results"] = ret
+                if (ret.isNotEmpty()) {
+                    ResponseEntity(response, HttpStatus.OK)
+                } else {
+                    arrayOf<String>()
+                }
+            }
+        } catch (e: Exception) {
+            e.printStackTrace()
+            ResponseEntity(e, HttpStatus.INTERNAL_SERVER_ERROR)
+        }
+    }
+
     @GetMapping("/bugs/{id}")
     fun getBugById(@PathVariable("id") id: Long): Any {
         val bugData = bugRepository.findById(id)
@@ -174,7 +329,7 @@ class BugController(
                     it.name == bugInput.status.uppercase()
                 } || !enumValues<Enum.Dev_Status>().any { it.name == bugInput.dev_status.uppercase() }) {
                 ResponseEntity(HttpStatus.BAD_REQUEST)
-            } else if (!regex(bugInput.goodday_url.trim()) || !regex(bugInput.image_url.trim())) {
+            } else if (!regex(bugInput.goodday_url.trim()) || (bugInput.image_url.isNotEmpty() && !regex(bugInput.image_url.trim()))) {
                 ResponseEntity(HttpStatus.BAD_REQUEST)
             } else if (countPlat == 0) {
                 ResponseEntity(HttpStatus.NOT_FOUND)

+ 3 - 1
src/main/kotlin/com/swagger/rest/controllers/CommentController.kt

@@ -120,7 +120,9 @@ class CommentController(
         return try {
             val userId = userRepository.getUserByUsername(SecurityContextHolder.getContext().authentication.name)
             val found = bugRepository.findById(id)
-            if (commentInput.content.length > 500) {
+            if  (commentInput.content.isEmpty()){
+                ResponseEntity(HttpStatus.BAD_REQUEST)
+            } else if (commentInput.content.length > 500) {
                 ResponseEntity(HttpStatus.PAYLOAD_TOO_LARGE)
             } else {
                 if (found.isPresent) {

+ 1 - 0
src/main/kotlin/com/swagger/rest/controllers/PlatformController.kt

@@ -225,4 +225,5 @@ class PlatformController(
             ResponseEntity(e, HttpStatus.INTERNAL_SERVER_ERROR)
         }
     }
+
 }

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

@@ -1,7 +1,6 @@
 package com.swagger.rest.models
 
 import jakarta.persistence.Column
-import java.util.*
 
 class CommentInput {
 

+ 5 - 0
src/main/kotlin/com/swagger/rest/repositories/BugRepository.kt

@@ -1,6 +1,8 @@
 package com.swagger.rest.repositories
 
 import com.swagger.rest.models.Bug
+import org.springframework.data.domain.Page
+import org.springframework.data.domain.Pageable
 import org.springframework.data.jpa.repository.JpaRepository
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor
 import org.springframework.data.jpa.repository.Query
@@ -10,4 +12,7 @@ interface BugRepository : JpaRepository<Bug, Long>, JpaSpecificationExecutor<Bug
     @Query("SELECT COUNT(0) FROM bug WHERE bug_id=?2 AND platform IN (SELECT platform_id FROM platform WHERE project_id IN (SELECT project_id FROM project WHERE OWNER=?1 UNION SELECT project_id FROM project_member WHERE role=2 AND user_id=?1))", nativeQuery = true)
     fun validOwnerAdmin(owner: String?, id: Long): Int
 
+    @Query("SELECT * FROM bug WHERE platform IN (SELECT platform_id FROM platform WHERE project_id=?1)", nativeQuery = true)
+    fun findByProject(project: String, pageable: Pageable): Page<Bug>
+
 }

+ 0 - 2
src/main/kotlin/com/swagger/rest/repositories/MemberRepository.kt

@@ -1,7 +1,5 @@
 package com.swagger.rest.repositories
 
-import com.swagger.rest.models.Enum
-import com.swagger.rest.models.Platform
 import com.swagger.rest.models.ProjectMember
 import org.springframework.data.jpa.repository.JpaRepository
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor

+ 0 - 3
src/main/kotlin/com/swagger/rest/repositories/ProjectRepository.kt

@@ -1,14 +1,11 @@
 package com.swagger.rest.repositories
 
-import com.swagger.rest.models.Platform
 import com.swagger.rest.models.Project
-import com.swagger.rest.models.User
 import org.springframework.data.domain.Page
 import org.springframework.data.domain.Pageable
 import org.springframework.data.jpa.repository.JpaRepository
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor
 import org.springframework.data.jpa.repository.Query
-import org.springframework.security.access.method.P
 
 
 interface ProjectRepository : JpaRepository<Project, Long>, JpaSpecificationExecutor<Project> {

+ 46 - 0
swagger3 project.yml

@@ -218,6 +218,31 @@ paths:
           $ref: '#/components/responses/404'
       security: 
         - testAuth: []
+  /projects/{projectId}/bug:
+    get:
+      tags:
+      - projects
+      summary: find all bugs of a project
+      description: return all bugs of a project
+      operationId: getBugByProjectId
+      parameters: 
+        - $ref: '#/components/parameters/projectPath'
+        - $ref: '#/components/parameters/page'
+        - $ref: '#/components/parameters/limit'
+        - name: sort
+          in: query
+          description: sort direction (default 'bug_id, desc')
+          schema:
+            type: string
+      responses:
+        200:
+          $ref: '#/components/responses/getArrayBug'
+        400:
+          $ref: '#/components/responses/400'
+        401:
+          $ref: '#/components/responses/UnauthorizedError'
+      security: 
+      - testAuth: []
   /platforms:
     get:
       tags: 
@@ -334,6 +359,27 @@ paths:
           $ref: '#/components/responses/404'
       security: 
         - testAuth: []
+  /platforms/{platformId}/bug:
+    get:
+      tags: 
+      - platforms
+      summary: find all bugs of a platform
+      description: return all bugs of a platform
+      operationId: getBugByPlatformId
+      parameters: 
+        - $ref: '#/components/parameters/platformPath'
+        - $ref: '#/components/parameters/page'
+        - $ref: '#/components/parameters/limit'
+        - $ref: '#/components/parameters/sortQuery'
+      responses:
+        200:
+          $ref: '#/components/responses/getArrayBug'
+        400:
+          $ref: '#/components/responses/400'
+        401:
+          $ref: '#/components/responses/UnauthorizedError'
+      security: 
+      - testAuth: []
   /users:
     get:
       tags: