Commit 48c2474b by Yiming Liu

Add CRUD for Cluster and Namespace Entity

parent 24ab7fb2
...@@ -27,22 +27,22 @@ public class AppController { ...@@ -27,22 +27,22 @@ public class AppController {
private AppService appService; private AppService appService;
@RequestMapping(path = "/apps/", method = RequestMethod.POST) @RequestMapping(path = "/apps/", method = RequestMethod.POST)
public ResponseEntity<AppDTO> createApp(@RequestBody AppDTO appDTO) { public ResponseEntity<AppDTO> create(@RequestBody AppDTO dto) {
App app = BeanUtils.transfrom(App.class, appDTO); App entity = BeanUtils.transfrom(App.class, dto);
app = appService.save(app); entity = appService.save(entity);
AppDTO dto = BeanUtils.transfrom(AppDTO.class, app); dto = BeanUtils.transfrom(AppDTO.class, entity);
return ResponseEntity.status(HttpStatus.CREATED).body(dto); return ResponseEntity.status(HttpStatus.CREATED).body(dto);
} }
@RequestMapping(path = "/apps/{appId}", method = RequestMethod.DELETE) @RequestMapping(path = "/apps/{appId}", method = RequestMethod.DELETE)
public void deleteApp(@PathVariable("appId") String appId) { public void delete(@PathVariable("appId") String appId) {
App app = appService.findOne(appId); App entity = appService.findOne(appId);
if (app == null) throw new NotFoundException("app not found for appId " + appId); if (entity == null) throw new NotFoundException("app not found for appId " + appId);
appService.delete(app.getId()); appService.delete(entity.getId());
} }
@RequestMapping("/apps") @RequestMapping("/apps")
public List<AppDTO> findApps(@RequestParam(value = "name", required = false) String name, public List<AppDTO> find(@RequestParam(value = "name", required = false) String name,
Pageable pageable) { Pageable pageable) {
List<App> app = null; List<App> app = null;
if (StringUtils.isBlank(name)) { if (StringUtils.isBlank(name)) {
...@@ -54,22 +54,22 @@ public class AppController { ...@@ -54,22 +54,22 @@ public class AppController {
} }
@RequestMapping("/apps/{appId}") @RequestMapping("/apps/{appId}")
public AppDTO getApp(@PathVariable("appId") String appId) { public AppDTO get(@PathVariable("appId") String appId) {
App app = appService.findOne(appId); App app = appService.findOne(appId);
if (app == null) throw new NotFoundException("app not found for appId " + appId); if (app == null) throw new NotFoundException("app not found for appId " + appId);
return BeanUtils.transfrom(AppDTO.class, app); return BeanUtils.transfrom(AppDTO.class, app);
} }
@RequestMapping(path = "/apps/{appId}", method = RequestMethod.PUT) @RequestMapping(path = "/apps/{appId}", method = RequestMethod.PUT)
public AppDTO updateApp(@PathVariable("appId") String appId, @RequestBody AppDTO appDTO) { public AppDTO update(@PathVariable("appId") String appId, @RequestBody AppDTO dto) {
if (!appId.equals(appDTO.getAppId())) { if (!appId.equals(dto.getAppId())) {
throw new IllegalArgumentException(String throw new IllegalArgumentException(String
.format("Path variable %s is not equals to object field %s", appId, appDTO.getAppId())); .format("Path variable %s is not equals to object field %s", appId, dto.getAppId()));
} }
App app = appService.findOne(appId); App entity = appService.findOne(appId);
if (app == null) throw new NotFoundException("app not found for appId " + appId); if (entity == null) throw new NotFoundException("app not found for appId " + appId);
app = appService.update(BeanUtils.transfrom(App.class, appDTO)); entity = appService.update(BeanUtils.transfrom(App.class, dto));
return BeanUtils.transfrom(AppDTO.class, app); return BeanUtils.transfrom(AppDTO.class, entity);
} }
} }
...@@ -3,8 +3,12 @@ package com.ctrip.apollo.adminservice.controller; ...@@ -3,8 +3,12 @@ package com.ctrip.apollo.adminservice.controller;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.Cluster; import com.ctrip.apollo.biz.entity.Cluster;
...@@ -12,6 +16,7 @@ import com.ctrip.apollo.biz.service.ClusterService; ...@@ -12,6 +16,7 @@ import com.ctrip.apollo.biz.service.ClusterService;
import com.ctrip.apollo.biz.service.ViewService; import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils; import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ClusterDTO; import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.exception.NotFoundException;
@RestController @RestController
public class ClusterController { public class ClusterController {
...@@ -22,16 +27,48 @@ public class ClusterController { ...@@ -22,16 +27,48 @@ public class ClusterController {
@Autowired @Autowired
private ClusterService clusterService; private ClusterService clusterService;
@RequestMapping(path = "/apps/{appId}/clusters", method = RequestMethod.POST)
public ResponseEntity<ClusterDTO> create(@PathVariable("appId") String appId,
@RequestBody ClusterDTO dto) {
Cluster entity = BeanUtils.transfrom(Cluster.class, dto);
entity = clusterService.save(entity);
dto = BeanUtils.transfrom(ClusterDTO.class, entity);
return ResponseEntity.status(HttpStatus.CREATED).body(dto);
}
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}", method = RequestMethod.DELETE)
public void delete(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName) {
Cluster entity = clusterService.findOne(appId, clusterName);
if (entity == null)
throw new NotFoundException("cluster not found for clusterName " + clusterName);
clusterService.delete(entity.getId());
}
@RequestMapping("/apps/{appId}/clusters") @RequestMapping("/apps/{appId}/clusters")
public List<ClusterDTO> findClusters(@PathVariable("appId") String appId) { public List<ClusterDTO> find(@PathVariable("appId") String appId) {
List<Cluster> clusters = viewService.findClusters(appId); List<Cluster> clusters = viewService.findClusters(appId);
return BeanUtils.batchTransform(ClusterDTO.class, clusters); return BeanUtils.batchTransform(ClusterDTO.class, clusters);
} }
@RequestMapping("/apps/{appId}/clusters/{clusterName}") @RequestMapping("/apps/{appId}/clusters/{clusterName}")
public ClusterDTO getCluster(@PathVariable("appId") String appId, public ClusterDTO get(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName) { @PathVariable("clusterName") String clusterName) {
Cluster cluster = clusterService.findOne(appId, clusterName); Cluster cluster = clusterService.findOne(appId, clusterName);
return BeanUtils.transfrom(ClusterDTO.class, cluster); return BeanUtils.transfrom(ClusterDTO.class, cluster);
} }
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}", method = RequestMethod.PUT)
public ClusterDTO update(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName, @RequestBody ClusterDTO dto) {
if (!clusterName.equals(dto.getName())) {
throw new IllegalArgumentException(String
.format("Path variable %s is not equals to object field %s", clusterName, dto.getName()));
}
Cluster entity = clusterService.findOne(appId, clusterName);
if (entity == null) throw new NotFoundException("cluster not found for name " + clusterName);
entity = clusterService.update(BeanUtils.transfrom(Cluster.class, dto));
return BeanUtils.transfrom(ClusterDTO.class, entity);
}
} }
...@@ -3,8 +3,12 @@ package com.ctrip.apollo.adminservice.controller; ...@@ -3,8 +3,12 @@ package com.ctrip.apollo.adminservice.controller;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.Namespace; import com.ctrip.apollo.biz.entity.Namespace;
...@@ -12,6 +16,7 @@ import com.ctrip.apollo.biz.service.NamespaceService; ...@@ -12,6 +16,7 @@ import com.ctrip.apollo.biz.service.NamespaceService;
import com.ctrip.apollo.biz.service.ViewService; import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils; import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.NamespaceDTO; import com.ctrip.apollo.core.dto.NamespaceDTO;
import com.ctrip.apollo.core.exception.NotFoundException;
@RestController @RestController
public class NamespaceController { public class NamespaceController {
...@@ -22,25 +27,59 @@ public class NamespaceController { ...@@ -22,25 +27,59 @@ public class NamespaceController {
@Autowired @Autowired
private NamespaceService namespaceService; private NamespaceService namespaceService;
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces", method = RequestMethod.POST)
public ResponseEntity<NamespaceDTO> create(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName, @RequestBody NamespaceDTO dto) {
Namespace entity = BeanUtils.transfrom(Namespace.class, dto);
entity = namespaceService.save(entity);
dto = BeanUtils.transfrom(NamespaceDTO.class, entity);
return ResponseEntity.status(HttpStatus.CREATED).body(dto);
}
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}", method = RequestMethod.DELETE)
public void delete(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
Namespace entity = namespaceService.findOne(appId, clusterName, namespaceName);
if (entity == null)
throw new NotFoundException("namespace not found for namespaceName " + namespaceName);
namespaceService.delete(entity.getId());
}
@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces") @RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces")
public List<NamespaceDTO> findNamespaces(@PathVariable("appId") String appId, public List<NamespaceDTO> find(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName) { @PathVariable("clusterName") String clusterName) {
List<Namespace> groups = viewService.findNamespaces(appId, clusterName); List<Namespace> groups = viewService.findNamespaces(appId, clusterName);
return BeanUtils.batchTransform(NamespaceDTO.class, groups); return BeanUtils.batchTransform(NamespaceDTO.class, groups);
} }
@RequestMapping("/namespaces/{namespaceId}")
public NamespaceDTO get(@PathVariable("namespaceId") Long namespaceId) {
Namespace namespace = namespaceService.findOne(namespaceId);
return BeanUtils.transfrom(NamespaceDTO.class, namespace);
}
@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}") @RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}")
public NamespaceDTO getNamespace(@PathVariable("appId") String appId, public NamespaceDTO get(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName, @PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) { @PathVariable("namespaceName") String namespaceName) {
Namespace namespace = namespaceService.findOne(appId, Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
clusterName, namespaceName);
return BeanUtils.transfrom(NamespaceDTO.class, namespace); return BeanUtils.transfrom(NamespaceDTO.class, namespace);
} }
@RequestMapping("/namespaces/{namespaceId}") @RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}", method = RequestMethod.PUT)
public NamespaceDTO getNamespace(@PathVariable("namespaceId") Long namespaceId) { public NamespaceDTO update(@PathVariable("appId") String appId,
Namespace namespace = namespaceService.findOne(namespaceId); @PathVariable("clusterName") String clusterName,
return BeanUtils.transfrom(NamespaceDTO.class, namespace); @PathVariable("namespaceName") String namespaceName, @RequestBody NamespaceDTO dto) {
if (!namespaceName.equals(dto.getNamespaceName())) {
throw new IllegalArgumentException(
String.format("Path variable %s is not equals to object field %s", namespaceName,
dto.getNamespaceName()));
}
Namespace entity = namespaceService.findOne(appId, clusterName, namespaceName);
if (entity == null)
throw new NotFoundException("namespace not found for name " + namespaceName);
entity = namespaceService.update(BeanUtils.transfrom(Namespace.class, dto));
return BeanUtils.transfrom(NamespaceDTO.class, entity);
} }
} }
...@@ -3,8 +3,10 @@ package com.ctrip.apollo.biz.service; ...@@ -3,8 +3,10 @@ package com.ctrip.apollo.biz.service;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.entity.Cluster; import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.repository.ClusterRepository; import com.ctrip.apollo.biz.repository.ClusterRepository;
import com.ctrip.apollo.biz.utils.BeanUtils;
@Service @Service
public class ClusterService { public class ClusterService {
...@@ -15,4 +17,19 @@ public class ClusterService { ...@@ -15,4 +17,19 @@ public class ClusterService {
public Cluster findOne(String appId, String name) { public Cluster findOne(String appId, String name) {
return clusterRepository.findByAppIdAndName(appId, name); return clusterRepository.findByAppIdAndName(appId, name);
} }
public Cluster save(Cluster entity) {
return clusterRepository.save(entity);
}
public void delete(long id) {
clusterRepository.delete(id);
}
public Cluster update(Cluster cluster) {
Cluster managedCluster =
clusterRepository.findByAppIdAndName(cluster.getAppId(), cluster.getName());
BeanUtils.copyEntityProperties(cluster, managedCluster);
return clusterRepository.save(managedCluster);
}
} }
...@@ -5,6 +5,7 @@ import org.springframework.stereotype.Service; ...@@ -5,6 +5,7 @@ import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.Namespace; import com.ctrip.apollo.biz.entity.Namespace;
import com.ctrip.apollo.biz.repository.NamespaceRepository; import com.ctrip.apollo.biz.repository.NamespaceRepository;
import com.ctrip.apollo.biz.utils.BeanUtils;
@Service @Service
public class NamespaceService { public class NamespaceService {
...@@ -12,13 +13,27 @@ public class NamespaceService { ...@@ -12,13 +13,27 @@ public class NamespaceService {
@Autowired @Autowired
private NamespaceRepository namespaceRepository; private NamespaceRepository namespaceRepository;
public void delete(long id) {
namespaceRepository.delete(id);
}
public Namespace findOne(Long namespaceId) { public Namespace findOne(Long namespaceId) {
return namespaceRepository.findOne(namespaceId); return namespaceRepository.findOne(namespaceId);
} }
public Namespace findOne(String appId, String clusterName, public Namespace findOne(String appId, String clusterName, String namespaceName) {
String namespaceName) {
return namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId, clusterName, return namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId, clusterName,
namespaceName); namespaceName);
} }
public Namespace save(Namespace entity) {
return namespaceRepository.save(entity);
}
public Namespace update(Namespace namespace) {
Namespace managedNamespace = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(
namespace.getAppId(), namespace.getClusterName(), namespace.getNamespaceName());
BeanUtils.copyEntityProperties(namespace, managedNamespace);
return namespaceRepository.save(managedNamespace);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment