macksonmu commented on code in PR #1831: URL: https://github.com/apache/incubator-streampark/pull/1831#discussion_r996316353
########## streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/system/controller/VariableController.java: ########## @@ -0,0 +1,118 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.streampark.console.system.controller; + +import org.apache.streampark.console.base.domain.RestRequest; +import org.apache.streampark.console.base.domain.RestResponse; +import org.apache.streampark.console.base.exception.ApiAlertException; +import org.apache.streampark.console.core.entity.Application; +import org.apache.streampark.console.core.service.CommonService; +import org.apache.streampark.console.system.entity.Variable; +import org.apache.streampark.console.system.service.VariableService; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import lombok.extern.slf4j.Slf4j; +import org.apache.shiro.authz.annotation.RequiresPermissions; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import javax.validation.Valid; +import javax.validation.constraints.NotBlank; + +import java.util.List; + +@Slf4j +@Validated +@RestController +@RequestMapping("variable") +public class VariableController { + + @Autowired + private CommonService commonService; + + @Autowired + private VariableService variableService; + + @PostMapping("list") + @RequiresPermissions("variable:view") + public RestResponse variableList(RestRequest restRequest, Variable variable) { + IPage<Variable> variableList = variableService.findVariables(variable, restRequest); + return RestResponse.success(variableList); + } + + @PostMapping("post") + @RequiresPermissions("variable:add") + public RestResponse addVariable(@Valid Variable variable) throws Exception { + boolean isExists = this.variableService.findByVariableCode(variable.getTeamId(), variable.getVariableCode()) != null; + if (isExists) { + throw new ApiAlertException("Sorry, the variable code already exists."); + } + variable.setCreator(commonService.getCurrentUser().getUserId()); + this.variableService.createVariable(variable); + return RestResponse.success(); + } + + @PutMapping("update") + @RequiresPermissions("variable:update") + public RestResponse updateVariable(@Valid Variable variable) throws Exception { + Variable findVariable = this.variableService.findByVariableCode(variable.getTeamId(), variable.getVariableCode()); + if (findVariable == null) { + throw new ApiAlertException("Sorry, the variable does not exist."); + } + if (findVariable.getId().longValue() != variable.getId().longValue()) { Review Comment: > If id is wrong, back-end should throw exception. > > We should getById, and check whether immutable field is changed. If yes, throw exception.If no, it's ok. BTW, we should use the old Object, and just change some fields that can be changed. > > You can take a look `TeamServiceImpl.updateTeam`. From my side, this is more reasonable. > > ``` > @Override > public void updateTeam(Team team) { > Team oldTeam = Optional.ofNullable(this.getById(team)) > .orElseThrow(() -> new IllegalArgumentException(String.format("Team id [id=%s] not found", team.getId()))); > AssertUtils.isTrue(oldTeam.getTeamName().equals(team.getTeamName()), "Team name cannot be changed."); > oldTeam.setDescription(team.getDescription()); > oldTeam.setModifyTime(new Date()); > updateById(oldTeam); > } > ``` Just communicated with Huajie, he said not to think so much. ^_^ -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
