Skip to content

Commit

Permalink
feat: added delete and update
Browse files Browse the repository at this point in the history
  • Loading branch information
Kammerlo committed Jan 16, 2025
1 parent 55ce5bd commit ef80875
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
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;
Expand All @@ -26,6 +27,7 @@
import static jakarta.persistence.EnumType.STRING;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface DashboardRepository extends JpaRepository<DashboardEntity, String> {
public interface DashboardRepository extends JpaRepository<DashboardEntity, Long> {

List<DashboardEntity> findAllByOrganisationID(String organisationID);

Optional<DashboardEntity> findByIdAndAndOrganisationID(Long id, String organisationID);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import lombok.extern.slf4j.Slf4j;
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.requests.metric.UpdateDashboardRequest;
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.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
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;
Expand All @@ -22,6 +24,8 @@
import java.util.List;
import java.util.Optional;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.name;

@RestController
@RequestMapping("/api/metrics")
@CrossOrigin(origins = "http://localhost:3000")
Expand All @@ -47,17 +51,37 @@ public ResponseEntity<MetricDataResponse> getDashboardData(@RequestBody GetMetri
Optional.ofNullable(getMetricDataRequest.getEndDate()))));
}

@Tag(name = "Save Dashboards", description = "Save Dashboards")
@Tag(name = "Dashboards", description = "Save Dashboards")
@PostMapping(value = "/saveDashboard", produces = "application/json")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<Void> saveDashboard(@RequestBody SaveDashboardRequest saveDashboardRequest) {
metricService.saveDashboard(saveDashboardRequest.getDashboards(), saveDashboardRequest.getOrganisationID());
return ResponseEntity.ok().build();
// @PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<Boolean> saveDashboard(@RequestBody SaveDashboardRequest saveDashboardRequest) {
boolean success = metricService.saveDashboard(saveDashboardRequest.getDashboards(), saveDashboardRequest.getOrganisationID());
if (success) {
return ResponseEntity.ok().build();
} else {
return ResponseEntity.badRequest().build();
}
}

@Tag(name = "Get Dashboards", description = "Get Dashboards")
@Tag(name = "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));
}

@Tag(name = "Dashboards", description = "Delete Dashboards")
@DeleteMapping(value = "/deleteDashboard/{organisationID}/{dashboardID}", produces = "application/json")
// @PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<Void> deleteDashboard(@PathVariable("organisationID") String organisationID, @PathVariable("dashboardID") Long dashboardID) {
metricService.deleteDashboard(organisationID, dashboardID);
return ResponseEntity.ok().build();
}

@Tag(name = "Dashboards", description = "Update Dashboards")
@PostMapping(value = "/updateDashboard", produces = "application/json")
// @PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<Boolean> updateDashboard(@RequestBody UpdateDashboardRequest updateDashboardRequest) {
metricService.updateDashboard(updateDashboardRequest.getDashboard(), updateDashboardRequest.getOrganisationID());
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.cardanofoundation.lob.app.accounting_reporting_core.resource.requests.metric;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.views.metric.DashboardView;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class UpdateDashboardRequest {

DashboardView dashboard;
String organisationID;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ public interface MetricService {

List<DashboardView> getAllDashboards(String organisationID);

void deleteDashboard(String organisationID, Long dashboardID);

void updateDashboard(DashboardView dashboard, String organisationID);
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,23 @@ private MetricExecutor getMetricExecutor(MetricEnum metricName) {
.orElseThrow(() -> new MetricNotFoundException(String.format("Metric %s not found", metricName)));
}

@Override
public void deleteDashboard(String organisationID, Long dashboardID) {
DashboardEntity dashboardEntity = dashboardRepository.findByIdAndAndOrganisationID(dashboardID, organisationID)
.orElseThrow(() -> new MetricNotFoundException(String.format("Dashboard %s not found", dashboardID)));
dashboardRepository.delete(dashboardEntity);
}

@Override
public void updateDashboard(DashboardView dashboard, String organisationID) {
DashboardEntity dashboardEntity = dashboardRepository.findByIdAndAndOrganisationID(dashboard.getId(), organisationID)
.orElseThrow(() -> new MetricNotFoundException(String.format("Dashboard %s not found", dashboard.getId())));

DashboardEntity updatedEntity = dashboardViewMapper.mapToDashboardEntity(dashboard, organisationID);
updatedEntity.getCharts().forEach(chartEntity -> chartEntity.setDashboard(updatedEntity));
updatedEntity.setId(dashboardEntity.getId());
dashboardRepository.save(updatedEntity);
}


}

0 comments on commit ef80875

Please sign in to comment.