Skip to content

Commit

Permalink
feat: implementing saving and getting
Browse files Browse the repository at this point in the history
  • Loading branch information
Kammerlo committed Jan 15, 2025
1 parent 82086bb commit 9d242e3
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
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;
import lombok.Getter;
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
Expand All @@ -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<ChartEntity> charts;
}
Original file line number Diff line number Diff line change
@@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<DashboardEntity, String> {

List<DashboardEntity> findAllByOrganisationID(String organisationID);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -49,4 +52,10 @@ public ResponseEntity<Void> 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<List<DashboardView>> getDashboards(@PathVariable("organisationID") String organisationID) {
return ResponseEntity.ok(metricService.getAllDashboards(organisationID));
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChartView> charts;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
public interface MetricService {

Map<MetricEnum, List<MetricEnum.SubMetric>> getAvailableMetrics();
Map<MetricEnum, List<Object>> getData(Map<MetricEnum, List<MetricEnum.SubMetric>> metrics, String organisationID, Optional<LocalDate> startDate, Optional<LocalDate> endDate);

Map<MetricEnum, List<Object>> getData(Map<MetricEnum, List<MetricEnum.SubMetric>> metrics, String organisationID, Optional<LocalDate> startDate, Optional<LocalDate> endDate);

boolean saveDashboard(List<DashboardView> dashboards, String organisationID);
}

List<DashboardView> getAllDashboards(String organisationID);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +21,7 @@ public class MetricServiceImpl implements MetricService{

private final List<MetricExecutor> metricExecutors;
private final DashboardRepository dashboardRepository;
private final DashboardViewToEntity dashboardViewToEntity;
private final DashboardViewMapper dashboardViewMapper;

@Override
public Map<MetricEnum, List<MetricEnum.SubMetric>> getAvailableMetrics() {
Expand All @@ -46,13 +45,25 @@ public Map<MetricEnum, List<Object>> getData(Map<MetricEnum, List<MetricEnum.Sub
@Override
public boolean saveDashboard(List<DashboardView> dashboards, String organisationID) {
List<DashboardEntity> 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<DashboardEntity> dashboardEntities = dashboardRepository.saveAll(dashboardsEntities);
return dashboardEntities.size() == dashboards.size();
}

@Override
public List<DashboardView> getAllDashboards(String organisationID) {
List<DashboardEntity> 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 9d242e3

Please sign in to comment.