feat: Add error handling for dataset endpoints & return dataset that is beeing returend for the abilty to get the id
This commit is contained in:
parent
2ef03b07e0
commit
0ca9579949
@ -9,8 +9,11 @@ import de.uni_passau.fim.PADAS.group3.DataDash.model.Category;
|
|||||||
import de.uni_passau.fim.PADAS.group3.DataDash.model.Dataset;
|
import de.uni_passau.fim.PADAS.group3.DataDash.model.Dataset;
|
||||||
import de.uni_passau.fim.PADAS.group3.DataDash.model.DatasetService;
|
import de.uni_passau.fim.PADAS.group3.DataDash.model.DatasetService;
|
||||||
import de.uni_passau.fim.PADAS.group3.DataDash.model.Type;
|
import de.uni_passau.fim.PADAS.group3.DataDash.model.Type;
|
||||||
|
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.web.config.EnableSpringDataWebSupport;
|
import org.springframework.data.web.config.EnableSpringDataWebSupport;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
@ -24,54 +27,60 @@ public class DatasetController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private DatasetService datasetService;
|
private DatasetService datasetService;
|
||||||
|
|
||||||
// @GetMapping
|
|
||||||
// public List<Dataset> getAllDatasets() {
|
|
||||||
// return datasetService.getAllDatasets();
|
|
||||||
// }
|
|
||||||
|
|
||||||
@GetMapping("/id/{id}")
|
@GetMapping("/id/{id}")
|
||||||
public Dataset getDatasetById(@PathVariable("id") UUID id) {
|
public ResponseEntity<?> getDatasetById(@PathVariable("id") UUID id) {
|
||||||
return datasetService.getDatasetById(id);
|
Dataset d = datasetService.getDatasetById(id);
|
||||||
|
if (d == null) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
return new ResponseEntity<>(d , HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public Dataset createDataset(@RequestBody Dataset dataset) {
|
public Dataset createDataset(@RequestBody Dataset dataset) {
|
||||||
datasetService.addDataset(dataset);
|
return datasetService.addDataset(dataset);
|
||||||
// TODO: figure out what the fuck i need to do here
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @PutMapping("/{id}")
|
|
||||||
// public Dataset updateDataset(@PathVariable("id") Long id, @RequestBody
|
|
||||||
// Dataset dataset) {
|
|
||||||
// return datasetService.updateDataset(id, dataset);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
|
|
||||||
@DeleteMapping("/id/{id}")
|
@DeleteMapping("/id/{id}")
|
||||||
public void deleteDataset(@PathVariable("id") UUID id) {
|
public ResponseEntity<?> deleteDataset(@PathVariable("id") UUID id) {
|
||||||
|
if (datasetService.getDatasetById(id) == null) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
datasetService.deleteDataset(id);
|
datasetService.deleteDataset(id);
|
||||||
|
return new ResponseEntity<>(HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/id/{id}/upvote")
|
@PutMapping("/id/{id}/upvote")
|
||||||
public Dataset upvote(@PathVariable("id") UUID id) {
|
public ResponseEntity<?> upvote(@PathVariable("id") UUID id) {
|
||||||
|
if (datasetService.getDatasetById(id) == null) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
datasetService.upvoteDataset(id);
|
datasetService.upvoteDataset(id);
|
||||||
return datasetService.getDatasetById(id);
|
return new ResponseEntity<>(datasetService.getDatasetById(id), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/id/{id}/downvote")
|
@PutMapping("/id/{id}/downvote")
|
||||||
public Dataset downvote(@PathVariable("id") UUID id) {
|
public ResponseEntity<?> downvote(@PathVariable("id") UUID id) {
|
||||||
|
if (datasetService.getDatasetById(id) == null) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
datasetService.downvoteDataset(id);
|
datasetService.downvoteDataset(id);
|
||||||
return getDatasetById(id); // new ResponseEntity<>(null, HttpStatus.OK);
|
return new ResponseEntity<>(getDatasetById(id), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/id/{id}/vote")
|
@PutMapping("/id/{id}/vote")
|
||||||
public Dataset postMethodName(@PathVariable("id") UUID id,
|
public ResponseEntity<?> postMethodName(@PathVariable("id") UUID id,
|
||||||
@RequestParam("stars") int stars) {
|
@RequestParam("stars") int stars) {
|
||||||
if (stars > 0 && stars < 6) {
|
if (datasetService.getDatasetById(id) == null) {
|
||||||
datasetService.voteDataset(id, stars);
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
return datasetService.getDatasetById(id);
|
if (!(stars > 0 && stars < 6)) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
datasetService.voteDataset(id, stars);
|
||||||
|
return new ResponseEntity<>(datasetService.getDatasetById(id), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
@ -88,7 +97,8 @@ public class DatasetController {
|
|||||||
@RequestParam(value = "category", required = false) Category category) {
|
@RequestParam(value = "category", required = false) Category category) {
|
||||||
Pageable pageable = PageRequest.of(page, size,
|
Pageable pageable = PageRequest.of(page, size,
|
||||||
Sort.by(direction.equals("asc") ? Sort.Direction.ASC : Sort.Direction.DESC, sort));
|
Sort.by(direction.equals("asc") ? Sort.Direction.ASC : Sort.Direction.DESC, sort));
|
||||||
return datasetService.getDatasetsByOptionalCriteria(title, description, author, abst, type, rating, category,pageable);
|
return datasetService.getDatasetsByOptionalCriteria(title, description, author, abst, type, rating, category,
|
||||||
|
pageable);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/search")
|
@GetMapping("/search")
|
||||||
|
@ -28,9 +28,9 @@ public class DatasetService {
|
|||||||
return datasetRepository.getDatasetById(id);
|
return datasetRepository.getDatasetById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addDataset(Dataset dataset) {
|
public Dataset addDataset(Dataset dataset) {
|
||||||
dataset.setDate(LocalDate.now());
|
dataset.setDate(LocalDate.now());
|
||||||
datasetRepository.save(dataset);
|
return datasetRepository.save(dataset);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateDatasetTitle(UUID id, String title) {
|
public void updateDatasetTitle(UUID id, String title) {
|
||||||
|
Loading…
Reference in New Issue
Block a user