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

org.ssssssss.magicapi.controller.MagicDataSourceController Maven / Gradle / Ivy

package org.ssssssss.magicapi.controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.ssssssss.magicapi.config.MagicConfiguration;
import org.ssssssss.magicapi.config.Valid;
import org.ssssssss.magicapi.interceptor.Authorization;
import org.ssssssss.magicapi.model.DataSourceInfo;
import org.ssssssss.magicapi.model.JsonBean;
import org.ssssssss.magicapi.provider.MagicAPIService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

public class MagicDataSourceController extends MagicController implements MagicExceptionHandler {

	public MagicDataSourceController(MagicConfiguration configuration) {
		super(configuration);
	}

	/**
	 * 查询数据源列表
	 */
	@RequestMapping("/datasource/list")
	@ResponseBody
	public JsonBean> list(HttpServletRequest request) {
		return new JsonBean<>(magicAPIService.datasourceList()
				.stream()
				.filter(it -> allowVisit(request, Authorization.VIEW, it))
				.collect(Collectors.toList())
		);
	}

	@RequestMapping("/datasource/test")
	@ResponseBody
	public JsonBean test(@RequestBody DataSourceInfo properties) {
		return new JsonBean<>(magicAPIService.testDataSource(properties));
	}

	/**
	 * 保存数据源
	 *
	 * @param properties 数据源配置信息
	 */
	@RequestMapping("/datasource/save")
	@Valid(readonly = false)
	@ResponseBody
	public JsonBean save(HttpServletRequest request, @RequestBody DataSourceInfo properties) {
		isTrue(allowVisit(request, Authorization.SAVE, properties), PERMISSION_INVALID);
		return new JsonBean<>(magicAPIService.saveDataSource(properties));
	}

	/**
	 * 删除数据源
	 *
	 * @param id 数据源ID
	 */
	@RequestMapping("/datasource/delete")
	@Valid(readonly = false)
	@ResponseBody
	public JsonBean delete(HttpServletRequest request, String id) {
		DataSourceInfo dataSource = getDataSourceInfo(id);
		isTrue(allowVisit(request, Authorization.DELETE, dataSource), PERMISSION_INVALID);
		return new JsonBean<>(magicAPIService.deleteDataSource(id));
	}

	@RequestMapping("/datasource/detail")
	@ResponseBody
	public JsonBean detail(HttpServletRequest request, String id) {
		DataSourceInfo dataSource = getDataSourceInfo(id);
		isTrue(allowVisit(request, Authorization.VIEW, dataSource), PERMISSION_INVALID);
		return new JsonBean<>(dataSource);
	}

	private DataSourceInfo getDataSourceInfo(String id){
		DataSourceInfo dataSource = magicAPIService.getDataSource(id);
		notNull(dataSource, DATASOURCE_NOT_FOUND);
		return dataSource;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy