| 
					
				 | 
			
			
				@@ -2,14 +2,16 @@ package com.swagger.rest.controllers 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.swagger.rest.models.Platform 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.swagger.rest.models.PlatformInput 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.swagger.rest.models.Project 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 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 org.springframework.data.domain.Page 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+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 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -42,33 +44,50 @@ class PlatformController( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     ): Any { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         return try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             val orders: MutableList<Sort.Order> = ArrayList() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (sort[0].contains(",")) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                for (sortOrder in sort) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    val sort_ = sortOrder.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    orders.add(Sort.Order(getSortDirection(sort_[1]), sort_[0])) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                orders.add(Sort.Order(Sort.Direction.DESC, sort[0])) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            val pagingSort: Pageable = PageRequest.of(page, limit, Sort.by(orders as List<Sort.Order>)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            val platforms: Page<Platform> = if (project == null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                platformRepository.findAll(pagingSort) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            val column = listOf("id", "name", "project_id") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            val sort2 = if (!sort.contains(",")) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sort + ",desc" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                platformRepository.findByProject(project, pagingSort) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sort 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            val ret: List<Platform?> = platforms.content 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            val response: MutableMap<String, Any> = HashMap() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            response["currentPage"] = platforms.number 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            response["totalRecord"] = platforms.totalElements 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            response["totalPage"] = platforms.totalPages 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            response["results"] = ret 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (ret.isNotEmpty()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                ResponseEntity(response, HttpStatus.OK) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (!column.contains(sort2[0])) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                ResponseEntity<Platform>(HttpStatus.BAD_REQUEST) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                arrayOf<String>() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                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 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 ret: List<Platform?> = platforms.content 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                val response: MutableMap<String, Any> = HashMap() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                response["currentPage"] = platforms.number 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                response["totalRecord"] = platforms.totalElements 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                response["totalPage"] = platforms.totalPages 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                response["results"] = ret 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (ret.isNotEmpty()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    ResponseEntity(response, HttpStatus.OK) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    arrayOf<String>() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } catch (e: Exception) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            ResponseEntity(null, HttpStatus.INTERNAL_SERVER_ERROR) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            e.printStackTrace() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            ResponseEntity(e, HttpStatus.INTERNAL_SERVER_ERROR) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -102,7 +121,7 @@ class PlatformController( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         val newPlatform = Platform() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         newPlatform.name = platform.name!!.trim() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        newPlatform.project_id = foundProject 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        newPlatform.project = foundProject 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         ResponseEntity<Platform>(platformRepository.save(newPlatform), HttpStatus.CREATED) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 } else {//invalid name 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -133,9 +152,9 @@ class PlatformController( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         ResponseEntity<Platform>(HttpStatus.NOT_FOUND) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         val check = platformRepository.findByName(input.name, targetProject.id.toString()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        if (check.isEmpty() || (input.name == platformExist.get().name && targetProject.id.toString() == platformExist.get().project_id!!.id.toString())) {//tidak ada yg sama 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        if (check.isEmpty() || (input.name == platformExist.get().name && targetProject.id.toString() == platformExist.get().project!!.id.toString())) {//tidak ada yg sama 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                             val savePlatform = platformExist.get() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                            savePlatform.project_id = targetProject 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            savePlatform.project = targetProject 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                             savePlatform.name = input.name!!.trim() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                             ResponseEntity<Platform>(platformRepository.save(savePlatform), HttpStatus.OK) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         } else { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -161,9 +180,9 @@ class PlatformController( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 val find = platformRepository.findById(id) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 val userId = userRepository.getUserByUsername(SecurityContextHolder.getContext().authentication.name) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 val validOwner = 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    projectRepository.validOwner(find.get().project_id!!.id.toString(), userId.id.toString()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    projectRepository.validOwner(find.get().project!!.id.toString(), userId.id.toString()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 val validAdmin = 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    memberRepository.validRole(find.get().project_id!!.id.toString(), userId.id.toString(), "2") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    memberRepository.validRole(find.get().project!!.id.toString(), userId.id.toString(), "2") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 if (validOwner > 0 || validAdmin > 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     if (find.isPresent) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         platformRepository.deleteById(id) 
			 |