From f9287a03426ff44bf2c6b8a320435438b73bb581 Mon Sep 17 00:00:00 2001 From: qianmoQ Date: Mon, 25 Nov 2024 14:03:36 +0800 Subject: [PATCH] refactor(dashboard): refactor create --- configure/schema/2024.4.0/schema.sql | 9 ++ .../server/controller/BaseController.java | 25 ++++-- .../service/entity/DashboardEntity.java | 4 + .../datacap/service/entity/DataSetEntity.java | 19 +++++ .../datacap/service/entity/ReportEntity.java | 14 +++- .../service/service/DashboardService.java | 3 - .../service/impl/DashboardServiceImpl.java | 17 +++- .../service/impl/ExecuteServiceImpl.java | 12 +-- core/datacap-ui/src/model/base.ts | 1 + .../views/components/visual/VisualView.vue | 2 +- .../pages/admin/dashboard/DashboardInfo.vue | 6 +- .../dashboard/components/ChartContainer.vue | 7 +- .../dashboard/components/DashboardEditor.vue | 9 +- .../components/DashboardEditorV2.vue | 6 ++ .../dashboard/components/DashboardView.vue | 2 +- docs/docs/api/dashboard/save.md | 84 +++++++++++++++++++ docs/mkdocs.yml | 1 + 17 files changed, 184 insertions(+), 37 deletions(-) create mode 100644 configure/schema/2024.4.0/schema.sql create mode 100644 core/datacap-ui/src/views/pages/admin/dashboard/components/DashboardEditorV2.vue create mode 100644 docs/docs/api/dashboard/save.md diff --git a/configure/schema/2024.4.0/schema.sql b/configure/schema/2024.4.0/schema.sql new file mode 100644 index 00000000..9e208762 --- /dev/null +++ b/configure/schema/2024.4.0/schema.sql @@ -0,0 +1,9 @@ +USE +`datacap`; + +ALTER TABLE `datacap_dashboard` + ADD COLUMN `version` VARCHAR(100) DEFAULT NULL; + +UPDATE `datacap_dashboard` +SET `version` = '1.0' +WHERE `version` IS NULL; diff --git a/core/datacap-server/src/main/java/io/edurt/datacap/server/controller/BaseController.java b/core/datacap-server/src/main/java/io/edurt/datacap/server/controller/BaseController.java index 1f82cdd1..3dbd1f10 100644 --- a/core/datacap-server/src/main/java/io/edurt/datacap/server/controller/BaseController.java +++ b/core/datacap-server/src/main/java/io/edurt/datacap/server/controller/BaseController.java @@ -12,9 +12,8 @@ import org.springframework.web.bind.annotation.DeleteMapping; 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.PutMapping; import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import java.io.Serializable; @@ -42,14 +41,30 @@ public abstract class BaseController } /** - * Save changes + * Create new resource */ - @RequestMapping(method = {RequestMethod.POST, RequestMethod.PUT}) - public CommonResponse saveAndUpdate(@RequestBody T configure) + @PostMapping + @JsonView(value = {EntityView.UserView.class}) + public CommonResponse create(@RequestBody T configure) { return service.saveOrUpdate(repository, configure); } + /** + * Update existing resource + */ + @PutMapping + @JsonView(value = {EntityView.UserView.class}) + public CommonResponse update(@RequestBody T configure) + { + return repository.findByCode(configure.getCode()) + .map(entity -> { + configure.setId(entity.getId()); + return service.saveOrUpdate(repository, configure); + }) + .orElseGet(() -> CommonResponse.failure("Resource [ " + configure.getCode() + " ] not found")); + } + @Deprecated @DeleteMapping public CommonResponse delete(@RequestParam(value = "id") Long id) diff --git a/core/datacap-service/src/main/java/io/edurt/datacap/service/entity/DashboardEntity.java b/core/datacap-service/src/main/java/io/edurt/datacap/service/entity/DashboardEntity.java index fa96fb54..dd9d7aee 100644 --- a/core/datacap-service/src/main/java/io/edurt/datacap/service/entity/DashboardEntity.java +++ b/core/datacap-service/src/main/java/io/edurt/datacap/service/entity/DashboardEntity.java @@ -52,6 +52,10 @@ public class DashboardEntity @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private AvatarEntity avatar; + @Column(name = "version") + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) + private String version; + @ManyToOne @JoinTable(name = "datacap_dashboard_user_relation", joinColumns = @JoinColumn(name = "dashboard_id"), diff --git a/core/datacap-service/src/main/java/io/edurt/datacap/service/entity/DataSetEntity.java b/core/datacap-service/src/main/java/io/edurt/datacap/service/entity/DataSetEntity.java index 4cad3352..b028bef0 100644 --- a/core/datacap-service/src/main/java/io/edurt/datacap/service/entity/DataSetEntity.java +++ b/core/datacap-service/src/main/java/io/edurt/datacap/service/entity/DataSetEntity.java @@ -1,8 +1,10 @@ package io.edurt.datacap.service.entity; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonView; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import io.edurt.datacap.common.enums.DataSetState; +import io.edurt.datacap.common.view.EntityView; import io.edurt.datacap.service.converter.ListConverter; import io.edurt.datacap.service.enums.SyncMode; import lombok.AllArgsConstructor; @@ -38,50 +40,65 @@ public class DataSetEntity extends BaseEntity { @Column(name = "description") + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private String description; @Column(name = "query") + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private String query; @Column(name = "sync_mode") @Enumerated(EnumType.STRING) + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private SyncMode syncMode = SyncMode.MANUAL; @Column(name = "expression") + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private String expression; // only for TIMING @Column(name = "state") @Convert(converter = ListConverter.class) + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private List state; @Column(name = "message") + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private String message; @Column(name = "table_name") + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private String tableName; @Column(name = "code") + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private String code; @Column(name = "scheduler") + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private String scheduler; @Column(name = "executor") + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private String executor; @Column(name = "total_rows") + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private String totalRows; @Column(name = "total_size") + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private String totalSize; @Column(name = "life_cycle") + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private String lifeCycle; @Column(name = "life_cycle_column") + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private String lifeCycleColumn; @Column(name = "life_cycle_type") + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private String lifeCycleType; @ManyToOne @@ -89,6 +106,7 @@ public class DataSetEntity joinColumns = @JoinColumn(name = "dataset_id"), inverseJoinColumns = @JoinColumn(name = "source_id")) @JsonIgnoreProperties(value = {"user"}) + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private SourceEntity source; @ManyToOne @@ -96,6 +114,7 @@ public class DataSetEntity joinColumns = @JoinColumn(name = "dataset_id"), inverseJoinColumns = @JoinColumn(name = "user_id")) @JsonIgnoreProperties(value = {"roles", "thirdConfigure", "avatarConfigure"}) + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private UserEntity user; @Transient diff --git a/core/datacap-service/src/main/java/io/edurt/datacap/service/entity/ReportEntity.java b/core/datacap-service/src/main/java/io/edurt/datacap/service/entity/ReportEntity.java index 472c333a..eddaa0f2 100644 --- a/core/datacap-service/src/main/java/io/edurt/datacap/service/entity/ReportEntity.java +++ b/core/datacap-service/src/main/java/io/edurt/datacap/service/entity/ReportEntity.java @@ -1,7 +1,8 @@ package io.edurt.datacap.service.entity; -import com.fasterxml.jackson.annotation.JsonIncludeProperties; +import com.fasterxml.jackson.annotation.JsonView; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import io.edurt.datacap.common.view.EntityView; import io.edurt.datacap.service.enums.ReportType; import lombok.AllArgsConstructor; import lombok.Data; @@ -35,39 +36,44 @@ public class ReportEntity extends BaseEntity { @Column(name = "configure") + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private String configure; @Column(name = "realtime") + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private boolean realtime; @Column(name = "type") @Enumerated(EnumType.STRING) + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private ReportType type; @Column(name = "query") + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private String query; @Column(name = "description") + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private String description; @ManyToOne @JoinTable(name = "datacap_report_user_relation", joinColumns = @JoinColumn(name = "report_id"), inverseJoinColumns = @JoinColumn(name = "user_id")) - @JsonIncludeProperties(value = {"id", "name", "username", "code"}) + @JsonView(value = {EntityView.AdminView.class}) private UserEntity user; @ManyToOne @JoinTable(name = "datacap_report_source_relation", joinColumns = @JoinColumn(name = "report_id"), inverseJoinColumns = @JoinColumn(name = "source_id")) - @JsonIncludeProperties(value = {"id", "code", "name", "type"}) + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private SourceEntity source; @ManyToOne @JoinTable(name = "datacap_report_dataset_relation", joinColumns = @JoinColumn(name = "report_id"), inverseJoinColumns = @JoinColumn(name = "dataset_id")) - @JsonIncludeProperties(value = {"id", "code", "name", "query", "description"}) + @JsonView(value = {EntityView.UserView.class, EntityView.AdminView.class}) private DataSetEntity dataset; } diff --git a/core/datacap-service/src/main/java/io/edurt/datacap/service/service/DashboardService.java b/core/datacap-service/src/main/java/io/edurt/datacap/service/service/DashboardService.java index edad85fb..71f4a580 100644 --- a/core/datacap-service/src/main/java/io/edurt/datacap/service/service/DashboardService.java +++ b/core/datacap-service/src/main/java/io/edurt/datacap/service/service/DashboardService.java @@ -9,7 +9,4 @@ import org.springframework.data.repository.PagingAndSortingRepository; public interface DashboardService extends BaseService { - CommonResponse> getAll(FilterBody filter); - - CommonResponse saveOrUpdate(PagingAndSortingRepository repository, DashboardEntity configure); } diff --git a/core/datacap-service/src/main/java/io/edurt/datacap/service/service/impl/DashboardServiceImpl.java b/core/datacap-service/src/main/java/io/edurt/datacap/service/service/impl/DashboardServiceImpl.java index 546ec011..846ed590 100644 --- a/core/datacap-service/src/main/java/io/edurt/datacap/service/service/impl/DashboardServiceImpl.java +++ b/core/datacap-service/src/main/java/io/edurt/datacap/service/service/impl/DashboardServiceImpl.java @@ -5,11 +5,12 @@ import io.edurt.datacap.service.adapter.PageRequestAdapter; import io.edurt.datacap.service.body.FilterBody; import io.edurt.datacap.service.entity.DashboardEntity; import io.edurt.datacap.service.entity.PageEntity; +import io.edurt.datacap.service.repository.BaseRepository; import io.edurt.datacap.service.repository.DashboardRepository; +import io.edurt.datacap.service.repository.ReportRepository; import io.edurt.datacap.service.security.UserDetailsService; import io.edurt.datacap.service.service.DashboardService; import org.springframework.data.domain.Pageable; -import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.stereotype.Service; @Service @@ -17,23 +18,31 @@ public class DashboardServiceImpl implements DashboardService { private final DashboardRepository repository; + private final ReportRepository reportRepository; - public DashboardServiceImpl(DashboardRepository repository) + public DashboardServiceImpl(DashboardRepository repository, ReportRepository reportRepository) { this.repository = repository; + this.reportRepository = reportRepository; } @Override - public CommonResponse> getAll(FilterBody filter) + public CommonResponse> getAll(BaseRepository baseRepository, FilterBody filter) { Pageable pageable = PageRequestAdapter.of(filter); return CommonResponse.success(PageEntity.build(repository.findAllByUser(UserDetailsService.getUser(), pageable))); } @Override - public CommonResponse saveOrUpdate(PagingAndSortingRepository repository, DashboardEntity configure) + public CommonResponse saveOrUpdate(BaseRepository baseRepository, DashboardEntity configure) { configure.setUser(UserDetailsService.getUser()); + configure.getReports() + .forEach(entity -> reportRepository.findByCode(entity.getCode()) + .ifPresentOrElse( + report -> entity.setId(report.getId()), + () -> {throw new RuntimeException("Report [ " + entity.getCode() + " ] not found");} + )); return CommonResponse.success(repository.save(configure)); } } diff --git a/core/datacap-service/src/main/java/io/edurt/datacap/service/service/impl/ExecuteServiceImpl.java b/core/datacap-service/src/main/java/io/edurt/datacap/service/service/impl/ExecuteServiceImpl.java index d1f3dd45..a4b0b2c6 100644 --- a/core/datacap-service/src/main/java/io/edurt/datacap/service/service/impl/ExecuteServiceImpl.java +++ b/core/datacap-service/src/main/java/io/edurt/datacap/service/service/impl/ExecuteServiceImpl.java @@ -44,15 +44,9 @@ public class ExecuteServiceImpl @Override public CommonResponse execute(ExecuteEntity configure) { - try { - return sourceRepository.findById(Long.valueOf(configure.getName())) - .map(entity -> handleSourceEntity(entity, configure.getContent())) - .orElse(CommonResponse.failure(ServiceState.SOURCE_NOT_FOUND)); - } - catch (NumberFormatException e) { - log.error("Invalid source id: {}", configure.getName(), e); - return CommonResponse.failure(ServiceState.INVALID_PARAMETER, "Invalid source id"); - } + return sourceRepository.findByCode(configure.getName()) + .map(entity -> handleSourceEntity(entity, configure.getContent())) + .orElse(CommonResponse.failure(ServiceState.SOURCE_NOT_FOUND)); } /** diff --git a/core/datacap-ui/src/model/base.ts b/core/datacap-ui/src/model/base.ts index d4f7511e..91aef2fe 100644 --- a/core/datacap-ui/src/model/base.ts +++ b/core/datacap-ui/src/model/base.ts @@ -3,6 +3,7 @@ export interface BaseModel id?: number name?: string active?: boolean + code?: string createTime?: string updateTime?: string } \ No newline at end of file diff --git a/core/datacap-ui/src/views/components/visual/VisualView.vue b/core/datacap-ui/src/views/components/visual/VisualView.vue index 60fbf3a1..69c0bd5e 100644 --- a/core/datacap-ui/src/views/components/visual/VisualView.vue +++ b/core/datacap-ui/src/views/components/visual/VisualView.vue @@ -143,7 +143,7 @@ export default defineComponent({ default: () => '400px' }, original: { - type: Number + type: String } }, data() diff --git a/core/datacap-ui/src/views/pages/admin/dashboard/DashboardInfo.vue b/core/datacap-ui/src/views/pages/admin/dashboard/DashboardInfo.vue index b5c12a9b..c3f3f3f9 100644 --- a/core/datacap-ui/src/views/pages/admin/dashboard/DashboardInfo.vue +++ b/core/datacap-ui/src/views/pages/admin/dashboard/DashboardInfo.vue @@ -2,7 +2,7 @@
- +
@@ -21,7 +21,8 @@ export default defineComponent({ return { loading: false, saving: false, - dataInfo: null as DashboardModel | null + dataInfo: null as DashboardModel | null, + version: '2' } }, created() @@ -40,6 +41,7 @@ export default defineComponent({ .then(response => { if (response.status) { this.dataInfo = response.data + this.version = response.data.version } else { this.$Message.error({ diff --git a/core/datacap-ui/src/views/pages/admin/dashboard/components/ChartContainer.vue b/core/datacap-ui/src/views/pages/admin/dashboard/components/ChartContainer.vue index e05d31f4..33cf32d0 100644 --- a/core/datacap-ui/src/views/pages/admin/dashboard/components/ChartContainer.vue +++ b/core/datacap-ui/src/views/pages/admin/dashboard/components/ChartContainer.vue @@ -12,7 +12,7 @@ - +