diff --git a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/domain/entity/metric/ChartEntity.java b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/domain/entity/metric/ChartEntity.java new file mode 100644 index 00000000..7b85f339 --- /dev/null +++ b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/domain/entity/metric/ChartEntity.java @@ -0,0 +1,48 @@ +package org.cardanofoundation.lob.app.accounting_reporting_core.domain.entity.metric; + +import jakarta.persistence.Entity; +import jakarta.persistence.Enumerated; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.cardanofoundation.lob.app.accounting_reporting_core.domain.core.metric.MetricEnum; +import org.hibernate.annotations.JdbcType; +import org.hibernate.dialect.PostgreSQLEnumJdbcType; + +import static jakarta.persistence.EnumType.STRING; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@Builder +@Entity +@Table(name = "accounting_core_charts") +public class ChartEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + @ManyToOne + @JoinColumn(name = "dashboard_id", nullable = false) + private DashboardEntity dashboard; + private Double xPos; + private Double yPos; + private Double width; + private Double height; + @Enumerated(STRING) + @JdbcType(PostgreSQLEnumJdbcType.class) + private MetricEnum metric; + @Enumerated(STRING) + @JdbcType(PostgreSQLEnumJdbcType.class) + private MetricEnum.SubMetric subMetric; + +} diff --git a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/domain/entity/metric/DashboardEntity.java b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/domain/entity/metric/DashboardEntity.java index 524131df..f3d2e115 100644 --- a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/domain/entity/metric/DashboardEntity.java +++ b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/domain/entity/metric/DashboardEntity.java @@ -1,8 +1,15 @@ package org.cardanofoundation.lob.app.accounting_reporting_core.domain.entity.metric; +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; import jakarta.persistence.Table; import lombok.AllArgsConstructor; import lombok.Builder; @@ -10,8 +17,14 @@ import lombok.NoArgsConstructor; import org.cardanofoundation.lob.app.accounting_reporting_core.domain.core.Validable; import org.cardanofoundation.lob.app.accounting_reporting_core.domain.core.metric.MetricEnum; +import org.hibernate.annotations.JdbcType; +import org.hibernate.dialect.PostgreSQLEnumJdbcType; import org.springframework.data.domain.Persistable; +import java.util.List; + +import static jakarta.persistence.EnumType.STRING; + @Getter @NoArgsConstructor @AllArgsConstructor @@ -21,15 +34,12 @@ public class DashboardEntity{ @Id - @GeneratedValue - private int id; - + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + @Column(name = "organisation_id") + private String organisationID; private String name; private String description; - private Double xPos; - private Double yPos; - private Double width; - private Double height; - private MetricEnum metric; - private MetricEnum.SubMetric subMetric; + @OneToMany(mappedBy = "dashboard", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true) + private List charts; } diff --git a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/mapper/ChartViewMapper.java b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/mapper/ChartViewMapper.java new file mode 100644 index 00000000..5b8e7e56 --- /dev/null +++ b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/mapper/ChartViewMapper.java @@ -0,0 +1,15 @@ +package org.cardanofoundation.lob.app.accounting_reporting_core.mapper; + +import org.cardanofoundation.lob.app.accounting_reporting_core.domain.entity.metric.ChartEntity; +import org.cardanofoundation.lob.app.accounting_reporting_core.resource.views.metric.ChartView; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; + +@Mapper(componentModel = "spring") +public interface ChartViewMapper { + + @Mapping(target = "xPos", source = "chartView.XPos") + @Mapping(target = "yPos", source = "chartView.YPos") + ChartEntity toChartEntity(ChartView chartView); + +} diff --git a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/mapper/DashboardViewToEntity.java b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/mapper/DashboardViewMapper.java similarity index 60% rename from accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/mapper/DashboardViewToEntity.java rename to accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/mapper/DashboardViewMapper.java index 9e829bdf..c29e1871 100644 --- a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/mapper/DashboardViewToEntity.java +++ b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/mapper/DashboardViewMapper.java @@ -5,12 +5,14 @@ import org.mapstruct.Mapper; import org.mapstruct.Mapping; -@Mapper(componentModel = "spring") -public interface DashboardViewToEntity { +@Mapper(componentModel = "spring", uses = ChartViewMapper.class) +public interface DashboardViewMapper { @Mapping(target = "id", ignore = true) - @Mapping(target = "xPos", source = "dashboardView.XPos") - @Mapping(target = "yPos", source = "dashboardView.YPos") + @Mapping(target = "organisationID", source = "organisationID") + @Mapping(target = "charts", source = "dashboardView.charts") DashboardEntity mapToDashboardEntity(DashboardView dashboardView, String organisationID); + DashboardView mapToDashboardView(DashboardEntity dashboardEntity); + } diff --git a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/repository/DashboardRepository.java b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/repository/DashboardRepository.java index e94ff427..77f5bc96 100644 --- a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/repository/DashboardRepository.java +++ b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/repository/DashboardRepository.java @@ -3,5 +3,10 @@ import org.cardanofoundation.lob.app.accounting_reporting_core.domain.entity.metric.DashboardEntity; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + public interface DashboardRepository extends JpaRepository { + + List findAllByOrganisationID(String organisationID); + } diff --git a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/resource/MetricController.java b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/resource/MetricController.java index e881ad34..c7c4fa1c 100644 --- a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/resource/MetricController.java +++ b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/resource/MetricController.java @@ -6,16 +6,19 @@ import org.cardanofoundation.lob.app.accounting_reporting_core.resource.requests.metric.GetMetricDataRequest; import org.cardanofoundation.lob.app.accounting_reporting_core.resource.requests.metric.SaveDashboardRequest; import org.cardanofoundation.lob.app.accounting_reporting_core.resource.response.metric.MetricDataResponse; +import org.cardanofoundation.lob.app.accounting_reporting_core.resource.views.metric.DashboardView; import org.cardanofoundation.lob.app.accounting_reporting_core.resource.views.metric.MetricView; import org.cardanofoundation.lob.app.accounting_reporting_core.service.internal.metrics.MetricService; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; 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.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.List; import java.util.Optional; @RestController @@ -49,4 +52,10 @@ public ResponseEntity saveDashboard(@RequestBody SaveDashboardRequest save metricService.saveDashboard(saveDashboardRequest.getDashboards(), saveDashboardRequest.getOrganisationID()); return ResponseEntity.ok().build(); } + + @Tag(name = "Get Dashboards", description = "Get Dashboards") + @GetMapping(value = "/dashboards/{organisationID}", produces = "application/json") + public ResponseEntity> getDashboards(@PathVariable("organisationID") String organisationID) { + return ResponseEntity.ok(metricService.getAllDashboards(organisationID)); + } } diff --git a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/resource/views/metric/ChartView.java b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/resource/views/metric/ChartView.java new file mode 100644 index 00000000..f54d4642 --- /dev/null +++ b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/resource/views/metric/ChartView.java @@ -0,0 +1,21 @@ +package org.cardanofoundation.lob.app.accounting_reporting_core.resource.views.metric; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.cardanofoundation.lob.app.accounting_reporting_core.domain.core.metric.MetricEnum; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +public class ChartView { + + private Double xPos; + private Double yPos; + private Double width; + private Double height; + private MetricEnum metric; + private MetricEnum.SubMetric subMetric; +} diff --git a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/resource/views/metric/DashboardView.java b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/resource/views/metric/DashboardView.java index 11cdcaac..54623854 100644 --- a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/resource/views/metric/DashboardView.java +++ b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/resource/views/metric/DashboardView.java @@ -6,19 +6,16 @@ import lombok.Setter; import org.cardanofoundation.lob.app.accounting_reporting_core.domain.core.metric.MetricEnum; +import java.util.List; + @Getter @Setter @AllArgsConstructor @NoArgsConstructor public class DashboardView { + private Long id; private String name; private String description; - private String userID; - private Double xPos; - private Double yPos; - private Double width; - private Double height; - private MetricEnum metric; - private MetricEnum.SubMetric subMetric; + private List charts; } diff --git a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/metrics/MetricService.java b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/metrics/MetricService.java index 3e53188d..10b11b88 100644 --- a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/metrics/MetricService.java +++ b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/metrics/MetricService.java @@ -11,7 +11,11 @@ public interface MetricService { Map> getAvailableMetrics(); - Map> getData(Map> metrics, String organisationID, Optional startDate, Optional endDate); + + Map> getData(Map> metrics, String organisationID, Optional startDate, Optional endDate); boolean saveDashboard(List dashboards, String organisationID); -} + + List getAllDashboards(String organisationID); + +} \ No newline at end of file diff --git a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/metrics/MetricServiceImpl.java b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/metrics/MetricServiceImpl.java index e9c5a8ad..3c78d596 100644 --- a/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/metrics/MetricServiceImpl.java +++ b/accounting_reporting_core/src/main/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/metrics/MetricServiceImpl.java @@ -4,13 +4,12 @@ import org.cardanofoundation.lob.app.accounting_reporting_core.domain.core.metric.MetricEnum; import org.cardanofoundation.lob.app.accounting_reporting_core.domain.entity.metric.DashboardEntity; import org.cardanofoundation.lob.app.accounting_reporting_core.exception.MetricNotFoundException; -import org.cardanofoundation.lob.app.accounting_reporting_core.mapper.DashboardViewToEntity; +import org.cardanofoundation.lob.app.accounting_reporting_core.mapper.DashboardViewMapper; import org.cardanofoundation.lob.app.accounting_reporting_core.repository.DashboardRepository; import org.cardanofoundation.lob.app.accounting_reporting_core.resource.views.metric.DashboardView; import org.springframework.stereotype.Service; import java.time.LocalDate; -import java.time.LocalDateTime; import java.util.List; import java.util.Map; import java.util.Optional; @@ -22,7 +21,7 @@ public class MetricServiceImpl implements MetricService{ private final List metricExecutors; private final DashboardRepository dashboardRepository; - private final DashboardViewToEntity dashboardViewToEntity; + private final DashboardViewMapper dashboardViewMapper; @Override public Map> getAvailableMetrics() { @@ -46,13 +45,25 @@ public Map> getData(Map dashboards, String organisationID) { List dashboardsEntities = dashboards.stream() - .map(dashboardView -> dashboardViewToEntity.mapToDashboardEntity(dashboardView, organisationID)) + .map(dashboardView -> { + DashboardEntity dashboardEntity = dashboardViewMapper.mapToDashboardEntity(dashboardView, organisationID); + dashboardEntity.getCharts().forEach(chartEntity -> chartEntity.setDashboard(dashboardEntity)); + return dashboardEntity; + }) .toList(); List dashboardEntities = dashboardRepository.saveAll(dashboardsEntities); return dashboardEntities.size() == dashboards.size(); } + @Override + public List getAllDashboards(String organisationID) { + List allByOrganisationID = dashboardRepository.findAllByOrganisationID(organisationID); + return allByOrganisationID.stream() + .map(dashboardViewMapper::mapToDashboardView) + .toList(); + } + private MetricExecutor getMetricExecutor(MetricEnum metricName) { return metricExecutors.stream() .filter(metricExecutorInterface -> metricExecutorInterface.getName().equals(metricName)) diff --git a/accounting_reporting_core/src/main/resources/db/migration/postgresql/common/V4__adding_dashboard_saving.sql b/accounting_reporting_core/src/main/resources/db/migration/postgresql/common/V4__adding_dashboard_saving.sql index 7c0cdb9b..e1818f85 100644 --- a/accounting_reporting_core/src/main/resources/db/migration/postgresql/common/V4__adding_dashboard_saving.sql +++ b/accounting_reporting_core/src/main/resources/db/migration/postgresql/common/V4__adding_dashboard_saving.sql @@ -11,14 +11,19 @@ CREATE TYPE account_core_submetric_type AS ENUM ( ); CREATE TABLE IF NOT EXISTS accounting_core_dashboards ( - id BIGINT PRIMARY KEY, + id BIGSERIAL PRIMARY KEY, organisation_id VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, - description TEXT, - x_pos DOUBLE PRECISION, - y_pos DOUBLE PRECISION, - width DOUBLE PRECISION, - height DOUBLE PRECISION, - metric accounting_core_metric_type NOT NULL, - submetric account_core_submetric_type NOT NULL + description TEXT +); + +CREATE TABLE IF NOT EXISTS accounting_core_charts ( + id BIGSERIAL PRIMARY KEY, + dashboard_id BIGINT NOT NULL REFERENCES accounting_core_dashboards(id) ON DELETE CASCADE, + x_pos DOUBLE PRECISION, + y_pos DOUBLE PRECISION, + width DOUBLE PRECISION, + height DOUBLE PRECISION, + metric accounting_core_metric_type NOT NULL, + sub_metric account_core_submetric_type NOT NULL ); \ No newline at end of file diff --git a/accounting_reporting_core/src/test/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/metrics/MetricServiceTest.java b/accounting_reporting_core/src/test/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/metrics/MetricServiceTest.java index 25b0ef5f..06cddbeb 100644 --- a/accounting_reporting_core/src/test/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/metrics/MetricServiceTest.java +++ b/accounting_reporting_core/src/test/java/org/cardanofoundation/lob/app/accounting_reporting_core/service/internal/metrics/MetricServiceTest.java @@ -1,6 +1,8 @@ package org.cardanofoundation.lob.app.accounting_reporting_core.service.internal.metrics; import org.cardanofoundation.lob.app.accounting_reporting_core.domain.core.metric.MetricEnum; +import org.cardanofoundation.lob.app.accounting_reporting_core.mapper.DashboardViewMapper; +import org.cardanofoundation.lob.app.accounting_reporting_core.repository.DashboardRepository; import org.cardanofoundation.lob.app.accounting_reporting_core.service.internal.metrics.executors.BalanceSheetMetricService; import org.cardanofoundation.lob.app.accounting_reporting_core.service.internal.metrics.executors.IncomeStatementMetricService; import org.junit.jupiter.api.BeforeEach; @@ -26,10 +28,14 @@ class MetricServiceTest { @Mock IncomeStatementMetricService incomeStatementMetricService; + @Mock + DashboardRepository dashboardRepository; + @Mock + DashboardViewMapper dashboardViewMapper; @BeforeEach void setup() { - metricService = new MetricServiceImpl(List.of(balanceSheetMetricService, incomeStatementMetricService)); + metricService = new MetricServiceImpl(List.of(balanceSheetMetricService, incomeStatementMetricService), dashboardRepository, dashboardViewMapper); } @Test