moremind commented on code in PR #5602: URL: https://github.com/apache/shenyu/pull/5602#discussion_r1720784481
########## shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ScalePolicyController.java: ########## @@ -0,0 +1,89 @@ +/* + * 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.shenyu.admin.controller; + +import jakarta.validation.Valid; +import org.apache.shenyu.admin.aspect.annotation.RestApi; +import org.apache.shenyu.admin.mapper.ScalePolicyMapper; +import org.apache.shenyu.admin.model.dto.ScalePolicyDTO; +import org.apache.shenyu.admin.model.result.ShenyuAdminResult; +import org.apache.shenyu.admin.model.vo.ScalePolicyVO; +import org.apache.shenyu.admin.service.ScalePolicyService; +import org.apache.shenyu.admin.utils.ShenyuResultMessage; +import org.apache.shenyu.admin.validation.annotation.Existed; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; + +import java.util.Optional; + +/** + * this is ScalePolicy controller. + */ +@RestApi("/scale/policy") +public class ScalePolicyController { + + private final ScalePolicyService scalePolicyService; + + public ScalePolicyController(final ScalePolicyService scalePolicyService) { + this.scalePolicyService = scalePolicyService; + } + + /** + * get all policies. + * + * @return ShenyuAdminResult + */ + @GetMapping("/getAllPolicies") + public ShenyuAdminResult selectAll() { + return ShenyuAdminResult.success(ShenyuResultMessage.QUERY_SUCCESS, scalePolicyService.selectAll()); + } + + /** + * detail scale policy. + * + * @param id primary key + * @return ShenyuAdminResult + */ + @GetMapping("/{id}") + public ShenyuAdminResult detailPolicy(@PathVariable("id") @Valid + @Existed(provider = ScalePolicyMapper.class, + message = "scale policy is not existed") final String id) { + ScalePolicyVO scalePolicyVO = scalePolicyService.findById(id); + return Optional.ofNullable(scalePolicyVO) + .map(item -> ShenyuAdminResult.success(ShenyuResultMessage.DETAIL_SUCCESS, item)) + .orElse(ShenyuAdminResult.error(ShenyuResultMessage.DETAIL_FAILED)); + } + + /** + * update scale policy. + * + * @param id primary key + * @param scalePolicyDTO scale policy info + * @return ShenyuAdminResult + */ + @PutMapping("/{id}") + public ShenyuAdminResult updateScalePolicy(@PathVariable("id") @Valid + @Existed(provider = ScalePolicyMapper.class, Review Comment: `@valid` should put on another line ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ScalePolicyController.java: ########## @@ -0,0 +1,89 @@ +/* + * 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.shenyu.admin.controller; + +import jakarta.validation.Valid; +import org.apache.shenyu.admin.aspect.annotation.RestApi; +import org.apache.shenyu.admin.mapper.ScalePolicyMapper; +import org.apache.shenyu.admin.model.dto.ScalePolicyDTO; +import org.apache.shenyu.admin.model.result.ShenyuAdminResult; +import org.apache.shenyu.admin.model.vo.ScalePolicyVO; +import org.apache.shenyu.admin.service.ScalePolicyService; +import org.apache.shenyu.admin.utils.ShenyuResultMessage; +import org.apache.shenyu.admin.validation.annotation.Existed; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; + +import java.util.Optional; + +/** + * this is ScalePolicy controller. + */ +@RestApi("/scale/policy") +public class ScalePolicyController { + + private final ScalePolicyService scalePolicyService; + + public ScalePolicyController(final ScalePolicyService scalePolicyService) { + this.scalePolicyService = scalePolicyService; + } + + /** + * get all policies. + * + * @return ShenyuAdminResult + */ + @GetMapping("/getAllPolicies") + public ShenyuAdminResult selectAll() { + return ShenyuAdminResult.success(ShenyuResultMessage.QUERY_SUCCESS, scalePolicyService.selectAll()); + } + + /** + * detail scale policy. + * + * @param id primary key + * @return ShenyuAdminResult + */ + @GetMapping("/{id}") + public ShenyuAdminResult detailPolicy(@PathVariable("id") @Valid + @Existed(provider = ScalePolicyMapper.class, + message = "scale policy is not existed") final String id) { + ScalePolicyVO scalePolicyVO = scalePolicyService.findById(id); + return Optional.ofNullable(scalePolicyVO) + .map(item -> ShenyuAdminResult.success(ShenyuResultMessage.DETAIL_SUCCESS, item)) + .orElse(ShenyuAdminResult.error(ShenyuResultMessage.DETAIL_FAILED)); + } + + /** + * update scale policy. + * + * @param id primary key + * @param scalePolicyDTO scale policy info + * @return ShenyuAdminResult + */ + @PutMapping("/{id}") + public ShenyuAdminResult updateScalePolicy(@PathVariable("id") @Valid + @Existed(provider = ScalePolicyMapper.class, + message = "scale policy is not existed") final String id, + @Valid @RequestBody final ScalePolicyDTO scalePolicyDTO) { + scalePolicyDTO.setId(id); Review Comment: the id should in scalePolicyDTO ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ScalePolicyController.java: ########## @@ -0,0 +1,89 @@ +/* + * 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.shenyu.admin.controller; + +import jakarta.validation.Valid; +import org.apache.shenyu.admin.aspect.annotation.RestApi; +import org.apache.shenyu.admin.mapper.ScalePolicyMapper; +import org.apache.shenyu.admin.model.dto.ScalePolicyDTO; +import org.apache.shenyu.admin.model.result.ShenyuAdminResult; +import org.apache.shenyu.admin.model.vo.ScalePolicyVO; +import org.apache.shenyu.admin.service.ScalePolicyService; +import org.apache.shenyu.admin.utils.ShenyuResultMessage; +import org.apache.shenyu.admin.validation.annotation.Existed; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; + +import java.util.Optional; + +/** + * this is ScalePolicy controller. + */ +@RestApi("/scale/policy") +public class ScalePolicyController { + + private final ScalePolicyService scalePolicyService; + + public ScalePolicyController(final ScalePolicyService scalePolicyService) { + this.scalePolicyService = scalePolicyService; + } + + /** + * get all policies. + * + * @return ShenyuAdminResult + */ + @GetMapping("/getAllPolicies") + public ShenyuAdminResult selectAll() { + return ShenyuAdminResult.success(ShenyuResultMessage.QUERY_SUCCESS, scalePolicyService.selectAll()); + } + + /** + * detail scale policy. + * + * @param id primary key + * @return ShenyuAdminResult + */ + @GetMapping("/{id}") + public ShenyuAdminResult detailPolicy(@PathVariable("id") @Valid + @Existed(provider = ScalePolicyMapper.class, + message = "scale policy is not existed") final String id) { + ScalePolicyVO scalePolicyVO = scalePolicyService.findById(id); + return Optional.ofNullable(scalePolicyVO) + .map(item -> ShenyuAdminResult.success(ShenyuResultMessage.DETAIL_SUCCESS, item)) + .orElse(ShenyuAdminResult.error(ShenyuResultMessage.DETAIL_FAILED)); + } + + /** + * update scale policy. + * + * @param id primary key + * @param scalePolicyDTO scale policy info + * @return ShenyuAdminResult + */ + @PutMapping("/{id}") + public ShenyuAdminResult updateScalePolicy(@PathVariable("id") @Valid + @Existed(provider = ScalePolicyMapper.class, + message = "scale policy is not existed") final String id, + @Valid @RequestBody final ScalePolicyDTO scalePolicyDTO) { + scalePolicyDTO.setId(id); Review Comment: you can check in pathvaraiable and body ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/model/dto/ScalePolicyDTO.java: ########## @@ -0,0 +1,323 @@ +/* + * 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.shenyu.admin.model.dto; + +import jakarta.validation.constraints.NotNull; +import org.apache.shenyu.admin.mapper.ScalePolicyMapper; +import org.apache.shenyu.admin.validation.annotation.Existed; + +import java.io.Serializable; +import java.util.Objects; + +/** + * this is scale policy from by web front. + */ +public class ScalePolicyDTO implements Serializable { + + private static final long serialVersionUID = 3319087894871820638L; + + + /** + * primary key id. + */ + @Existed(provider = ScalePolicyMapper.class, nullOfIgnore = true, message = "scale policy is not existed") + private String id; + + /** + * sort. + */ + @NotNull + private Integer sort; + + /** + * status 1:enable 0:disable. + */ + @NotNull + private Integer status; + + /** + * number of bootstrap. + */ + private Integer num; + + /** + * begin time. + */ + private String beginTime; + + /** + * end time. + */ + private String endTime; + + public ScalePolicyDTO() { + } + + public ScalePolicyDTO(final String id, + @NotNull final Integer sort, + @NotNull final Integer status, + final Integer num, + final String beginTime, + final String endTime) { + this.id = id; + this.sort = sort; + this.status = status; + this.num = num; + this.beginTime = beginTime; + this.endTime = endTime; + } + + /** + * Gets the value of id. + * + * @return the value of id + */ + public @Existed(provider = ScalePolicyMapper.class, nullOfIgnore = true, message = "scale policy is not existed") String getId() { + return id; + } + + /** + * Sets the id. + * + * @param id id + */ + public void setId(@Existed(provider = ScalePolicyMapper.class, nullOfIgnore = true, message = "scale policy is not existed") final String id) { + this.id = id; + } + + /** + * Gets the value of sort. + * + * @return the value of sort + */ + public @NotNull Integer getSort() { + return sort; + } + + /** + * Sets the sort. + * + * @param sort sort + */ + public void setSort(@NotNull final Integer sort) { + this.sort = sort; + } + + /** + * Gets the value of status. + * + * @return the value of status + */ + public @NotNull Integer getStatus() { + return status; + } + + /** + * Sets the status. + * + * @param status status + */ + public void setStatus(@NotNull final Integer status) { + this.status = status; + } + + /** + * Gets the value of num. + * + * @return the value of num + */ + public Integer getNum() { + return num; + } + + /** + * Sets the num. + * + * @param num num + */ + public void setNum(final Integer num) { + this.num = num; + } + + /** + * Gets the value of beginTime. + * + * @return the value of beginTime + */ + public String getBeginTime() { + return beginTime; + } + + /** + * Sets the beginTime. + * + * @param beginTime beginTime + */ + public void setBeginTime(final String beginTime) { + this.beginTime = beginTime; + } + + /** + * Gets the value of endTime. + * + * @return the value of endTime + */ + public String getEndTime() { + return endTime; + } + + /** + * Sets the endTime. + * + * @param endTime endTime + */ + public void setEndTime(final String endTime) { + this.endTime = endTime; + } + + /** + * builder method. + * + * @return builder object. + */ + public static ScalePolicyDTO.ScalePolicyDTOBuilder builder() { + return new ScalePolicyDTO.ScalePolicyDTOBuilder(); + } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ScalePolicyDTO that = (ScalePolicyDTO) o; + return Objects.equals(id, that.id) + && Objects.equals(sort, that.sort) + && Objects.equals(status, that.status) + && Objects.equals(num, that.num) + && Objects.equals(beginTime, that.beginTime) + && Objects.equals(endTime, that.endTime); + } + + @Override + public int hashCode() { + return Objects.hash(id, sort, status, num, beginTime, endTime); + } + + public static final class ScalePolicyDTOBuilder { + + private @Existed(provider = ScalePolicyMapper.class, nullOfIgnore = true, message = "scale policy is not existed") String id; Review Comment: ` @Existed(provider = ScalePolicyMapper.class, nullOfIgnore = true, message = "scale policy is not existed") private String id; ` ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ScaleRlueController.java: ########## @@ -0,0 +1,138 @@ +/* + * 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.shenyu.admin.controller; + +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.NotNull; +import org.apache.shenyu.admin.aspect.annotation.RestApi; +import org.apache.shenyu.admin.mapper.ScaleRuleMapper; +import org.apache.shenyu.admin.model.dto.ScaleRuleDTO; +import org.apache.shenyu.admin.model.page.CommonPager; +import org.apache.shenyu.admin.model.page.PageParameter; +import org.apache.shenyu.admin.model.query.ScaleRuleQuery; +import org.apache.shenyu.admin.model.result.ShenyuAdminResult; +import org.apache.shenyu.admin.model.vo.ScaleRuleVO; +import org.apache.shenyu.admin.service.ScaleRuleService; +import org.apache.shenyu.admin.utils.ShenyuResultMessage; +import org.apache.shenyu.admin.validation.annotation.Existed; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.DeleteMapping; + +import java.util.List; +import java.util.Optional; + +/** + * this is scale rule controller. + */ +@RestApi("/scale/rule") +public class ScaleRlueController { + + private final ScaleRuleService scaleRuleService; + + public ScaleRlueController(final ScaleRuleService scaleRuleService) { + this.scaleRuleService = scaleRuleService; + } + + /** + * get all rules. + * + * @return ShenyuAdminResult + */ + @GetMapping("/getAllRules") + public ShenyuAdminResult selectAll() { + return ShenyuAdminResult.success(ShenyuResultMessage.QUERY_SUCCESS, scaleRuleService.selectAll()); + } + + /** + * query scale rule. + * + * @param metricName metricName + * @param currentPage currentPage + * @param pageSize pageSize + * @return org.apache.shenyu.admin.model.result.ShenyuAdminResult + */ + @GetMapping("") + public ShenyuAdminResult queryRule(final String metricName, + @RequestParam @NotNull final Integer currentPage, + @RequestParam @NotNull final Integer pageSize) { + CommonPager<ScaleRuleVO> commonPager = + scaleRuleService.listByPage(new ScaleRuleQuery(metricName, new PageParameter(currentPage, pageSize))); + return ShenyuAdminResult.success(ShenyuResultMessage.QUERY_SUCCESS, commonPager); + } + + /** + * detail scale rule. + * + * @param id id + * @return ShenyuAdminResult + */ + @GetMapping("/{id}") + public ShenyuAdminResult detailRule(@PathVariable("id") @Valid Review Comment: `@valid` same question ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/model/dto/ScalePolicyDTO.java: ########## @@ -0,0 +1,323 @@ +/* + * 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.shenyu.admin.model.dto; + +import jakarta.validation.constraints.NotNull; +import org.apache.shenyu.admin.mapper.ScalePolicyMapper; +import org.apache.shenyu.admin.validation.annotation.Existed; + +import java.io.Serializable; +import java.util.Objects; + +/** + * this is scale policy from by web front. + */ +public class ScalePolicyDTO implements Serializable { + + private static final long serialVersionUID = 3319087894871820638L; + + + /** + * primary key id. + */ + @Existed(provider = ScalePolicyMapper.class, nullOfIgnore = true, message = "scale policy is not existed") + private String id; + + /** + * sort. + */ + @NotNull + private Integer sort; + + /** + * status 1:enable 0:disable. + */ + @NotNull + private Integer status; + + /** + * number of bootstrap. + */ + private Integer num; + + /** + * begin time. + */ + private String beginTime; + + /** + * end time. + */ + private String endTime; + + public ScalePolicyDTO() { + } + + public ScalePolicyDTO(final String id, + @NotNull final Integer sort, + @NotNull final Integer status, + final Integer num, + final String beginTime, + final String endTime) { + this.id = id; + this.sort = sort; + this.status = status; + this.num = num; + this.beginTime = beginTime; + this.endTime = endTime; + } + + /** + * Gets the value of id. + * + * @return the value of id + */ + public @Existed(provider = ScalePolicyMapper.class, nullOfIgnore = true, message = "scale policy is not existed") String getId() { + return id; + } + + /** + * Sets the id. + * + * @param id id + */ + public void setId(@Existed(provider = ScalePolicyMapper.class, nullOfIgnore = true, message = "scale policy is not existed") final String id) { + this.id = id; + } + + /** + * Gets the value of sort. + * + * @return the value of sort + */ + public @NotNull Integer getSort() { + return sort; + } + + /** + * Sets the sort. + * + * @param sort sort + */ + public void setSort(@NotNull final Integer sort) { + this.sort = sort; + } + + /** + * Gets the value of status. + * + * @return the value of status + */ + public @NotNull Integer getStatus() { + return status; + } + + /** + * Sets the status. + * + * @param status status + */ + public void setStatus(@NotNull final Integer status) { + this.status = status; + } + + /** + * Gets the value of num. + * + * @return the value of num + */ + public Integer getNum() { + return num; + } + + /** + * Sets the num. + * + * @param num num + */ + public void setNum(final Integer num) { + this.num = num; + } + + /** + * Gets the value of beginTime. + * + * @return the value of beginTime + */ + public String getBeginTime() { + return beginTime; + } + + /** + * Sets the beginTime. + * + * @param beginTime beginTime + */ + public void setBeginTime(final String beginTime) { + this.beginTime = beginTime; + } + + /** + * Gets the value of endTime. + * + * @return the value of endTime + */ + public String getEndTime() { + return endTime; + } + + /** + * Sets the endTime. + * + * @param endTime endTime + */ + public void setEndTime(final String endTime) { + this.endTime = endTime; + } + + /** + * builder method. + * + * @return builder object. + */ + public static ScalePolicyDTO.ScalePolicyDTOBuilder builder() { + return new ScalePolicyDTO.ScalePolicyDTOBuilder(); + } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ScalePolicyDTO that = (ScalePolicyDTO) o; + return Objects.equals(id, that.id) + && Objects.equals(sort, that.sort) + && Objects.equals(status, that.status) + && Objects.equals(num, that.num) + && Objects.equals(beginTime, that.beginTime) + && Objects.equals(endTime, that.endTime); + } + + @Override + public int hashCode() { + return Objects.hash(id, sort, status, num, beginTime, endTime); + } + + public static final class ScalePolicyDTOBuilder { + + private @Existed(provider = ScalePolicyMapper.class, nullOfIgnore = true, message = "scale policy is not existed") String id; Review Comment: the annotation location is ugly ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ScaleRlueController.java: ########## @@ -0,0 +1,138 @@ +/* + * 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.shenyu.admin.controller; + +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.NotNull; +import org.apache.shenyu.admin.aspect.annotation.RestApi; +import org.apache.shenyu.admin.mapper.ScaleRuleMapper; +import org.apache.shenyu.admin.model.dto.ScaleRuleDTO; +import org.apache.shenyu.admin.model.page.CommonPager; +import org.apache.shenyu.admin.model.page.PageParameter; +import org.apache.shenyu.admin.model.query.ScaleRuleQuery; +import org.apache.shenyu.admin.model.result.ShenyuAdminResult; +import org.apache.shenyu.admin.model.vo.ScaleRuleVO; +import org.apache.shenyu.admin.service.ScaleRuleService; +import org.apache.shenyu.admin.utils.ShenyuResultMessage; +import org.apache.shenyu.admin.validation.annotation.Existed; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.DeleteMapping; + +import java.util.List; +import java.util.Optional; + +/** + * this is scale rule controller. + */ +@RestApi("/scale/rule") +public class ScaleRlueController { + + private final ScaleRuleService scaleRuleService; + + public ScaleRlueController(final ScaleRuleService scaleRuleService) { + this.scaleRuleService = scaleRuleService; + } + + /** + * get all rules. + * + * @return ShenyuAdminResult + */ + @GetMapping("/getAllRules") + public ShenyuAdminResult selectAll() { + return ShenyuAdminResult.success(ShenyuResultMessage.QUERY_SUCCESS, scaleRuleService.selectAll()); + } + + /** + * query scale rule. + * + * @param metricName metricName + * @param currentPage currentPage + * @param pageSize pageSize + * @return org.apache.shenyu.admin.model.result.ShenyuAdminResult + */ + @GetMapping("") + public ShenyuAdminResult queryRule(final String metricName, + @RequestParam @NotNull final Integer currentPage, + @RequestParam @NotNull final Integer pageSize) { + CommonPager<ScaleRuleVO> commonPager = + scaleRuleService.listByPage(new ScaleRuleQuery(metricName, new PageParameter(currentPage, pageSize))); + return ShenyuAdminResult.success(ShenyuResultMessage.QUERY_SUCCESS, commonPager); + } + + /** + * detail scale rule. + * + * @param id id + * @return ShenyuAdminResult + */ + @GetMapping("/{id}") + public ShenyuAdminResult detailRule(@PathVariable("id") @Valid + @Existed(provider = ScaleRuleMapper.class, + message = "scale role is not existed") final String id) { + ScaleRuleVO scaleRuleVO = scaleRuleService.findById(id); + return Optional.ofNullable(scaleRuleVO) + .map(item -> ShenyuAdminResult.success(ShenyuResultMessage.DETAIL_SUCCESS, item)) + .orElse(ShenyuAdminResult.error(ShenyuResultMessage.DETAIL_FAILED)); + } + + /** + * create scale rule. + * + * @param scaleRuleDTO scaleRuleDTO + * @return ShenyuAdminResult + */ + @PostMapping("") + public ShenyuAdminResult createRule(@Valid @RequestBody final ScaleRuleDTO scaleRuleDTO) { + return ShenyuAdminResult.success(ShenyuResultMessage.CREATE_SUCCESS, scaleRuleService.createOrUpdate(scaleRuleDTO)); + } + + /** + * update rule. + * + * @param id id + * @param scaleRuleDTO scaleRuleDTO + * @return ShenyuAdminResult + */ + @PutMapping("/{id}") + public ShenyuAdminResult updateRule(@PathVariable("id") @Valid + @Existed(provider = ScaleRuleMapper.class, + message = "scale rule is not existed") final String id, + @Valid @RequestBody final ScaleRuleDTO scaleRuleDTO) { + scaleRuleDTO.setId(id); Review Comment: same question -- 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]
