diff --git a/src/main/java/com/eactive/httpmockserver/server/controller/ServerMgmtController.java b/src/main/java/com/eactive/httpmockserver/server/controller/ServerMgmtController.java new file mode 100644 index 0000000..c4e1ab2 --- /dev/null +++ b/src/main/java/com/eactive/httpmockserver/server/controller/ServerMgmtController.java @@ -0,0 +1,117 @@ +package com.eactive.httpmockserver.server.controller; + +import com.eactive.httpmockserver.client.mapper.ServerMapper; +import com.eactive.httpmockserver.server.dto.ServerDTO; +import com.eactive.httpmockserver.server.dto.ServerSearch; +import com.eactive.httpmockserver.server.entity.Server; +import com.eactive.httpmockserver.server.service.ServerMgmtService; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; +import org.springframework.validation.Validator; +import org.springframework.web.bind.annotation.*; + + +@Controller +@RequestMapping("/mgmt/servers") +public class ServerMgmtController { + + @Autowired + ServerMgmtService serverMgmtService; + + @Autowired + ServerMapper serverMapper; + + @Autowired + private Validator validator; + + public static final String SERVER_EDIT = "page/servers/serverEdit"; + + public static final String SERVER_LIST_VIEW = "redirect:/mgmt/servers/list_view.do"; + + @GetMapping("/list_view.do") + public String listView(@ModelAttribute("serverSearch") ServerSearch serverSearch, + ModelMap model, + Pageable pageable) { + + Page page = serverMgmtService.findAll(serverSearch.buildSpecification(), pageable); + model.addAttribute("page", page); + model.addAttribute("pageable", pageable); + model.addAttribute("modelSearch", serverSearch); + + return "page/servers/serverList"; + } + + @GetMapping("/create_view.do") + public String createView(ModelMap model) { + ServerDTO defaultServer = new ServerDTO(); + model.addAttribute("server", defaultServer); + return SERVER_EDIT; + } + + @GetMapping("/update_view.do") + public String updateView(ModelMap model, @RequestParam("id") Long id) { + Server found = serverMgmtService.findById(id); + ServerDTO server = serverMapper.map(found); + model.addAttribute("server", server); + return SERVER_EDIT; + } + + @PostMapping("/register.do") + public String register(@ModelAttribute("server") ServerDTO dto, + BindingResult bindingResult, + ModelMap model) { + + if (dto == null) { + return "redirect:/mgmt/servers/create_view.do"; + } + + validator.validate(dto, bindingResult); + + if (bindingResult.hasErrors()) { + model.addAttribute("server", dto); + return SERVER_EDIT; + } + + Server server = serverMapper.map(dto); + serverMgmtService.create(server); + return SERVER_LIST_VIEW; + } + + @PostMapping("/update.do") + public String update(@ModelAttribute("server") ServerDTO dto, + BindingResult bindingResult, + ModelMap model) { + + if (StringUtils.isEmpty(dto.getId().toString())) { + return SERVER_LIST_VIEW; + } + + validator.validate(dto, bindingResult); + + if (bindingResult.hasErrors()) { + model.addAttribute("server", dto); + return SERVER_EDIT; + } + + Server updated = serverMapper.map(dto); + serverMgmtService.update(dto.getId(), updated); + return SERVER_LIST_VIEW; + } + + @PostMapping("/delete.do") + public String delete(@ModelAttribute ServerSearch modelSearch) { + + modelSearch.getCheckedIdForDel().forEach(id -> { + serverMgmtService.delete(Long.parseLong(id)); + }); + + return SERVER_LIST_VIEW; + } + + +} diff --git a/src/main/java/com/eactive/httpmockserver/server/dto/ServerDTO.java b/src/main/java/com/eactive/httpmockserver/server/dto/ServerDTO.java new file mode 100644 index 0000000..7c7ce2b --- /dev/null +++ b/src/main/java/com/eactive/httpmockserver/server/dto/ServerDTO.java @@ -0,0 +1,70 @@ +package com.eactive.httpmockserver.server.dto; + +import javax.validation.constraints.NotEmpty; + +public class ServerDTO { + + private Long id; + + @NotEmpty + private String name; + + @NotEmpty + private String scheme = "http"; + + @NotEmpty + private String hostname; + + @NotEmpty + private String basePath = "/"; + + private int port = 80; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getScheme() { + return scheme; + } + + public void setScheme(String scheme) { + this.scheme = scheme; + } + + public String getHostname() { + return hostname; + } + + public void setHostname(String hostname) { + this.hostname = hostname; + } + + public String getBasePath() { + return basePath; + } + + public void setBasePath(String basePath) { + this.basePath = basePath; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } +} diff --git a/src/main/java/com/eactive/httpmockserver/server/dto/ServerSearch.java b/src/main/java/com/eactive/httpmockserver/server/dto/ServerSearch.java new file mode 100644 index 0000000..9693c38 --- /dev/null +++ b/src/main/java/com/eactive/httpmockserver/server/dto/ServerSearch.java @@ -0,0 +1,74 @@ +package com.eactive.httpmockserver.server.dto; + +import com.eactive.httpmockserver.common.search.BaseSearch; +import com.eactive.httpmockserver.common.search.ColumnSearchModel; +import com.eactive.httpmockserver.common.search.SearchCondition; +import com.eactive.httpmockserver.common.search.SearchModel; +import com.eactive.httpmockserver.server.entity.Server; + +import java.util.ArrayList; +import java.util.List; + +public class ServerSearch implements BaseSearch { + + private String name; + + private String hostname; + + private String basePath; + + private int port; + + private List checkedIdForDel; + + @Override + public List buildSearchCondition() { + List models = new ArrayList<>(); + models.add(new ColumnSearchModel("name", SearchCondition.LIKE, name)); + models.add(new ColumnSearchModel("hostname", SearchCondition.LIKE, hostname)); + models.add(new ColumnSearchModel("basePath", SearchCondition.LIKE, basePath)); + models.add(new ColumnSearchModel("port", SearchCondition.EQUAL, port)); + + return models; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getHostname() { + return hostname; + } + + public void setHostname(String hostname) { + this.hostname = hostname; + } + + public String getBasePath() { + return basePath; + } + + public void setBasePath(String basePath) { + this.basePath = basePath; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public List getCheckedIdForDel() { + return checkedIdForDel; + } + + public void setCheckedIdForDel(List checkedIdForDel) { + this.checkedIdForDel = checkedIdForDel; + } +} diff --git a/src/main/java/com/eactive/httpmockserver/client/entity/Server.java b/src/main/java/com/eactive/httpmockserver/server/entity/Server.java similarity index 96% rename from src/main/java/com/eactive/httpmockserver/client/entity/Server.java rename to src/main/java/com/eactive/httpmockserver/server/entity/Server.java index f37d71a..1a1a7d0 100644 --- a/src/main/java/com/eactive/httpmockserver/client/entity/Server.java +++ b/src/main/java/com/eactive/httpmockserver/server/entity/Server.java @@ -1,4 +1,4 @@ -package com.eactive.httpmockserver.client.entity; +package com.eactive.httpmockserver.server.entity; import javax.persistence.*; diff --git a/src/main/java/com/eactive/httpmockserver/server/service/ServerMgmtService.java b/src/main/java/com/eactive/httpmockserver/server/service/ServerMgmtService.java new file mode 100644 index 0000000..d512331 --- /dev/null +++ b/src/main/java/com/eactive/httpmockserver/server/service/ServerMgmtService.java @@ -0,0 +1,50 @@ +package com.eactive.httpmockserver.server.service; + +import com.eactive.httpmockserver.client.repository.ServerRepository; +import com.eactive.httpmockserver.common.exception.NotFoundException; +import com.eactive.httpmockserver.server.entity.Server; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.domain.Specification; +import org.springframework.stereotype.Service; + +import javax.transaction.Transactional; + +@Service +@Transactional +public class ServerMgmtService { + + @Autowired + private ServerRepository serverRepository; + + public Page findAll(Specification serverSpecification, Pageable pageable) { + return serverRepository.findAll(serverSpecification, pageable); + } + + public Server findById(Long id) { + return serverRepository.findById(id).orElseThrow(() -> new NotFoundException("Server Not Found")); + } + + public void create(Server server) { + serverRepository.save(server); + } + + public void update(Long id, Server updated) { + Server server = serverRepository.findById(id).orElseThrow(() -> new NotFoundException("Server Not Found")); + + server.setName(updated.getName()); + server.setScheme(updated.getScheme()); + server.setHostname(updated.getHostname()); + server.setBasePath(updated.getBasePath()); + server.setPort(updated.getPort()); + + serverRepository.save(server); + + + } + + public void delete(Long id) { + serverRepository.deleteById(id); + } +}