package gaf3.core.services.role

import gaf3.core.data.ErrorResult
import gaf3.core.data.PagedData
import gaf3.core.services.role.domain.RoleForm
import gaf3.core.services.role.entity.GafRoleItem
import gaf3.core.services.role.service.RoleService
import org.springframework.web.bind.annotation.*
import javax.validation.Valid

@RestController
class RoleServiceController(val service: RoleService) {

    @GetMapping(path = ["/gaf/role/items"])
    fun list(filter: GafRoleItem): PagedData<GafRoleItem>{
        return service.find(filter)
    }

    @PostMapping(path = ["/gaf/role/items"])
    fun create(@Valid @RequestBody data: RoleForm): GafRoleItem {
        return service.create(data.type?:"0", data)
    }

    @DeleteMapping(path = ["/gaf/role/items/{id}"])
    fun delete(@PathVariable id: String): ErrorResult {
        service.delete(id)
        return ErrorResult(0,"deleted")
    }

    @PostMapping(path = ["/gaf/role/items/{id}"])
    fun update(@PathVariable id: String, @RequestBody data: RoleForm): GafRoleItem {
        return service.update(id, data)
    }

    @GetMapping(path = ["/gaf/role/items/{id}"])
    fun fetch(@PathVariable id: String): GafRoleItem? {
        return service.fetch(id)
    }
}