浏览代码

hanya owner atau member

athrainsky 11 月之前
父节点
当前提交
f76fddd09c

+ 4 - 10
src/main/kotlin/com/swagger/rest/controllers/PlatformController.kt

@@ -40,11 +40,12 @@ class PlatformController(
         @RequestParam(required = false) project: String?,
         @RequestParam(defaultValue = 0.toString()) page: Int,
         @RequestParam(defaultValue = 3.toString()) limit: Int,
-        @RequestParam(defaultValue = "id, desc") sort: Array<String>
+        @RequestParam(defaultValue = "platform_id, desc") sort: Array<String>
     ): Any {
         return try {
+            val userId = userRepository.getUserByUsername(SecurityContextHolder.getContext().authentication.name)
             val orders: MutableList<Sort.Order> = ArrayList()
-            val column = listOf("id", "name", "project_id")
+            val column = listOf("platform_id", "name", "project_id")
             val sort2 = if (!sort.contains(",")) {
                 sort + ",desc"
             } else {
@@ -65,14 +66,7 @@ class PlatformController(
                     orders.add(Sort.Order(getSortDirection(sort2[1]), sort2[0]))
                 }
                 val pagingSort: Pageable = PageRequest.of(page, limit, Sort.by(orders as List<Sort.Order>))
-                val spec = Specification<Platform> { root, query, builder ->
-                    val list: MutableList<Predicate> = mutableListOf()
-                    project?.let {
-                        list.add(builder.equal(root.get<Project>("project").get<Long>("id"), project))
-                    }
-                    builder.and(*list.toTypedArray())
-                }
-                val platforms = platformRepository.findAll(spec, pagingSort)
+                val platforms = platformRepository.findOwnerOrMember(userId.id.toString(), pagingSort)
                 val ret: List<Platform?> = platforms.content
                 val response: MutableMap<String, Any> = HashMap()
                 response["currentPage"] = platforms.number

+ 6 - 3
src/main/kotlin/com/swagger/rest/controllers/ProjectController.kt

@@ -5,9 +5,11 @@ import com.swagger.rest.repositories.MemberRepository
 import com.swagger.rest.repositories.PlatformRepository
 import com.swagger.rest.repositories.ProjectRepository
 import com.swagger.rest.repositories.UserRepository
+import jakarta.persistence.criteria.Predicate
 import org.springframework.data.domain.PageRequest
 import org.springframework.data.domain.Pageable
 import org.springframework.data.domain.Sort
+import org.springframework.data.jpa.domain.Specification
 import org.springframework.http.HttpStatus
 import org.springframework.http.ResponseEntity
 import org.springframework.security.core.context.SecurityContextHolder
@@ -35,11 +37,12 @@ class ProjectController(
     fun getProject(
         @RequestParam(defaultValue = 0.toString()) page: Int,
         @RequestParam(defaultValue = 3.toString()) limit: Int,
-        @RequestParam(defaultValue = "id, desc") sort: Array<String>
+        @RequestParam(defaultValue = "project_id, desc") sort: Array<String>
     ): Any {
+        val userId = userRepository.getUserByUsername(SecurityContextHolder.getContext().authentication.name)
         return try {
             val orders: MutableList<Sort.Order> = ArrayList()
-            val column = listOf("id", "name", "description", "owner")
+            val column = listOf("project_id", "name", "description", "owner")
             val sort2 = if (!sort.contains(",")) {
                 sort + ",desc"
             } else {
@@ -60,7 +63,7 @@ class ProjectController(
                     orders.add(Sort.Order(getSortDirection(sort2[1]), sort2[0]))
                 }
                 val pagingSort: Pageable = PageRequest.of(page, limit, Sort.by(orders))
-                val projects = projectRepository.findAll(pagingSort)
+                val projects = projectRepository.findOwnerOrMember(userId.id.toString(), pagingSort)
                 val ret = projects.content
                 val response: MutableMap<String, Any> = HashMap()
                 response["currentPage"] = projects.number

+ 6 - 0
src/main/kotlin/com/swagger/rest/repositories/PlatformRepository.kt

@@ -1,6 +1,9 @@
 package com.swagger.rest.repositories
 
 import com.swagger.rest.models.Platform
+import com.swagger.rest.models.Project
+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
@@ -11,4 +14,7 @@ interface PlatformRepository : JpaRepository<Platform, Long>, JpaSpecificationEx
 
     @Query("SELECT f.* FROM platform f WHERE project_id=?1", nativeQuery = true)
     fun findByProjectId(project: String?): List<Platform>
+
+    @Query("SELECT * FROM platform WHERE project_id IN (SELECT project_id FROM project WHERE OWNER=?1) OR project_id IN (SELECT project_id FROM project_member WHERE user_id=?1)", nativeQuery = true)
+    fun findOwnerOrMember(user: String, pageable: Pageable): Page<Platform>
 }

+ 8 - 2
src/main/kotlin/com/swagger/rest/repositories/ProjectRepository.kt

@@ -1,14 +1,20 @@
 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> {
-    fun findByNameContaining(name: String?, pageable: Pageable?): Page<Project>
+interface ProjectRepository : JpaRepository<Project, Long>, JpaSpecificationExecutor<Project> {
+
+    @Query("SELECT * FROM project f WHERE OWNER=?1 OR (project_id IN (SELECT project_id FROM project_member WHERE user_id=?1))", nativeQuery = true)
+    fun findOwnerOrMember(user: String, pageable: Pageable):Page<Project>
 
     fun findByName(name: String?): Project
 

+ 5 - 1
swagger3 project.yml

@@ -30,7 +30,11 @@ paths:
       parameters: 
         - $ref: '#/components/parameters/page'
         - $ref: '#/components/parameters/limit'
-        - $ref: '#/components/parameters/sortQuery'
+        - name: sort
+          in: query
+          description: sort direction (default 'project_id, desc')
+          schema:
+            type: string
       responses:
         200:
           $ref: '#/components/responses/getArrayProject'