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:
		@@ -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;
 | 
			
		||||
@@ -24,54 +27,60 @@ 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
 | 
			
		||||
@@ -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")
 | 
			
		||||
 
 | 
			
		||||
@@ -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) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user