| 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.core.exception.BadRequestException; |
| 6 | |
import com.ctrip.framework.apollo.core.utils.StringUtils; |
| 7 | |
import com.ctrip.framework.apollo.portal.auth.UserInfoHolder; |
| 8 | |
import com.ctrip.framework.apollo.portal.entity.po.ServerConfig; |
| 9 | |
import com.ctrip.framework.apollo.portal.repository.ServerConfigRepository; |
| 10 | |
|
| 11 | |
import org.springframework.beans.factory.annotation.Autowired; |
| 12 | |
import org.springframework.web.bind.annotation.RequestBody; |
| 13 | |
import org.springframework.web.bind.annotation.RequestMapping; |
| 14 | |
import org.springframework.web.bind.annotation.RequestMethod; |
| 15 | |
import org.springframework.web.bind.annotation.RestController; |
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
@RestController |
| 21 | 1 | public class PortalServerConfigController { |
| 22 | |
|
| 23 | |
@Autowired |
| 24 | |
private ServerConfigRepository serverConfigRepository; |
| 25 | |
@Autowired |
| 26 | |
private UserInfoHolder userInfoHolder; |
| 27 | |
|
| 28 | |
@RequestMapping(value = "/server/config", method = RequestMethod.POST) |
| 29 | |
public ServerConfig createOrUpdate(@RequestBody ServerConfig serverConfig) { |
| 30 | |
|
| 31 | 0 | if (serverConfig == null || StringUtils.isContainEmpty(serverConfig.getKey(), serverConfig.getValue())) { |
| 32 | 0 | throw new BadRequestException("request payload contains empty"); |
| 33 | |
} |
| 34 | |
|
| 35 | 0 | String modifiedBy = userInfoHolder.getUser().getUserId(); |
| 36 | |
|
| 37 | 0 | ServerConfig storedConfig = serverConfigRepository.findByKey(serverConfig.getKey()); |
| 38 | |
|
| 39 | 0 | if (storedConfig == null) { |
| 40 | 0 | serverConfig.setDataChangeCreatedBy(modifiedBy); |
| 41 | 0 | serverConfig.setDataChangeLastModifiedBy(modifiedBy); |
| 42 | 0 | return serverConfigRepository.save(serverConfig); |
| 43 | |
} else { |
| 44 | 0 | BeanUtils.copyEntityProperties(serverConfig, storedConfig); |
| 45 | 0 | storedConfig.setDataChangeLastModifiedBy(modifiedBy); |
| 46 | 0 | return serverConfigRepository.save(storedConfig); |
| 47 | |
} |
| 48 | |
|
| 49 | |
|
| 50 | |
} |
| 51 | |
|
| 52 | |
|
| 53 | |
} |