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.DatasetService;
|
||||
import de.uni_passau.fim.PADAS.group3.DataDash.model.Type;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.web.config.EnableSpringDataWebSupport;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.data.domain.Sort;
|
||||
|
||||
import java.util.UUID;
|
||||
@ -23,59 +26,65 @@ import org.springframework.web.bind.annotation.RequestBody;
|
||||
public class DatasetController {
|
||||
@Autowired
|
||||
private DatasetService datasetService;
|
||||
|
||||
// @GetMapping
|
||||
// public List<Dataset> getAllDatasets() {
|
||||
// return datasetService.getAllDatasets();
|
||||
// }
|
||||
|
||||
|
||||
@GetMapping("/id/{id}")
|
||||
public Dataset getDatasetById(@PathVariable("id") UUID id) {
|
||||
return datasetService.getDatasetById(id);
|
||||
public ResponseEntity<?> getDatasetById(@PathVariable("id") UUID 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
|
||||
public Dataset createDataset(@RequestBody Dataset dataset) {
|
||||
datasetService.addDataset(dataset);
|
||||
// TODO: figure out what the fuck i need to do here
|
||||
return null;
|
||||
return datasetService.addDataset(dataset);
|
||||
}
|
||||
|
||||
// @PutMapping("/{id}")
|
||||
// public Dataset updateDataset(@PathVariable("id") Long id, @RequestBody
|
||||
// Dataset dataset) {
|
||||
// return datasetService.updateDataset(id, dataset);
|
||||
// }
|
||||
//
|
||||
|
||||
@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);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@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);
|
||||
return datasetService.getDatasetById(id);
|
||||
return new ResponseEntity<>(datasetService.getDatasetById(id), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@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);
|
||||
return getDatasetById(id); // new ResponseEntity<>(null, HttpStatus.OK);
|
||||
return new ResponseEntity<>(getDatasetById(id), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PutMapping("/id/{id}/vote")
|
||||
public Dataset postMethodName(@PathVariable("id") UUID id,
|
||||
public ResponseEntity<?> postMethodName(@PathVariable("id") UUID id,
|
||||
@RequestParam("stars") int stars) {
|
||||
if (stars > 0 && stars < 6) {
|
||||
datasetService.voteDataset(id, stars);
|
||||
if (datasetService.getDatasetById(id) == null) {
|
||||
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
|
||||
public Page<Dataset> getDatasetsByDateAfter(@RequestParam(value = "author", required = false) String author,
|
||||
public Page<Dataset> getDatasetsByDateAfter(@RequestParam(value = "author", required = false) String author,
|
||||
@RequestParam(value = "title", required = false) String title,
|
||||
@RequestParam(value = "description", required = false) String description,
|
||||
@RequestParam(value = "abst", required = false) String abst,
|
||||
@ -88,7 +97,8 @@ public class DatasetController {
|
||||
@RequestParam(value = "category", required = false) Category category) {
|
||||
Pageable pageable = PageRequest.of(page, size,
|
||||
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")
|
||||
@ -98,10 +108,10 @@ public class DatasetController {
|
||||
@RequestParam(value = "sort", required = false, defaultValue = "upvotes") String sort,
|
||||
@RequestParam(value = "direction", required = false, defaultValue = "desc") String direction,
|
||||
@RequestParam(value = "category", required = false, defaultValue = "%") String category,
|
||||
@RequestParam(value = "type", required = false, defaultValue = "%") String type){
|
||||
@RequestParam(value = "type", required = false, defaultValue = "%") String type) {
|
||||
Pageable pageable = PageRequest.of(page, size,
|
||||
Sort.by(direction.equals("asc") ? Sort.Direction.ASC : Sort.Direction.DESC, sort));
|
||||
return datasetService.searchByOptionalCriteria(search, category, type, pageable);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -28,9 +28,9 @@ public class DatasetService {
|
||||
return datasetRepository.getDatasetById(id);
|
||||
}
|
||||
|
||||
public void addDataset(Dataset dataset) {
|
||||
public Dataset addDataset(Dataset dataset) {
|
||||
dataset.setDate(LocalDate.now());
|
||||
datasetRepository.save(dataset);
|
||||
return datasetRepository.save(dataset);
|
||||
}
|
||||
|
||||
public void updateDatasetTitle(UUID id, String title) {
|
||||
|
Loading…
Reference in New Issue
Block a user