package gaf3.core.services.dept

import gaf3.core.data.ErrorResult
import gaf3.core.data.PagedData
import gaf3.core.services.dept.domain.DeptForm
import gaf3.core.services.dept.entity.GafDeptItem
import gaf3.core.services.dept.service.DeptService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import javax.validation.Valid

@RestController
class DeptServiceController(@Autowired val service: DeptService) {

    @GetMapping(path = ["/gaf/dept/items"])
    fun findItems(pid: String?, recursive: Boolean?): PagedData<GafDeptItem> {
        val rs = service.find(pid, recursive ?: false)
        return PagedData(rs, rs.size)
    }

    @PostMapping(path = ["/gaf/dept/items"])
    fun create(@RequestParam pid: String?, @Valid @RequestBody data: DeptForm): GafDeptItem {
        return service.create(pid ?: "", data)
    }

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

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

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