refactor: fix internal server errror on bad request
This commit is contained in:
parent
027528909d
commit
cdeb4fc2be
@ -13,6 +13,7 @@ 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.HttpStatus;
|
||||||
|
import org.springframework.http.HttpStatusCode;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
|
|
||||||
@ -26,14 +27,14 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||||||
public class DatasetController {
|
public class DatasetController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private DatasetService datasetService;
|
private DatasetService datasetService;
|
||||||
|
|
||||||
@GetMapping("/id/{id}")
|
@GetMapping("/id/{id}")
|
||||||
public ResponseEntity<?> getDatasetById(@PathVariable("id") UUID id) {
|
public ResponseEntity<Dataset> getDatasetById(@PathVariable("id") UUID id) {
|
||||||
Dataset d = datasetService.getDatasetById(id);
|
Dataset d = datasetService.getDatasetById(id);
|
||||||
if (d == null) {
|
if (d == null) {
|
||||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
return new ResponseEntity<>(d , HttpStatus.OK);
|
return new ResponseEntity<>(d, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ResponseStatus(HttpStatus.CREATED)
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
@ -52,7 +53,7 @@ public class DatasetController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/id/{id}/upvote")
|
@PutMapping("/id/{id}/upvote")
|
||||||
public ResponseEntity<?> upvote(@PathVariable("id") UUID id) {
|
public ResponseEntity<Dataset> upvote(@PathVariable("id") UUID id) {
|
||||||
if (datasetService.getDatasetById(id) == null) {
|
if (datasetService.getDatasetById(id) == null) {
|
||||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
@ -62,16 +63,16 @@ public class DatasetController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/id/{id}/downvote")
|
@PutMapping("/id/{id}/downvote")
|
||||||
public ResponseEntity<?> downvote(@PathVariable("id") UUID id) {
|
public ResponseEntity<Dataset> downvote(@PathVariable("id") UUID id) {
|
||||||
if (datasetService.getDatasetById(id) == null) {
|
if (datasetService.getDatasetById(id) == null) {
|
||||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
datasetService.downvoteDataset(id);
|
datasetService.downvoteDataset(id);
|
||||||
return new ResponseEntity<>(getDatasetById(id), HttpStatus.OK);
|
return new ResponseEntity<>(datasetService.getDatasetById(id), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/id/{id}/vote")
|
@PutMapping("/id/{id}/vote")
|
||||||
public ResponseEntity<?> postMethodName(@PathVariable("id") UUID id,
|
public ResponseEntity<Dataset> postMethodName(@PathVariable("id") UUID id,
|
||||||
@RequestParam("stars") int stars) {
|
@RequestParam("stars") int stars) {
|
||||||
if (datasetService.getDatasetById(id) == null) {
|
if (datasetService.getDatasetById(id) == null) {
|
||||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
@ -96,22 +97,32 @@ public class DatasetController {
|
|||||||
@RequestParam(value = "direction", required = false, defaultValue = "desc") String direction,
|
@RequestParam(value = "direction", required = false, defaultValue = "desc") String direction,
|
||||||
@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(Sort.Direction.fromString(direction), sort));
|
||||||
return datasetService.getDatasetsByOptionalCriteria(title, description, author, abst, type, rating, category,
|
return datasetService.getDatasetsByOptionalCriteria(title, description, author, abst, type, rating, category,
|
||||||
pageable);
|
pageable);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/search")
|
@GetMapping("/search")
|
||||||
public Page<Dataset> search(@RequestParam(value = "search", required = false, defaultValue = "%") String search,
|
public ResponseEntity<Page<Dataset>> search(
|
||||||
|
@RequestParam(value = "search", required = false, defaultValue = "%") String search,
|
||||||
@RequestParam(value = "page", required = false, defaultValue = "0") int page,
|
@RequestParam(value = "page", required = false, defaultValue = "0") int page,
|
||||||
@RequestParam(value = "size", required = false, defaultValue = "20") int size,
|
@RequestParam(value = "size", required = false, defaultValue = "20") int size,
|
||||||
@RequestParam(value = "sort", required = false, defaultValue = "upvotes") String sort,
|
@RequestParam(value = "sort", required = false, defaultValue = "upvotes") String sort,
|
||||||
@RequestParam(value = "direction", required = false, defaultValue = "desc") String direction,
|
@RequestParam(value = "direction", required = false, defaultValue = "desc") String direction,
|
||||||
@RequestParam(value = "category", required = false, defaultValue = "%") String category,
|
@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,
|
Pageable pageable = null;
|
||||||
Sort.by(direction.equals("asc") ? Sort.Direction.ASC : Sort.Direction.DESC, sort));
|
if (!Dataset.getSort().contains(sort))
|
||||||
return datasetService.searchByOptionalCriteria(search, category, type, pageable);
|
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||||
|
try {
|
||||||
|
pageable = PageRequest.of(page, size,
|
||||||
|
Sort.by(Sort.Direction.fromString(direction), sort));
|
||||||
|
} catch (Exception e) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ResponseEntity<>(datasetService.searchByOptionalCriteria(search, category, type, pageable),
|
||||||
|
HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -2,6 +2,8 @@ package de.uni_passau.fim.PADAS.group3.DataDash.model;
|
|||||||
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.sql.Date;
|
import java.sql.Date;
|
||||||
|
|
||||||
@ -41,6 +43,8 @@ public class Dataset {
|
|||||||
|
|
||||||
private URL url;
|
private URL url;
|
||||||
|
|
||||||
|
private static final List<String> sortable = Arrays.asList("author", "title", "upvotes", "date");
|
||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
private Category categorie;
|
private Category categorie;
|
||||||
|
|
||||||
@ -111,6 +115,10 @@ public class Dataset {
|
|||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<String> getSort() {
|
||||||
|
return sortable;
|
||||||
|
}
|
||||||
|
|
||||||
public void setAbst(String abst) {
|
public void setAbst(String abst) {
|
||||||
this.abst = abst.substring(0, Math.min(abst.length(), 100));
|
this.abst = abst.substring(0, Math.min(abst.length(), 100));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user