All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kasinf.framework.rest.web.controller.BaseCrudController Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package com.kasinf.framework.rest.web.controller;

import com.kasinf.framework.rest.eneity.AbstractEntity;
import com.kasinf.framework.core.response.BaseResponse;
import com.kasinf.framework.rest.web.util.ControllerUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

/**
 * @param   要操作的实体
 * @param  要操作实体的主键
 * @author lkhsh
 * 基础增删改查控制器
 */
public abstract class BaseCrudController extends BaseSearchController {

    @PostMapping(consumes = {MediaType.APPLICATION_JSON_VALUE})
    public BaseResponse saveWithJson(@RequestBody T t) {
        t = getBaseService().create(t);
        return config.getReturnBodyOnCreate() ? success(t) : success();
    }

    @PostMapping(consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public BaseResponse saveWithForm(T t) {
        t = getBaseService().create(t);
        return config.getReturnBodyOnCreate() ? success(t) : success();
    }

    @PutMapping(consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE})
    public BaseResponse updateWithJson(@RequestBody T t) {
        t = getBaseService().update(t);
        return config.getReturnBodyOnUpdate() ? success(t) : success();
    }

    @PutMapping(consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public BaseResponse updateWithForm(T t) {
        t = getBaseService().update(t);
        return config.getReturnBodyOnUpdate() ? success(t) : success();
    }

    @PatchMapping(value = "{id}", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE})
    public BaseResponse patchWithJson(@RequestBody T t, @PathVariable("id") T old) {
        ControllerUtils.setPatchBean(t, old);
        t = getBaseService().update(old);
        return config.getReturnBodyOnUpdate() ? success(t) : success();
    }

    @PatchMapping(value = "{id}", consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public BaseResponse patchWithForm(T t, @PathVariable("id") T old) {
        ControllerUtils.setPatchBean(t, old);
        t = getBaseService().update(old);
        return config.getReturnBodyOnUpdate() ? success(old) : success();
    }

    @DeleteMapping(value = "b/{ids}")
    public BaseResponse batchDelete(@PathVariable ID[] ids){
        getBaseService().delete(ids);
        return success();
    }

    @DeleteMapping(value = "{id}")
    public BaseResponse batchDelete(@PathVariable("id") T t){
        getBaseService().delete(t);
        return success();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy