|
| 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.annotation.RequirePermission; |
| 21 | +import io.github.malonetalk.enums.PermissionEnum; |
| 22 | +import io.github.malonetalk.model.dto.CsvImportConfirmDto; |
| 23 | +import io.github.malonetalk.model.dto.CsvParseOptions; |
| 24 | +import io.github.malonetalk.model.vo.BooleanVo; |
| 25 | +import io.github.malonetalk.model.vo.CsvImportPageVo; |
| 26 | +import io.github.malonetalk.model.vo.CsvImportVo; |
| 27 | +import io.github.malonetalk.service.csv.CsvImportService; |
| 28 | +import jakarta.validation.Valid; |
| 29 | +import lombok.RequiredArgsConstructor; |
| 30 | +import org.springframework.http.HttpStatus; |
| 31 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 32 | +import org.springframework.web.bind.annotation.GetMapping; |
| 33 | +import org.springframework.web.bind.annotation.PathVariable; |
| 34 | +import org.springframework.web.bind.annotation.PostMapping; |
| 35 | +import org.springframework.web.bind.annotation.PutMapping; |
| 36 | +import org.springframework.web.bind.annotation.RequestBody; |
| 37 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 38 | +import org.springframework.web.bind.annotation.RequestParam; |
| 39 | +import org.springframework.web.bind.annotation.ResponseStatus; |
| 40 | +import org.springframework.web.bind.annotation.RestController; |
| 41 | +import org.springframework.web.multipart.MultipartFile; |
| 42 | + |
| 43 | +@RestController |
| 44 | +@RequestMapping("/api/csv-imports") |
| 45 | +@RequiredArgsConstructor |
| 46 | +public class CsvImportController { |
| 47 | + private final CsvImportService service; |
| 48 | + |
| 49 | + @PostMapping("/preview") |
| 50 | + @RequirePermission(PermissionEnum.DATA_CSV_IMPORT) |
| 51 | + public CsvImportVo preview( |
| 52 | + @RequestParam MultipartFile file, |
| 53 | + @RequestParam(defaultValue = "UTF-8") String encoding, |
| 54 | + @RequestParam(defaultValue = ",") String delimiter, |
| 55 | + @RequestParam(defaultValue = "true") boolean header, |
| 56 | + @RequestParam(defaultValue = "true") boolean emptyAsNull) |
| 57 | + throws Exception { |
| 58 | + return service.preview( |
| 59 | + file, |
| 60 | + CsvParseOptions.builder() |
| 61 | + .encoding(encoding) |
| 62 | + .delimiter(delimiter) |
| 63 | + .header(header) |
| 64 | + .emptyAsNull(emptyAsNull) |
| 65 | + .build()); |
| 66 | + } |
| 67 | + |
| 68 | + @PutMapping("/{id}/preview") |
| 69 | + @RequirePermission(PermissionEnum.DATA_CSV_IMPORT) |
| 70 | + public CsvImportVo repreview( |
| 71 | + @PathVariable String id, @Valid @RequestBody CsvParseOptions options) throws Exception { |
| 72 | + return service.repreview(id, options); |
| 73 | + } |
| 74 | + |
| 75 | + @PostMapping("/{id}/confirm") |
| 76 | + @ResponseStatus(HttpStatus.ACCEPTED) |
| 77 | + @RequirePermission({PermissionEnum.DATA_CSV_IMPORT, PermissionEnum.SEMANTIC_TABLE_SYNC}) |
| 78 | + public CsvImportVo confirm( |
| 79 | + @PathVariable String id, @Valid @RequestBody CsvImportConfirmDto request) { |
| 80 | + return service.confirm(id, request); |
| 81 | + } |
| 82 | + |
| 83 | + @GetMapping("/{id}") |
| 84 | + @RequirePermission( |
| 85 | + value = { |
| 86 | + PermissionEnum.DATA_CSV_IMPORT, |
| 87 | + PermissionEnum.DATA_CSV_DELETE, |
| 88 | + PermissionEnum.DATA_DATASOURCE_READ |
| 89 | + }, |
| 90 | + anyOf = true) |
| 91 | + public CsvImportVo get(@PathVariable String id) { |
| 92 | + return service.get(id); |
| 93 | + } |
| 94 | + |
| 95 | + @GetMapping |
| 96 | + @RequirePermission( |
| 97 | + value = { |
| 98 | + PermissionEnum.DATA_CSV_IMPORT, |
| 99 | + PermissionEnum.DATA_CSV_DELETE, |
| 100 | + PermissionEnum.DATA_DATASOURCE_READ |
| 101 | + }, |
| 102 | + anyOf = true) |
| 103 | + public CsvImportPageVo list( |
| 104 | + @RequestParam(defaultValue = "1") int page, |
| 105 | + @RequestParam(defaultValue = "10") int pageSize) { |
| 106 | + return service.list(page, pageSize); |
| 107 | + } |
| 108 | + |
| 109 | + @PostMapping("/{id}/sync") |
| 110 | + @ResponseStatus(HttpStatus.ACCEPTED) |
| 111 | + @RequirePermission({PermissionEnum.DATA_CSV_IMPORT, PermissionEnum.SEMANTIC_TABLE_SYNC}) |
| 112 | + public BooleanVo sync(@PathVariable String id) { |
| 113 | + service.retrySync(id); |
| 114 | + return BooleanVo.TRUE; |
| 115 | + } |
| 116 | + |
| 117 | + @DeleteMapping("/{id}") |
| 118 | + @RequirePermission(PermissionEnum.DATA_CSV_DELETE) |
| 119 | + public BooleanVo delete(@PathVariable String id) throws Exception { |
| 120 | + service.delete(id); |
| 121 | + return BooleanVo.TRUE; |
| 122 | + } |
| 123 | +} |
0 commit comments