|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 github.com/MaloneTalk |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Affero General Public License as |
| 6 | + * published by the Free Software Foundation, either version 3 of the |
| 7 | + * License, or any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package io.github.malonetalk.controller; |
| 19 | + |
| 20 | +import io.github.malonetalk.common.Result; |
| 21 | +import io.github.malonetalk.dto.DomainCreateRequest; |
| 22 | +import io.github.malonetalk.dto.DomainPageQuery; |
| 23 | +import io.github.malonetalk.dto.DomainUpdateRequest; |
| 24 | +import io.github.malonetalk.dto.pagination.PageResponse; |
| 25 | +import io.github.malonetalk.entity.DomainInfo; |
| 26 | +import io.github.malonetalk.service.DomainService; |
| 27 | +import jakarta.validation.Valid; |
| 28 | +import lombok.RequiredArgsConstructor; |
| 29 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 30 | +import org.springframework.web.bind.annotation.GetMapping; |
| 31 | +import org.springframework.web.bind.annotation.PathVariable; |
| 32 | +import org.springframework.web.bind.annotation.PostMapping; |
| 33 | +import org.springframework.web.bind.annotation.PutMapping; |
| 34 | +import org.springframework.web.bind.annotation.RequestBody; |
| 35 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 36 | +import org.springframework.web.bind.annotation.RestController; |
| 37 | + |
| 38 | +@RestController |
| 39 | +@RequestMapping("/api/domains") |
| 40 | +@RequiredArgsConstructor |
| 41 | +public class DomainController { |
| 42 | + |
| 43 | + private final DomainService domainService; |
| 44 | + |
| 45 | + @GetMapping |
| 46 | + public Result<PageResponse<DomainInfo>> findDomains(@Valid DomainPageQuery query) { |
| 47 | + return Result.success(domainService.getDomainPage(query)); |
| 48 | + } |
| 49 | + |
| 50 | + @GetMapping("/{id}") |
| 51 | + public Result<DomainInfo> findById(@PathVariable Integer id) { |
| 52 | + if (id == null || id < 0) { |
| 53 | + return Result.error(400, "ID必须为非负数"); |
| 54 | + } |
| 55 | + DomainInfo domain = domainService.findById(id); |
| 56 | + if (domain == null) { |
| 57 | + return Result.error(404, "领域不存在"); |
| 58 | + } |
| 59 | + return Result.success(domain); |
| 60 | + } |
| 61 | + |
| 62 | + @PostMapping |
| 63 | + public Result<DomainInfo> create(@Valid @RequestBody DomainCreateRequest request) { |
| 64 | + return Result.success(domainService.create(request)); |
| 65 | + } |
| 66 | + |
| 67 | + @PutMapping("/{id}") |
| 68 | + public Result<DomainInfo> update( |
| 69 | + @PathVariable Integer id, @Valid @RequestBody DomainUpdateRequest request) { |
| 70 | + if (id == null || id < 0) { |
| 71 | + return Result.error(400, "ID必须为非负数"); |
| 72 | + } |
| 73 | + return Result.success(domainService.update(id, request)); |
| 74 | + } |
| 75 | + |
| 76 | + @DeleteMapping("/{id}") |
| 77 | + public Result<Boolean> delete(@PathVariable Integer id) { |
| 78 | + if (id == null || id < 0) { |
| 79 | + return Result.error(400, "ID必须为非负数"); |
| 80 | + } |
| 81 | + domainService.delete(id); |
| 82 | + return Result.success(true); |
| 83 | + } |
| 84 | +} |
0 commit comments