2017-09-25 10:06:58 +08:00
|
|
|
package route
|
|
|
|
|
|
|
|
|
|
import model.JSONResponse
|
|
|
|
|
import model.ModelMenu
|
|
|
|
|
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("/menu")
|
|
|
|
|
class CtrlMenu : Controller() {
|
|
|
|
|
private val service = Service()
|
|
|
|
|
|
|
|
|
|
@GET("/{id: [0-9]+}")
|
|
|
|
|
@Produces(Produces.JSON)
|
2017-09-25 11:56:47 +08:00
|
|
|
fun getMenuById(@Param("id") id: Int): JSONResponse {
|
2017-09-25 10:06:58 +08:00
|
|
|
val menu = service.getMenuById(id)
|
|
|
|
|
if (menu == null) {
|
|
|
|
|
return responseNotFoundById()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return responseSuccess(menu)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GET("/byRole/{roleId: [0-9]+}")
|
|
|
|
|
@Produces(Produces.JSON)
|
2017-09-25 11:56:47 +08:00
|
|
|
fun getMenuByRole(@Param("roleId") roleId: Int): JSONResponse {
|
2017-09-25 10:06:58 +08:00
|
|
|
val role = service.getRoleById(roleId)
|
|
|
|
|
if (role == null) {
|
|
|
|
|
return responseNotFoundById()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val menus = service.getMenuByRole(roleId)
|
|
|
|
|
return responseSuccess(menus)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@POST("/")
|
|
|
|
|
@Produces(Produces.JSON)
|
|
|
|
|
fun addMenu(): JSONResponse {
|
2017-09-25 11:56:47 +08:00
|
|
|
val menu: ModelMenu? = request.createEntityFromParameters(ModelMenu::class.java)
|
2017-09-25 10:06:58 +08:00
|
|
|
if (menu == null) {
|
|
|
|
|
return responseInvalidParams()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val isSuccess = service.saveMenu(menu)
|
|
|
|
|
return if (isSuccess) {
|
|
|
|
|
responseSuccess(menu)
|
|
|
|
|
} else {
|
|
|
|
|
responseOperationFailed()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@POST("/{id: [0-9]+}")
|
|
|
|
|
@Produces(Produces.JSON)
|
2017-09-25 11:56:47 +08:00
|
|
|
fun updateMenu(@Param("id") id: Int): JSONResponse {
|
2017-09-25 10:06:58 +08:00
|
|
|
val menu: ModelMenu? = request.createEntityFromBody(ModelMenu::class.java)
|
|
|
|
|
if (menu == null) {
|
|
|
|
|
return responseInvalidParams()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val isSuccess = service.updateMenu(menu)
|
|
|
|
|
return if (isSuccess) {
|
|
|
|
|
responseSuccess(menu)
|
|
|
|
|
} else {
|
|
|
|
|
responseOperationFailed()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@POST("/del/{id: [0-9]+}")
|
|
|
|
|
@Produces(Produces.JSON)
|
2017-09-25 11:56:47 +08:00
|
|
|
fun deleteMenu(@Param("id") id: Int): JSONResponse {
|
2017-09-25 10:06:58 +08:00
|
|
|
val menu = service.getMenuById(id)
|
|
|
|
|
if (menu == null) {
|
|
|
|
|
return responseNotFoundById()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val isSuccess = service.deleteMenu(id)
|
|
|
|
|
return if (isSuccess) {
|
|
|
|
|
responseSuccess(id)
|
|
|
|
|
} else {
|
|
|
|
|
responseOperationFailed()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@POST("/bindPerm/{menuId: [0-9]+}/{permId: [0-9]+}")
|
|
|
|
|
@Produces(Produces.JSON)
|
2017-09-25 11:56:47 +08:00
|
|
|
fun bindMenuToPerm(@Param("menuId") menuId: Int, @Param("permId") permId: Int): JSONResponse {
|
2017-09-25 10:06:58 +08:00
|
|
|
val menu = service.getMenuById(menuId)
|
|
|
|
|
val perm = service.getPermById(permId)
|
|
|
|
|
|
|
|
|
|
if (menu == null || perm == null) {
|
|
|
|
|
return responseNotFoundById()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val isSuccess = service.bindMenuToPerm(menuId, permId)
|
|
|
|
|
return if (isSuccess) {
|
|
|
|
|
responseSuccess(true)
|
|
|
|
|
} else {
|
|
|
|
|
responseOperationFailed()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@POST("/unbindPerm/{menuId: [0-9]+}/{permId: [0-9]+}")
|
|
|
|
|
@Produces(Produces.JSON)
|
2017-09-25 11:56:47 +08:00
|
|
|
fun unbindMenuToPerm(@Param("menuId") menuId: Int, @Param("permId") permId: Int): JSONResponse {
|
2017-09-25 10:06:58 +08:00
|
|
|
val menu = service.getMenuById(menuId)
|
|
|
|
|
val perm = service.getPermById(permId)
|
|
|
|
|
|
|
|
|
|
if (menu == null || perm == null) {
|
|
|
|
|
return responseNotFoundById()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val isSuccess = service.unbindMenuToPerm(menuId, permId)
|
|
|
|
|
return if (isSuccess) {
|
|
|
|
|
responseSuccess(true)
|
|
|
|
|
} else {
|
|
|
|
|
responseOperationFailed()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|