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

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

There is a newer version: 2.1.1
Show newest version
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.ApiInfo;
import org.ssssssss.magicapi.model.Backup;
import org.ssssssss.magicapi.model.JsonBean;
import org.ssssssss.magicapi.provider.ApiServiceProvider;
import org.ssssssss.magicapi.provider.MagicAPIService;
import org.ssssssss.magicapi.provider.MagicBackupService;

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

/**
 * 接口相关操作
 */
public class MagicAPIController extends MagicController implements MagicExceptionHandler {

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

	/**
	 * 删除接口
	 *
	 * @param id 接口ID
	 */
	@RequestMapping("/delete")
	@ResponseBody
	@Valid(readonly = false)
	public JsonBean delete(HttpServletRequest request, String id) {
		isTrue(allowVisit(request, Authorization.DELETE, getApiInfo(id)), PERMISSION_INVALID);
		return new JsonBean<>(magicAPIService.deleteApi(id));
	}

	/**
	 * 查询所有接口
	 */
	@RequestMapping("/list")
	@ResponseBody
	public JsonBean> list(HttpServletRequest request) {
		return new JsonBean<>(magicAPIService.apiList()
				.stream()
				.filter(it -> allowVisit(request, Authorization.VIEW, it))
				.map(ApiInfo::simple)
				.collect(Collectors.toList())
		);
	}

	/**
	 * 查询接口详情
	 *
	 * @param id 接口ID
	 */
	@RequestMapping("/get")
	@ResponseBody
	public JsonBean get(HttpServletRequest request, String id) {
		isTrue(allowVisit(request, Authorization.VIEW, getApiInfo(id)), PERMISSION_INVALID);
		return new JsonBean<>(magicAPIService.getApiInfo(id));
	}

	/**
	 * 查询历史记录
	 *
	 * @param id 接口ID
	 */
	@RequestMapping("/backups")
	@ResponseBody
	public JsonBean> backupList(HttpServletRequest request, String id) {
		isTrue(allowVisit(request, Authorization.VIEW, getApiInfo(id)), PERMISSION_INVALID);
		return new JsonBean<>(magicBackupService.backupById(id));
	}

	/**
	 * 获取历史记录
	 *
	 * @param id        接口ID
	 * @param timestamp 时间点
	 */
	@RequestMapping("/backup/get")
	@ResponseBody
	public JsonBean backups(HttpServletRequest request, String id, Long timestamp) {
		isTrue(allowVisit(request, Authorization.VIEW, getApiInfo(id)), PERMISSION_INVALID);
		return new JsonBean<>(magicBackupService.backupInfo(id, timestamp));
	}

	/**
	 * 移动接口
	 */
	@RequestMapping("/api/move")
	@ResponseBody
	@Valid(readonly = false)
	public JsonBean apiMove(HttpServletRequest request, String id, String groupId) {
		ApiInfo apiInfo = getApiInfo(id).copy();
		// 新的分组ID
		apiInfo.setGroupId(groupId);
		isTrue(allowVisit(request, Authorization.SAVE, apiInfo), PERMISSION_INVALID);
		return new JsonBean<>(magicAPIService.moveApi(id, groupId));
	}

	/**
	 * 保存接口
	 */
	@RequestMapping("/save")
	@ResponseBody
	@Valid(readonly = false)
	public JsonBean save(HttpServletRequest request, @RequestBody ApiInfo info) {
		isTrue(allowVisit(request, Authorization.SAVE, info), PERMISSION_INVALID);
		return new JsonBean<>(magicAPIService.saveApi(info));
	}

	private ApiInfo getApiInfo(String id){
		ApiInfo apiInfo = magicAPIService.getApiInfo(id);
		notNull(apiInfo, API_NOT_FOUND);
		return apiInfo;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy