|
@@ -5,6 +5,9 @@ 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.PageRequest
|
|
|
+import org.springframework.data.domain.Pageable
|
|
|
+import org.springframework.data.domain.Sort
|
|
|
import org.springframework.http.HttpStatus
|
|
|
import org.springframework.http.ResponseEntity
|
|
|
import org.springframework.security.core.context.SecurityContextHolder
|
|
@@ -19,29 +22,68 @@ class ProjectController(
|
|
|
private val userRepository: UserRepository,
|
|
|
private val memberRepository: MemberRepository
|
|
|
) {
|
|
|
+ private fun getSortDirection(direction: String): Sort.Direction {
|
|
|
+ if (direction == "asc") {
|
|
|
+ return Sort.Direction.ASC
|
|
|
+ } else if (direction == "desc") {
|
|
|
+ return Sort.Direction.DESC
|
|
|
+ }
|
|
|
+ return Sort.Direction.ASC
|
|
|
+ }
|
|
|
|
|
|
@GetMapping("/projects")
|
|
|
- fun getProject(@RequestParam(required = false) name: String?): Any {
|
|
|
+ fun getProject(
|
|
|
+ @RequestParam(required = false) name: String?,
|
|
|
+ @RequestParam(defaultValue = 0.toString()) page: Int,
|
|
|
+ @RequestParam(defaultValue = 3.toString()) limit: Int,
|
|
|
+ @RequestParam(defaultValue = "id, desc") sort: Array<String>
|
|
|
+ ): Any {
|
|
|
return try {
|
|
|
- val projects: List<Project> =
|
|
|
- if (name == null) projectRepository.findAll() else projectRepository.findByNameContaining(
|
|
|
- name
|
|
|
+ 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(getSortDirection(sort[1]), sort[0]))
|
|
|
+ orders.add(Sort.Order(Sort.Direction.DESC, sort[0]))
|
|
|
+ }
|
|
|
+ val pagingSort: Pageable = PageRequest.of(page, limit, Sort.by(orders as List<Sort.Order>))
|
|
|
+ val projects =
|
|
|
+ if (name == null) projectRepository.findAll(pagingSort) else projectRepository.findByNameContaining(
|
|
|
+ name, pagingSort
|
|
|
)
|
|
|
- if (projects.isEmpty()) {
|
|
|
+ val ret = projects.content
|
|
|
+ val response: MutableMap<String, Any> = HashMap()
|
|
|
+ response["currentPage"] = projects.number
|
|
|
+ response["totalRecord"] = projects.totalElements
|
|
|
+ response["totalPage"] = projects.totalPages
|
|
|
+ response["results"] = ret
|
|
|
+ if (ret.isEmpty()) {
|
|
|
arrayOf<String>()
|
|
|
- } else ResponseEntity<List<Project>?>(projects, HttpStatus.OK)
|
|
|
+ } else {
|
|
|
+ ResponseEntity(response, HttpStatus.OK)
|
|
|
+ }
|
|
|
} catch (e: Exception) {
|
|
|
- ResponseEntity<List<Project>?>(null, HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
+ ResponseEntity(null, HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@GetMapping("/projects/{id}")
|
|
|
fun getProjectById(@PathVariable("id") id: Long): ResponseEntity<Project> {
|
|
|
+ val userId = userRepository.getUserByUsername(SecurityContextHolder.getContext().authentication.name)
|
|
|
+ val validOwner = projectRepository.validOwner(id.toString(), userId.id.toString())
|
|
|
+ val validMember = memberRepository.validMember(id.toString(), userId.id.toString())
|
|
|
val projectData: Optional<Project?> = projectRepository.findById(id)
|
|
|
- return if (projectData.isPresent) {
|
|
|
- ResponseEntity<Project>(projectData.get(), HttpStatus.OK)
|
|
|
+ return if (validOwner > 0 || validMember > 0) {
|
|
|
+ if (projectData.isPresent) {
|
|
|
+ ResponseEntity<Project>(projectData.get(), HttpStatus.OK)
|
|
|
+ } else {
|
|
|
+ ResponseEntity<Project>(HttpStatus.NOT_FOUND)
|
|
|
+ }
|
|
|
} else {
|
|
|
- ResponseEntity<Project>(HttpStatus.NOT_FOUND)
|
|
|
+ ResponseEntity<Project>(HttpStatus.FORBIDDEN)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -106,7 +148,7 @@ class ProjectController(
|
|
|
@DeleteMapping("/projects/{id}")
|
|
|
fun deleteProject(@PathVariable("id") id: Long): ResponseEntity<HttpStatus> {
|
|
|
val find = projectRepository.findById(id)
|
|
|
- val used = platformRepository.findByProject(id.toString()).size
|
|
|
+ val used = platformRepository.findByProjectId(id.toString()).size
|
|
|
val userId = userRepository.getUserByUsername(SecurityContextHolder.getContext().authentication.name)
|
|
|
val validOwner = projectRepository.validOwner(id.toString(), userId.id.toString())
|
|
|
return try {
|