2017-09-25 10:06:58 +08:00
|
|
|
package route
|
|
|
|
|
|
|
|
|
|
import model.JSONResponse
|
|
|
|
|
import model.ModelRole
|
|
|
|
|
import ro.pippo.controller.*
|
|
|
|
|
import ro.pippo.controller.extractor.Param
|
|
|
|
|
import service.Service
|
|
|
|
|
import util.responseInvalidParams
|
|
|
|
|
import util.responseNotFoundById
|
|
|
|
|
import util.responseOperationFailed
|
|
|
|
|
import util.responseSuccess
|
|
|
|
|
|
|
|
|
|
@Path("/role")
|
|
|
|
|
class CtrlRole : Controller() {
|
|
|
|
|
val service = Service()
|
|
|
|
|
|
|
|
|
|
@GET("/{id: [0-9]+}")
|
|
|
|
|
@Produces(Produces.JSON)
|
2017-09-25 11:56:47 +08:00
|
|
|
fun getRoleById(@Param("id") id: Int): JSONResponse {
|
2017-09-25 10:06:58 +08:00
|
|
|
val role = service.getRoleById(id)
|
|
|
|
|
if (role == null) {
|
|
|
|
|
return responseNotFoundById()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return responseSuccess(role)
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-25 11:56:47 +08:00
|
|
|
@GET("/byUser/{userId: [0-9]+}")
|
2017-09-25 10:06:58 +08:00
|
|
|
@Produces(Produces.JSON)
|
2017-09-25 11:56:47 +08:00
|
|
|
fun getRoleByUserId(@Param("userId") userId: Int): JSONResponse {
|
2017-09-25 10:06:58 +08:00
|
|
|
val user = service.getUserById(userId)
|
|
|
|
|
if (user == null) {
|
|
|
|
|
return responseNotFoundById()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val role = service.getRoleByUserId(userId)
|
|
|
|
|
if (role == null) {
|
|
|
|
|
return responseNotFoundById()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return responseSuccess(role)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@POST("/")
|
|
|
|
|
@Produces(Produces.JSON)
|
|
|
|
|
fun addRole(): JSONResponse {
|
2017-09-25 11:56:47 +08:00
|
|
|
val role: ModelRole? = request.createEntityFromParameters(ModelRole::class.java)
|
2017-09-25 10:06:58 +08:00
|
|
|
if (role == null) {
|
|
|
|
|
return responseInvalidParams()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val isSuccess = service.saveRole(role)
|
|
|
|
|
return if (isSuccess) {
|
|
|
|
|
responseSuccess(role)
|
|
|
|
|
} else {
|
|
|
|
|
responseOperationFailed()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@POST("/del/{id: [0-9]+}")
|
|
|
|
|
@Produces(Produces.JSON)
|
2017-09-25 11:56:47 +08:00
|
|
|
fun delRole(@Param("roleId") roleId: Int): JSONResponse {
|
2017-09-25 10:06:58 +08:00
|
|
|
val role = service.getRoleById(roleId)
|
|
|
|
|
if (role == null) {
|
|
|
|
|
responseNotFoundById()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val isSuccess = service.deleteRole(roleId)
|
|
|
|
|
return if (isSuccess) {
|
|
|
|
|
responseSuccess(roleId)
|
|
|
|
|
} else {
|
|
|
|
|
responseOperationFailed()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@POST("/{id: [0-9]+}")
|
|
|
|
|
@Produces(Produces.JSON)
|
|
|
|
|
fun updateRole():JSONResponse {
|
|
|
|
|
val role: ModelRole? = request.createEntityFromBody(ModelRole::class.java)
|
|
|
|
|
if (role == null) {
|
|
|
|
|
return responseNotFoundById()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val isSuccess = service.updateRole(role)
|
|
|
|
|
return if (isSuccess) {
|
|
|
|
|
responseSuccess(role)
|
|
|
|
|
} else {
|
|
|
|
|
responseOperationFailed()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|