package dev.fyloz.trial.colorrecipesexplorer.exception.model

import dev.fyloz.trial.colorrecipesexplorer.exception.RestException
import dev.fyloz.trial.colorrecipesexplorer.model.Model
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.ResponseStatus

class EntityAlreadyExistsException(modelType: Class<out Model>, val identifierType: IdentifierType, val identifierName: String?, val requestedId: Any) : ModelException(modelType) {
    constructor(modelType: Class<out Model>, identifierType: IdentifierType, requestedId: Any) : this(modelType, identifierType, identifierType.name, requestedId)
    constructor(exception: EntityAlreadyExistsException) : this(exception.type, exception.identifierType, exception.identifierName, exception.requestedId)
}

@ResponseStatus(HttpStatus.CONFLICT)
class EntityAlreadyExistsRestException(val value: Any) : RestException(HttpStatus.CONFLICT) {
    override val exceptionMessage: String = "An entity with the given identifier already exists"

    override fun buildBody(): RestExceptionBody = object : RestExceptionBody() {
        val id = value
    }
}
