| 1 | |
package com.ctrip.apollo.adminservice.controller; |
| 2 | |
|
| 3 | |
import java.util.List; |
| 4 | |
|
| 5 | |
import org.springframework.beans.factory.annotation.Autowired; |
| 6 | |
import org.springframework.data.domain.Pageable; |
| 7 | |
import org.springframework.http.HttpStatus; |
| 8 | |
import org.springframework.http.ResponseEntity; |
| 9 | |
import org.springframework.web.bind.annotation.PathVariable; |
| 10 | |
import org.springframework.web.bind.annotation.RequestBody; |
| 11 | |
import org.springframework.web.bind.annotation.RequestMapping; |
| 12 | |
import org.springframework.web.bind.annotation.RequestMethod; |
| 13 | |
import org.springframework.web.bind.annotation.RequestParam; |
| 14 | |
import org.springframework.web.bind.annotation.RestController; |
| 15 | |
|
| 16 | |
import com.ctrip.apollo.biz.entity.App; |
| 17 | |
import com.ctrip.apollo.biz.service.AdminService; |
| 18 | |
import com.ctrip.apollo.biz.service.AppService; |
| 19 | |
import com.ctrip.apollo.common.utils.BeanUtils; |
| 20 | |
import com.ctrip.apollo.core.dto.AppDTO; |
| 21 | |
import com.ctrip.apollo.core.exception.NotFoundException; |
| 22 | |
import com.ctrip.apollo.core.utils.StringUtils; |
| 23 | |
|
| 24 | |
@RestController |
| 25 | 1 | public class AppController { |
| 26 | |
|
| 27 | |
@Autowired |
| 28 | |
private AppService appService; |
| 29 | |
@Autowired |
| 30 | |
private AdminService adminService; |
| 31 | |
|
| 32 | |
@RequestMapping(path = "/apps", method = RequestMethod.POST) |
| 33 | |
public ResponseEntity<AppDTO> create(@RequestBody AppDTO dto) { |
| 34 | 1 | App entity = BeanUtils.transfrom(App.class, dto); |
| 35 | 1 | entity = adminService.createNewApp(entity); |
| 36 | 1 | dto = BeanUtils.transfrom(AppDTO.class, entity); |
| 37 | 1 | return ResponseEntity.status(HttpStatus.CREATED).body(dto); |
| 38 | |
} |
| 39 | |
|
| 40 | |
@RequestMapping(path = "/apps/{appId}", method = RequestMethod.DELETE) |
| 41 | |
public void delete(@PathVariable("appId") String appId) { |
| 42 | 1 | App entity = appService.findOne(appId); |
| 43 | 1 | if (entity == null) throw new NotFoundException("app not found for appId " + appId); |
| 44 | 1 | appService.delete(entity.getId()); |
| 45 | 1 | } |
| 46 | |
|
| 47 | |
@RequestMapping("/apps") |
| 48 | |
public List<AppDTO> find(@RequestParam(value = "name", required = false) String name, |
| 49 | |
Pageable pageable) { |
| 50 | 0 | List<App> app = null; |
| 51 | 0 | if (StringUtils.isBlank(name)) { |
| 52 | 0 | app = appService.findAll(pageable); |
| 53 | |
} else { |
| 54 | 0 | app = appService.findByName(name); |
| 55 | |
} |
| 56 | 0 | return BeanUtils.batchTransform(AppDTO.class, app); |
| 57 | |
} |
| 58 | |
|
| 59 | |
@RequestMapping("/apps/{appId}") |
| 60 | |
public AppDTO get(@PathVariable("appId") String appId) { |
| 61 | 6 | App app = appService.findOne(appId); |
| 62 | 6 | if (app == null) throw new NotFoundException("app not found for appId " + appId); |
| 63 | 5 | return BeanUtils.transfrom(AppDTO.class, app); |
| 64 | |
} |
| 65 | |
|
| 66 | |
@RequestMapping(path = "/apps/{appId}", method = RequestMethod.PUT) |
| 67 | |
public AppDTO update(@PathVariable("appId") String appId, @RequestBody AppDTO dto) { |
| 68 | 1 | if (!appId.equals(dto.getAppId())) { |
| 69 | 0 | throw new IllegalArgumentException(String |
| 70 | 0 | .format("Path variable %s is not equals to object field %s", appId, dto.getAppId())); |
| 71 | |
} |
| 72 | 1 | App entity = appService.findOne(appId); |
| 73 | 1 | if (entity == null) throw new NotFoundException("app not found for appId " + appId); |
| 74 | 1 | entity = appService.update(BeanUtils.transfrom(App.class, dto)); |
| 75 | 1 | return BeanUtils.transfrom(AppDTO.class, entity); |
| 76 | |
} |
| 77 | |
|
| 78 | |
} |