시나리오 관리 추가
This commit is contained in:
+61
@@ -0,0 +1,61 @@
|
||||
package com.eactive.httpmockserver.client.controller;
|
||||
|
||||
import com.eactive.httpmockserver.client.dto.ApiScenarioDTO;
|
||||
import com.eactive.httpmockserver.client.entity.ApiScenario;
|
||||
import com.eactive.httpmockserver.client.mapper.ApiScenarioMapper;
|
||||
import com.eactive.httpmockserver.client.service.ApiScenarioMgmtService;
|
||||
import com.eactive.httpmockserver.common.util.SecurityUtil;
|
||||
import com.eactive.httpmockserver.user.entity.StaffUser;
|
||||
import com.eactive.httpmockserver.user.service.UserService;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/mgmt/api_scenario")
|
||||
public class ApiScenarioMgmtController {
|
||||
|
||||
private final ApiScenarioMgmtService apiScenarioMgmtService;
|
||||
|
||||
private final ApiScenarioMapper apiScenarioMapper;
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
public ApiScenarioMgmtController(ApiScenarioMgmtService apiScenarioMgmtService, ApiScenarioMapper apiScenarioMapper, UserService userService) {
|
||||
this.apiScenarioMgmtService = apiScenarioMgmtService;
|
||||
this.apiScenarioMapper = apiScenarioMapper;
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@GetMapping("/list.do")
|
||||
public ResponseEntity<List<ApiScenarioDTO>> listScenarios() {
|
||||
List<ApiScenario> scenarios = apiScenarioMgmtService.findAll();
|
||||
return ResponseEntity.ok(apiScenarioMapper.toDtoList(scenarios));
|
||||
}
|
||||
|
||||
@PostMapping("/create.do")
|
||||
public ResponseEntity<ApiScenarioDTO> createScenario(@RequestBody ApiScenarioDTO apiScenarioDto) {
|
||||
ApiScenario apiScenario = apiScenarioMapper.map(apiScenarioDto);
|
||||
StaffUser user = userService.findByEsntlId(SecurityUtil.getCurrentUserEsntlId());
|
||||
apiScenario.setOwner(user);
|
||||
ApiScenario createdScenario = apiScenarioMgmtService.createApiScenario(apiScenario);
|
||||
return new ResponseEntity<>(apiScenarioMapper.map(createdScenario), HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
|
||||
@PutMapping("/update.do")
|
||||
public ResponseEntity<ApiScenarioDTO> updateScenario(@RequestBody ApiScenarioDTO apiScenarioDto) {
|
||||
ApiScenario updatedApiScenario = apiScenarioMapper.map(apiScenarioDto);
|
||||
ApiScenario updatedScenario = apiScenarioMgmtService.updateApiScenario(apiScenarioDto.getId(), updatedApiScenario);
|
||||
return ResponseEntity.ok(apiScenarioMapper.map(updatedScenario));
|
||||
}
|
||||
|
||||
@PostMapping("/delete.do")
|
||||
public ResponseEntity<Void> deleteScenario(@RequestBody ApiScenarioDTO apiScenarioDto) {
|
||||
apiScenarioMgmtService.deleteApiScenario(apiScenarioDto.getId());
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.eactive.httpmockserver.client.dto;
|
||||
|
||||
public class ApiScenarioDTO {
|
||||
|
||||
private String id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
private String scenario;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getScenario() {
|
||||
return scenario;
|
||||
}
|
||||
|
||||
public void setScenario(String scenario) {
|
||||
this.scenario = scenario;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.eactive.httpmockserver.client.entity;
|
||||
|
||||
import com.eactive.httpmockserver.user.entity.StaffUser;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "API_SCENARIO")
|
||||
public class ApiScenario {
|
||||
|
||||
@Id
|
||||
private String id; //uuid
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "USER_ID")
|
||||
private StaffUser owner;
|
||||
|
||||
@Lob
|
||||
private String scenario;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getScenario() {
|
||||
return scenario;
|
||||
}
|
||||
|
||||
public void setScenario(String scenario) {
|
||||
this.scenario = scenario;
|
||||
}
|
||||
|
||||
public StaffUser getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public void setOwner(StaffUser owner) {
|
||||
this.owner = owner;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.eactive.httpmockserver.client.mapper;
|
||||
|
||||
import com.eactive.httpmockserver.client.dto.ApiScenarioDTO;
|
||||
import com.eactive.httpmockserver.client.entity.ApiScenario;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper(componentModel = "spring",
|
||||
unmappedTargetPolicy = ReportingPolicy.IGNORE)
|
||||
@Component
|
||||
public interface ApiScenarioMapper {
|
||||
|
||||
ApiScenario map(ApiScenarioDTO dto);
|
||||
|
||||
ApiScenarioDTO map(ApiScenario scenario);
|
||||
|
||||
List<ApiScenarioDTO> toDtoList(List<ApiScenario> scenarios);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.eactive.httpmockserver.client.repository;
|
||||
|
||||
import com.eactive.httpmockserver.client.entity.ApiScenario;
|
||||
import com.eactive.httpmockserver.user.entity.StaffUser;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ApiScenarioRepository extends JpaRepository<ApiScenario, String>, JpaSpecificationExecutor<ApiScenario> {
|
||||
|
||||
List<ApiScenario> findAllByOwner(StaffUser owner);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.eactive.httpmockserver.client.service;
|
||||
|
||||
import com.eactive.httpmockserver.client.entity.ApiScenario;
|
||||
import com.eactive.httpmockserver.client.repository.ApiScenarioRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class ApiScenarioMgmtService {
|
||||
|
||||
@Autowired
|
||||
ApiScenarioRepository apiScenarioRepository;
|
||||
|
||||
|
||||
public List<ApiScenario> findAll() {
|
||||
return apiScenarioRepository.findAll();
|
||||
}
|
||||
|
||||
public ApiScenario createApiScenario(ApiScenario apiScenario) {
|
||||
apiScenario.setId(UUID.randomUUID().toString());
|
||||
return apiScenarioRepository.save(apiScenario);
|
||||
}
|
||||
|
||||
|
||||
public ApiScenario updateApiScenario(String id, ApiScenario updatedApiScenario) {
|
||||
return apiScenarioRepository.findById(id)
|
||||
.map(apiScenario -> {
|
||||
apiScenario.setName(updatedApiScenario.getName());
|
||||
apiScenario.setDescription(updatedApiScenario.getDescription());
|
||||
apiScenario.setScenario(updatedApiScenario.getScenario());
|
||||
return apiScenarioRepository.save(apiScenario);
|
||||
}).orElseGet(() -> {
|
||||
// If the ApiScenario with the given id doesn't exist, create a new one
|
||||
updatedApiScenario.setId(id);
|
||||
return apiScenarioRepository.save(updatedApiScenario);
|
||||
});
|
||||
}
|
||||
|
||||
// Delete operation
|
||||
public void deleteApiScenario(String id) {
|
||||
apiScenarioRepository.deleteById(id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import org.springframework.boot.web.servlet.error.ErrorController;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@@ -23,4 +24,16 @@ public class PortalErrorController implements ErrorController {
|
||||
return "redirect:/";
|
||||
}
|
||||
|
||||
@PostMapping("/error")
|
||||
public String handleErrorPost(HttpServletRequest request, ModelMap model) {
|
||||
Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
|
||||
|
||||
if (status != null) {
|
||||
model.addAttribute("status", status);
|
||||
return "error";
|
||||
}
|
||||
|
||||
return "redirect:/";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user