Skip to content

Commit

Permalink
Add update API for item/release
Browse files Browse the repository at this point in the history
  • Loading branch information
yiming187 committed Apr 12, 2016
1 parent 4c2d7f4 commit 7d90505
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 21 deletions.
Expand Up @@ -3,6 +3,8 @@
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;
Expand All @@ -15,6 +17,7 @@
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ItemChangeSets;
import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.exception.NotFoundException;

@RestController
public class ItemController {
Expand All @@ -25,18 +28,40 @@ public class ItemController {
@Autowired
private ItemService itemService;

@RequestMapping(path = "/items/", method = RequestMethod.POST)
public ResponseEntity<ItemDTO> create(@RequestBody ItemDTO dto) {
Item entity = BeanUtils.transfrom(Item.class, dto);
entity = itemService.save(entity);
dto = BeanUtils.transfrom(ItemDTO.class, entity);
return ResponseEntity.status(HttpStatus.CREATED).body(dto);
}

@RequestMapping(path = "/items/{itemId}", method = RequestMethod.DELETE)
public void delete(@PathVariable("itemId") long itemId) {
Item entity = itemService.findOne(itemId);
if (entity == null) throw new NotFoundException("item not found for itemId " + itemId);
itemService.delete(entity.getId());
}

@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items")
public List<ItemDTO> findItems(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
List<Item> items = viewService.findItems(appId, clusterName, namespaceName);
return BeanUtils.batchTransform(ItemDTO.class, items);
}

@RequestMapping("/items/{itemId}")
public ItemDTO findOne(@PathVariable("itemId") long itemId) {
public ItemDTO get(@PathVariable("itemId") long itemId) {
Item item = itemService.findOne(itemId);
return BeanUtils.transfrom(ItemDTO.class, item);
}

@RequestMapping(path = "/item/{itemId}", method = RequestMethod.PUT)
public ItemDTO update(@PathVariable("itemId") long itemId, @RequestBody ItemDTO dto) {
Item entity = itemService.findOne(itemId);
if (entity == null) throw new NotFoundException("item not found for itemId " + itemId);
entity = itemService.update(BeanUtils.transfrom(Item.class, dto));
return BeanUtils.transfrom(ItemDTO.class, entity);
}
}
Expand Up @@ -30,6 +30,14 @@ public class NamespaceController {
@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) {
if (!appId.equals(dto.getAppId())) {
throw new IllegalArgumentException(String
.format("Path variable %s is not equals to object field %s", appId, dto.getAppId()));
}
if (!clusterName.equals(dto.getClusterName())) {
throw new IllegalArgumentException(String.format(
"Path variable %s is not equals to object field %s", clusterName, dto.getClusterName()));
}
Namespace entity = BeanUtils.transfrom(Namespace.class, dto);
entity = namespaceService.save(entity);
dto = BeanUtils.transfrom(NamespaceDTO.class, entity);
Expand All @@ -41,8 +49,8 @@ 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);
if (entity == null) throw new NotFoundException(
String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
namespaceService.delete(entity.getId());
}

Expand All @@ -56,6 +64,8 @@ public List<NamespaceDTO> find(@PathVariable("appId") String appId,
@RequestMapping("/namespaces/{namespaceId}")
public NamespaceDTO get(@PathVariable("namespaceId") Long namespaceId) {
Namespace namespace = namespaceService.findOne(namespaceId);
if (namespace == null)
throw new NotFoundException(String.format("namespace not found for %s", namespaceId));
return BeanUtils.transfrom(NamespaceDTO.class, namespace);
}

Expand All @@ -64,21 +74,31 @@ public NamespaceDTO get(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
if (namespace == null) throw new NotFoundException(
String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
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 (!appId.equals(dto.getAppId())) {
throw new IllegalArgumentException(String
.format("Path variable %s is not equals to object field %s", appId, dto.getAppId()));
}
if (!clusterName.equals(dto.getClusterName())) {
throw new IllegalArgumentException(String.format(
"Path variable %s is not equals to object field %s", clusterName, dto.getClusterName()));
}
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);
if (entity == null) throw new NotFoundException(
String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
entity = namespaceService.update(BeanUtils.transfrom(Namespace.class, dto));
return BeanUtils.transfrom(NamespaceDTO.class, entity);
}
Expand Down
Expand Up @@ -5,6 +5,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ctrip.apollo.biz.entity.Release;
Expand All @@ -13,7 +15,7 @@
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ReleaseDTO;
import com.ctrip.apollo.core.utils.StringUtils;
import com.ctrip.apollo.core.exception.NotFoundException;

@RestController
public class ReleaseController {
Expand All @@ -28,28 +30,37 @@ public class ReleaseController {
private ConfigService configService;

@RequestMapping("/release/{releaseId}")
public ReleaseDTO findOne(@PathVariable("releaseId") long releaseId) {
public ReleaseDTO get(@PathVariable("releaseId") long releaseId) {
Release release = releaseService.findOne(releaseId);
if (release == null)
throw new NotFoundException(String.format("release not found for %s", releaseId));
return BeanUtils.transfrom(ReleaseDTO.class, release);
}

@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/releases")
public List<ReleaseDTO> findReleases(@PathVariable("appId") String appId,
public List<ReleaseDTO> find(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
List<Release> releases = viewSerivce.findReleases(appId, clusterName, namespaceName);
return BeanUtils.batchTransform(ReleaseDTO.class, releases);
}

@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/releases/latest")
public ReleaseDTO findLatestRelease(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName){

if (StringUtils.isContainEmpty(appId, clusterName, namespaceName)){
return null;
}
public ReleaseDTO getLatest(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
Release release = configService.findRelease(appId, clusterName, namespaceName);
if (release == null) throw new NotFoundException(
String.format("latest release not found for %s %s %s", appId, clusterName, namespaceName));
return BeanUtils.transfrom(ReleaseDTO.class, release);
}

@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/releases", method = RequestMethod.POST)
public ReleaseDTO buildRelease(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName, @RequestParam("name") String name,
@RequestParam(name = "comment", required = false) String comment) {
Release release = releaseService.buildRelease(name, comment, appId, clusterName, namespaceName);
return BeanUtils.transfrom(ReleaseDTO.class, release);
}
}
Expand Up @@ -3,7 +3,6 @@
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;
Expand Down
Expand Up @@ -5,16 +5,31 @@

import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.repository.ItemRepository;
import com.ctrip.apollo.biz.utils.BeanUtils;

@Service
public class ItemService {

@Autowired
private ItemRepository itemRepository;

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

public Item findOne(long itemId) {
Item item = itemRepository.findOne(itemId);
return item;
}

public Item save(Item item) {
return itemRepository.save(item);
}

public Item update(Item item) {
Item managedItem = itemRepository.findOne(item.getId());
BeanUtils.copyEntityProperties(item, managedItem);
return itemRepository.save(managedItem);
}

}
@@ -1,23 +1,65 @@
package com.ctrip.apollo.biz.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.entity.Namespace;
import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.repository.ItemRepository;
import com.ctrip.apollo.biz.repository.NamespaceRepository;
import com.ctrip.apollo.biz.repository.ReleaseRepository;
import com.ctrip.apollo.core.exception.NotFoundException;
import com.google.gson.Gson;

/**
* @author Jason Song(song_s@ctrip.com)
*/
@Service
public class ReleaseService {

@Autowired
private ReleaseRepository releaseRepository;


@Autowired
private NamespaceRepository namespaceRepository;

@Autowired
private ItemRepository itemRepository;

private Gson gson = new Gson();

public Release findOne(long releaseId) {
Release release = releaseRepository.findOne(releaseId);
return release;
}

public Release buildRelease(String name, String comment, String appId, String clusterName,
String namespaceName) {
Namespace namespace = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId,
clusterName, namespaceName);
if (namespace == null) {
throw new NotFoundException(String.format("Could not find namespace for %s %s %s", appId,
clusterName, namespaceName));
}
List<Item> items = itemRepository.findByNamespaceId(namespace.getId());
Map<String, String> configurations = new HashMap<String, String>();
for (Item item : items) {
configurations.put(item.getKey(), item.getValue());
}

Release release = new Release();
release.setName(name);
release.setComment(comment);
release.setAppId(appId);
release.setClusterName(clusterName);
release.setNamespaceName(namespaceName);
release.setConfigurations(gson.toJson(configurations));
return releaseRepository.save(release);
}

}
Expand Up @@ -14,7 +14,7 @@ public String getAppId() {
return appId;
}

public String getClusterId() {
public String getClusterName() {
return clusterName;
}

Expand Down

0 comments on commit 7d90505

Please sign in to comment.