package com.eactive.httpmockserver; import java.util.Optional; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class ApiManageController { @Autowired private ApiService apiService; @Autowired private ApiServiceKeyService apiKeyService; @GetMapping("/manage/findAllApis") public String getAllApis(Pageable pageable, Model model) { //model.addAttribute("apiInfoList", apiService.findAllApis(pageable)); return "page/list-api"; } @SuppressWarnings("deprecation") @GetMapping(path = { "/manage/getApiForm", "/manage/getApiForm/{id}" }) public String getApiForm(Model model, @PathVariable Optional id) { if (id.isPresent()) { Optional optional = apiService.findById(id.get()); if (optional.isPresent()) { model.addAttribute("apiInfo", optional.get()); } else { throw new IllegalArgumentException("No data, id: " + id.get()); } } else { ApiInfo apiInfo = new ApiInfo(); apiInfo.setMethod(HttpMethod.POST.name()); apiInfo.setResponseContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); model.addAttribute("apiInfo", apiInfo); } return "page/add-edit-api"; } @PostMapping("/manage/saveApi") public String saveApi(@Valid ApiInfo apiInfo, BindingResult result, Model model) { if (result.hasErrors()) { return "page/add-edit-api"; } if (apiInfo.getId() == null) { apiService.save(apiInfo); } else { apiService.update(apiInfo); } return "redirect:/manage/findAllApis"; } @RequestMapping("/manage/deleteApi/{id}") public String deleteApi(@PathVariable Long id) { if (id == null) { throw new IllegalArgumentException("No data, id: " + id); } apiService.delete(id); return "redirect:/manage/findAllApis"; } @GetMapping("/manage/findAllApiKeys") public String getAllApiServiceKeys(Pageable pageable, Model model) { model.addAttribute("apiServiceKeyList", apiKeyService.findAllApiServiceKeys(pageable)); return "page/list-api-key"; } @GetMapping(path = { "/manage/getApiKeyForm", "/manage/getApiKeyForm/{id}" }) public String getApiKeyForm(Model model, @PathVariable Optional id) { if (id.isPresent()) { Optional optional = apiKeyService.findById(id.get()); if (optional.isPresent()) { model.addAttribute("apiServiceKey", optional.get()); } else { throw new IllegalArgumentException("No data, id: " + id.get()); } } else { model.addAttribute("apiServiceKey", new ApiServiceKey()); } return "page/add-edit-api-key"; } @PostMapping("/manage/saveApiKey") public String saveApiKey(@Valid ApiServiceKey apiServiceKey, BindingResult result, Model model) { if (result.hasErrors()) { return "page/add-edit-api-key"; } if (apiServiceKey.getId() == null) { apiKeyService.save(apiServiceKey); } else { apiKeyService.update(apiServiceKey); } return "redirect:/manage/findAllApiKeys"; } @RequestMapping("/manage/deleteApiKey/{id}") public String deleteApiKey(@PathVariable Long id) { if (id == null) { throw new IllegalArgumentException("No data, id: " + id); } apiKeyService.delete(id); return "redirect:/manage/findAllApiKeys"; } }