|
@@ -1,6 +1,8 @@
|
|
|
package com.swagger.rest.controllers
|
|
|
|
|
|
+import com.swagger.rest.models.Enum
|
|
|
import com.swagger.rest.models.MemberInput
|
|
|
+import com.swagger.rest.models.MemberOutput
|
|
|
import com.swagger.rest.models.ProjectMember
|
|
|
import com.swagger.rest.repositories.MemberRepository
|
|
|
import com.swagger.rest.repositories.ProjectRepository
|
|
@@ -21,8 +23,16 @@ class MemberController(
|
|
|
@GetMapping("/projects/{id}/member")
|
|
|
fun getMemberByProject(@PathVariable("id") id: Long): Any {
|
|
|
val memberData = memberRepository.findByProject(id.toString())
|
|
|
+ val output = memberData.map {
|
|
|
+ MemberOutput(
|
|
|
+ id = it.id,
|
|
|
+ project = it.project!!.description,
|
|
|
+ user = it.user!!.name,
|
|
|
+ role = Enum.Member.values()[it.role]
|
|
|
+ )
|
|
|
+ }
|
|
|
return if (memberData.isNotEmpty()) {
|
|
|
- ResponseEntity<List<ProjectMember>>(memberData, HttpStatus.OK)
|
|
|
+ ResponseEntity(output, HttpStatus.OK)
|
|
|
} else {
|
|
|
arrayOf<String>()
|
|
|
}
|
|
@@ -31,8 +41,16 @@ class MemberController(
|
|
|
@GetMapping("/projects/member/{id}")
|
|
|
fun getMemberProject(@PathVariable("id") id: Long): Any {
|
|
|
val memberData = memberRepository.findById(id)
|
|
|
+ val output = memberData.map {
|
|
|
+ MemberOutput(
|
|
|
+ id = it.id,
|
|
|
+ project = it.project!!.description,
|
|
|
+ user = it.user!!.name,
|
|
|
+ role = Enum.Member.values()[it.role]
|
|
|
+ )
|
|
|
+ }
|
|
|
return if (memberData.isPresent) {
|
|
|
- ResponseEntity<ProjectMember>(memberData.get(), HttpStatus.OK)
|
|
|
+ ResponseEntity<MemberOutput>(output.get(), HttpStatus.OK)
|
|
|
} else {
|
|
|
arrayOf<String>()
|
|
|
}
|
|
@@ -44,14 +62,11 @@ class MemberController(
|
|
|
return try {
|
|
|
val find = memberRepository.findById(id)
|
|
|
if (find.isPresent) {
|
|
|
- val validOwner =
|
|
|
- projectRepository.validOwner(find.get().project_id!!.id.toString(), userId.id.toString())
|
|
|
+ val validOwner = 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")
|
|
|
val targetNotAdmin = memberRepository.targetRole(
|
|
|
- find.get().project_id!!.id.toString(),
|
|
|
- find.get().user_id!!.id.toString(),
|
|
|
- find.get().role.toString()
|
|
|
+ find.get().project!!.id.toString(), find.get().user!!.id.toString(), find.get().role.toString()
|
|
|
)
|
|
|
if (validOwner > 0 || (validAdmin > 0 && targetNotAdmin > 0)) {
|
|
|
memberRepository.deleteById(id)
|
|
@@ -79,18 +94,29 @@ class MemberController(
|
|
|
val validAdmin = memberRepository.validRole(id.toString(), userId.id.toString(), "2")
|
|
|
if (found.isPresent && user.isPresent) {
|
|
|
if (validOwner > 0 || validAdmin > 0) {
|
|
|
- if (enumValues<Member>().any { it.name == member.role!!.uppercase() }) {
|
|
|
- val role = Member.valueOf(member.role!!.uppercase()).ordinal
|
|
|
+ if (enumValues<Enum.Member>().any { it.name == member.role!!.uppercase() }) {
|
|
|
+ val role = Enum.Member.valueOf(member.role!!.uppercase()).ordinal
|
|
|
val dup =
|
|
|
memberRepository.findDup(id.toString(), role.toString(), member.user_id.toString()).size
|
|
|
if (dup > 0) {//duplicate
|
|
|
ResponseEntity<ProjectMember>(HttpStatus.CONFLICT)
|
|
|
} else {
|
|
|
val saveMember = ProjectMember()
|
|
|
- saveMember.project_id = found.get()
|
|
|
- saveMember.user_id = user.get()
|
|
|
+ saveMember.project = found.get()
|
|
|
+ saveMember.user = user.get()
|
|
|
saveMember.role = role
|
|
|
- ResponseEntity<ProjectMember>(memberRepository.save(saveMember), HttpStatus.CREATED)
|
|
|
+ val save = memberRepository.save(saveMember)
|
|
|
+ if (listOf(save).isNotEmpty()) {
|
|
|
+ val output = MemberOutput(
|
|
|
+ id = save.id,
|
|
|
+ project = save.project!!.description,
|
|
|
+ user = save.user!!.name,
|
|
|
+ role = Enum.Member.values()[save.role]
|
|
|
+ )
|
|
|
+ ResponseEntity(output, HttpStatus.CREATED)
|
|
|
+ } else {
|
|
|
+ ResponseEntity(null, HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
+ }
|
|
|
}
|
|
|
} else {// invalid
|
|
|
ResponseEntity<ProjectMember>(HttpStatus.BAD_REQUEST)
|
|
@@ -105,8 +131,4 @@ class MemberController(
|
|
|
ResponseEntity<ProjectMember>(null, HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
-private enum class Member {
|
|
|
- QC, PROGRAMMER, ADMIN
|
|
|
}
|