package gaf3.core.services.bind

import gaf3.core.data.ErrorResult
import gaf3.core.data.PagedData
import gaf3.core.services.bind.domain.BatchBindForm
import gaf3.core.services.bind.domain.BatchBindResult
import gaf3.core.services.bind.domain.BindForm
import gaf3.core.services.bind.entity.GafBindItem
import gaf3.core.services.bind.service.BindService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import javax.validation.Valid

@RestController("gafBindServiceController")
class BindServiceController(@Autowired val service: BindService) {
//    companion object {
//        val log: Logger = LoggerFactory.getLogger(UserServiceController::class.java)
//    }

    @GetMapping(path = ["/gaf/bind/{type}/items"])
    fun find(@PathVariable type: String, source: String?, target: String?): PagedData<GafBindItem>{
        return service.find(type, source, target)
    }

    @PostMapping(path = ["/gaf/bind/{type}/items"])
    fun create(@PathVariable type: String, @Valid @RequestBody data: BindForm): GafBindItem {
        return service.create(type, data)
    }

    @PostMapping(path = ["/gaf/bind/{type}/items/batch"])
    fun batchCreate(@PathVariable type: String, replace: Boolean?, @Valid @RequestBody data: BatchBindForm): BatchBindResult {
        return service.batchCreate(type, replace ?: false, data)
    }

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

    @DeleteMapping(path = ["/gaf/bind/{type}/items"])
    fun delete(@PathVariable type: String, source: String?, target: String?): ErrorResult{
        service.delete(type, source, target)
        return ErrorResult(0,"deleted")
    }
}