##### Coverage Report - com.ctrip.framework.apollo.portal.controller.ServerConfigController

Classes in this File | Line Coverage | Branch Coverage | Complexity
ServerConfigController |
8% | 1/12
0% | 0/4
| 3.0;3

1 |  |
     package com.ctrip.framework.apollo.portal.controller;
2 |  |
3 |  |
4 |  |
     import com.ctrip.framework.apollo.common.utils.BeanUtils;
5 |  |
     import com.ctrip.framework.apollo.portal.auth.UserInfoHolder;
6 |  |
     import com.ctrip.framework.apollo.portal.entity.po.ServerConfig;
7 |  |
     import com.ctrip.framework.apollo.portal.repository.ServerConfigRepository;
8 |  |
9 |  |
     import org.springframework.beans.factory.annotation.Autowired;
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.RestController;
14 |  |
15 |  |
     import static com.ctrip.framework.apollo.portal.util.RequestPrecondition.checkArgument;
16 |  |
     import static com.ctrip.framework.apollo.portal.util.RequestPrecondition.checkModel;
17 |  |
18 |  |
     /**
19 |  |
      * 配置中心本身需要一些配置,这些配置放在数据库里面
20 |  |
      */
21 |  |
     @RestController
22 | 1 |
     public class ServerConfigController {
23 |  |
24 |  |
       @Autowired
25 |  |
       private ServerConfigRepository serverConfigRepository;
26 |  |
       @Autowired
27 |  |
       private UserInfoHolder userInfoHolder;
28 |  |
29 |  |
       @RequestMapping(value = "/server/config", method = RequestMethod.POST)
30 |  |
       public ServerConfig createOrUpdate(@RequestBody ServerConfig serverConfig) {
31 |  |
32 | 0 |
         checkModel(serverConfig != null);
33 | 0 |
         checkArgument(serverConfig.getKey(), serverConfig.getValue());
34 |  |
35 | 0 |
         String modifiedBy = userInfoHolder.getUser().getUserId();
36 |  |
37 | 0 |
         ServerConfig storedConfig = serverConfigRepository.findByKey(serverConfig.getKey());
38 |  |
39 | 0 |
         if (storedConfig == null) {//create
40 | 0 |
           serverConfig.setDataChangeCreatedBy(modifiedBy);
41 | 0 |
           serverConfig.setDataChangeLastModifiedBy(modifiedBy);
42 | 0 |
           return serverConfigRepository.save(serverConfig);
43 |  |
         } else {//update
44 | 0 |
           BeanUtils.copyEntityProperties(serverConfig, storedConfig);
45 | 0 |
           storedConfig.setDataChangeLastModifiedBy(modifiedBy);
46 | 0 |
           return serverConfigRepository.save(storedConfig);
47 |  |
         }
48 |  |
49 |  |
       }
50 |  |
51 |  |
52 |  |
     }