Skip to content

Commit

Permalink
Allow Non Persistent Data
Browse files Browse the repository at this point in the history
  • Loading branch information
sergioppires committed Feb 26, 2022
1 parent 9badb45 commit 1b77f07
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
import io.github.phillima.avisualizer.entity.AvisualizerEntity;
import io.github.phillima.avisualizer.model.AvisualizerModel;
import io.github.phillima.avisualizer.model.ErrorModel;
import io.github.phillima.avisualizer.model.SimpleResponse;
import io.github.phillima.avisualizer.service.AvisualizerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Objects;

@CrossOrigin
@RestController
Expand All @@ -25,6 +24,10 @@ public class Controller {
@RequestMapping("/data.json")
public ResponseEntity<AvisualizerEntity> returnAllData(@RequestParam(required = false, name = "project") String project) throws URISyntaxException, IOException {
AvisualizerEntity response = avisualizerService.getAllInformation(project);
if(Objects.isNull(response.getId())){
return ResponseEntity.badRequest().build();
}

return ResponseEntity.ok(response);
}

Expand All @@ -44,8 +47,16 @@ public String returnCV(@RequestParam(required = false, name = "project") String
}

@PostMapping("/data/save")
public ResponseEntity<AvisualizerEntity> saveData(@RequestBody AvisualizerModel model) {
AvisualizerEntity resp = avisualizerService.saveModel(model);
public ResponseEntity<AvisualizerEntity> saveData(
@RequestBody AvisualizerModel model,
@RequestParam(name = "persist", required = false, defaultValue = "true") boolean persist) {
AvisualizerEntity resp;
if(persist){
resp = avisualizerService.saveModel(model);
} else{
resp = avisualizerService.saveModelTemporary(model);
}

return ResponseEntity.ok(resp);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class AvisualizerEntity {
private String pv;
private String sv;

private LocalDateTime last_update;
private boolean persist;
private long consults;

private LocalDateTime last_update;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package io.github.phillima.avisualizer.service;

import com.github.phillima.asniffer.output.json.d3hierarchy.classview.JSONReportCV;
import com.github.phillima.asniffer.output.json.d3hierarchy.packageview.JSONReportPV;
import com.github.phillima.asniffer.output.json.d3hierarchy.systemview.JSONReportSV;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.github.phillima.avisualizer.entity.AvisualizerEntity;
import io.github.phillima.avisualizer.entity.ErrorEntity;
import io.github.phillima.avisualizer.model.AvisualizerModel;
Expand Down Expand Up @@ -42,8 +37,7 @@ public class AvisualizerService {

private static final String DEFAULT_NAME = "SpaceWeatherTSI";

private static final String NO_PARAM = "";

private static final int MAX_CONSULT = 2;

private AvisualizerModel currentModel = standardResponse();

Expand Down Expand Up @@ -139,6 +133,26 @@ public AvisualizerEntity saveModel(AvisualizerModel model){
entity.setPv(model.getPv());

entity.setHash(cvHash);
entity.setPersist(true);
entity.setConsults(0);

entity.setLast_update(LocalDateTime.now());

return this.repository.save(entity);
}

public AvisualizerEntity saveModelTemporary(AvisualizerModel model){
AvisualizerEntity entity = new AvisualizerEntity();
entity.setId(UUID.randomUUID().toString());
entity.setName(model.getName());

entity.setCv(model.getCv());
entity.setSv(model.getSv());
entity.setPv(model.getPv());

entity.setHash("");
entity.setPersist(false);
entity.setConsults(0);

entity.setLast_update(LocalDateTime.now());

Expand All @@ -148,6 +162,16 @@ public AvisualizerEntity saveModel(AvisualizerModel model){
public AvisualizerEntity getAllInformation(String projectID) {
Optional<AvisualizerEntity> response = this.repository.findById(projectID);
if(response.isPresent()){
Long consults = response.get().getConsults();
response.get().setConsults(consults+1);

if (!response.get().isPersist() && (consults+1) >= MAX_CONSULT){
this.repository.delete(response.get());
return response.get();
}

response.get().setLast_update(LocalDateTime.now());
this.repository.save(response.get());
return response.get();
}
return new AvisualizerEntity();
Expand Down

0 comments on commit 1b77f07

Please sign in to comment.