com.kasinf.framework.rest.web.controller.BaseCrudController Maven / Gradle / Ivy
The newest version!
package com.kasinf.framework.rest.web.controller;
import com.kasinf.framework.core.response.R;
import com.kasinf.framework.rest.config.SearchableConfiguration;
import com.kasinf.framework.rest.eneity.AbstractEntity;
import com.kasinf.framework.rest.web.util.ControllerUtils;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.io.Serializable;
/**
* @param 要操作的实体
* @param 要操作实体的主键
* @author lkhsh
* 基础增删改查控制器
*/
@SuppressWarnings("all")
public abstract class BaseCrudController extends BaseSearchController {
@PostMapping(consumes = {MediaType.APPLICATION_JSON_VALUE})
public R saveWithJson(@RequestBody T t) {
t = getBaseService().create(t);
return config.getReturnBodyOnCreate() ? success(t) : success(t.getId());
}
@PostMapping(consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public R saveWithForm(T t) {
t = getBaseService().create(t);
return config.getReturnBodyOnCreate() ? success(t) : success(t.getId());
}
@PutMapping(consumes = {MediaType.APPLICATION_JSON_VALUE})
public R updateWithJson(@RequestBody T t) {
t = getBaseService().update(t);
return config.getReturnBodyOnUpdate() ? success(t) : success();
}
@PutMapping(consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public R updateWithForm(T t) {
t = getBaseService().update(t);
return config.getReturnBodyOnUpdate() ? success(t) : success();
}
@PatchMapping(value = "{id}", consumes = {MediaType.APPLICATION_JSON_VALUE})
public R 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 R 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 R batchDelete(@PathVariable ID[] ids){
getBaseService().delete(ids);
return success();
}
@DeleteMapping(value = "{id}")
public R batchDelete(@PathVariable("id") T t){
getBaseService().delete(t);
return success();
}
}