Skip to content

Commit

Permalink
Add CRUD for Cluster and Namespace Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
yiming187 committed Apr 11, 2016
1 parent 24ab7fb commit 48c2474
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 29 deletions.
Expand Up @@ -27,22 +27,22 @@ public class AppController {
private AppService appService;

@RequestMapping(path = "/apps/", method = RequestMethod.POST)
public ResponseEntity<AppDTO> createApp(@RequestBody AppDTO appDTO) {
App app = BeanUtils.transfrom(App.class, appDTO);
app = appService.save(app);
AppDTO dto = BeanUtils.transfrom(AppDTO.class, app);
public ResponseEntity<AppDTO> create(@RequestBody AppDTO dto) {
App entity = BeanUtils.transfrom(App.class, dto);
entity = appService.save(entity);
dto = BeanUtils.transfrom(AppDTO.class, entity);
return ResponseEntity.status(HttpStatus.CREATED).body(dto);
}

@RequestMapping(path = "/apps/{appId}", method = RequestMethod.DELETE)
public void deleteApp(@PathVariable("appId") String appId) {
App app = appService.findOne(appId);
if (app == null) throw new NotFoundException("app not found for appId " + appId);
appService.delete(app.getId());
public void delete(@PathVariable("appId") String appId) {
App entity = appService.findOne(appId);
if (entity == null) throw new NotFoundException("app not found for appId " + appId);
appService.delete(entity.getId());
}

@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) {
List<App> app = null;
if (StringUtils.isBlank(name)) {
Expand All @@ -54,22 +54,22 @@ public List<AppDTO> findApps(@RequestParam(value = "name", required = false) Str
}

@RequestMapping("/apps/{appId}")
public AppDTO getApp(@PathVariable("appId") String appId) {
public AppDTO get(@PathVariable("appId") String appId) {
App app = appService.findOne(appId);
if (app == null) throw new NotFoundException("app not found for appId " + appId);
return BeanUtils.transfrom(AppDTO.class, app);
}

@RequestMapping(path = "/apps/{appId}", method = RequestMethod.PUT)
public AppDTO updateApp(@PathVariable("appId") String appId, @RequestBody AppDTO appDTO) {
if (!appId.equals(appDTO.getAppId())) {
public AppDTO update(@PathVariable("appId") String appId, @RequestBody AppDTO dto) {
if (!appId.equals(dto.getAppId())) {
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);
if (app == null) throw new NotFoundException("app not found for appId " + appId);
app = appService.update(BeanUtils.transfrom(App.class, appDTO));
return BeanUtils.transfrom(AppDTO.class, app);
App entity = appService.findOne(appId);
if (entity == null) throw new NotFoundException("app not found for appId " + appId);
entity = appService.update(BeanUtils.transfrom(App.class, dto));
return BeanUtils.transfrom(AppDTO.class, entity);
}

}
Expand Up @@ -3,15 +3,20 @@
import java.util.List;

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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.service.ClusterService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.exception.NotFoundException;

@RestController
public class ClusterController {
Expand All @@ -22,16 +27,48 @@ public class ClusterController {
@Autowired
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")
public List<ClusterDTO> findClusters(@PathVariable("appId") String appId) {
public List<ClusterDTO> find(@PathVariable("appId") String appId) {
List<Cluster> clusters = viewService.findClusters(appId);
return BeanUtils.batchTransform(ClusterDTO.class, clusters);
}

@RequestMapping("/apps/{appId}/clusters/{clusterName}")
public ClusterDTO getCluster(@PathVariable("appId") String appId,
public ClusterDTO get(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName) {
Cluster cluster = clusterService.findOne(appId, clusterName);
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);
}

}
Expand Up @@ -3,15 +3,20 @@
import java.util.List;

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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ctrip.apollo.biz.entity.Namespace;
import com.ctrip.apollo.biz.service.NamespaceService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.NamespaceDTO;
import com.ctrip.apollo.core.exception.NotFoundException;

@RestController
public class NamespaceController {
Expand All @@ -22,25 +27,59 @@ public class NamespaceController {
@Autowired
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")
public List<NamespaceDTO> findNamespaces(@PathVariable("appId") String appId,
public List<NamespaceDTO> find(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName) {
List<Namespace> groups = viewService.findNamespaces(appId, clusterName);
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}")
public NamespaceDTO getNamespace(@PathVariable("appId") String appId,
public NamespaceDTO get(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
Namespace namespace = namespaceService.findOne(appId,
clusterName, namespaceName);
Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
return BeanUtils.transfrom(NamespaceDTO.class, namespace);
}

@RequestMapping("/namespaces/{namespaceId}")
public NamespaceDTO getNamespace(@PathVariable("namespaceId") Long namespaceId) {
Namespace namespace = namespaceService.findOne(namespaceId);
return BeanUtils.transfrom(NamespaceDTO.class, namespace);
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}", method = RequestMethod.PUT)
public NamespaceDTO update(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@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);
}
}
Expand Up @@ -3,8 +3,10 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.repository.ClusterRepository;
import com.ctrip.apollo.biz.utils.BeanUtils;

@Service
public class ClusterService {
Expand All @@ -15,4 +17,19 @@ public class ClusterService {
public Cluster findOne(String appId, String 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);
}
}
Expand Up @@ -5,20 +5,35 @@

import com.ctrip.apollo.biz.entity.Namespace;
import com.ctrip.apollo.biz.repository.NamespaceRepository;
import com.ctrip.apollo.biz.utils.BeanUtils;

@Service
public class NamespaceService {

@Autowired
private NamespaceRepository namespaceRepository;

public void delete(long id) {
namespaceRepository.delete(id);
}

public Namespace findOne(Long namespaceId) {
return namespaceRepository.findOne(namespaceId);
}

public Namespace findOne(String appId, String clusterName,
String namespaceName) {
public Namespace findOne(String appId, String clusterName, String namespaceName) {
return namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId, clusterName,
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);
}
}

0 comments on commit 48c2474

Please sign in to comment.