Merge branch '16-add-useful-response-messages' into '11-add-api-for-getting-home-page-data'
Resolve "Add useful response Codes" See merge request padas/24ss-5430-web-and-data-eng/gruppe-3/datadash!31
This commit is contained in:
commit
77879b5f81
@ -3,12 +3,15 @@ import java.util.List;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import de.uni_passau.fim.PADAS.group3.DataDash.model.CategoryDto;
|
import de.uni_passau.fim.PADAS.group3.DataDash.model.CategoryDto;
|
||||||
import de.uni_passau.fim.PADAS.group3.DataDash.model.CategoryService;
|
import de.uni_passau.fim.PADAS.group3.DataDash.model.CategoryService;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
@ -29,14 +32,17 @@ public class CategoryController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/id/{id}")
|
@GetMapping("/id/{id}")
|
||||||
public CategoryDto getMethodName(@PathVariable("id") UUID id) {
|
public ResponseEntity<?> fetchCategoryById(@PathVariable("id") UUID id) {
|
||||||
return categoryService.getCategoryById(id);
|
CategoryDto category = categoryService.getCategoryById(id);
|
||||||
|
if(category == null) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
return new ResponseEntity<>(category, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public String postMethodName(@RequestBody CategoryDto dto) {
|
public void createCategory(@RequestBody CategoryDto dto) {
|
||||||
categoryService.addCategory(dto);
|
categoryService.addCategory(dto);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -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,58 +27,64 @@ 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<Dataset> 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<Dataset> 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<Dataset> 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<>(datasetService.getDatasetById(id), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/id/{id}/vote")
|
@PutMapping("/id/{id}/vote")
|
||||||
public Dataset postMethodName(@PathVariable("id") UUID id,
|
public ResponseEntity<Dataset> 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
|
||||||
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 = "title", required = false) String title,
|
||||||
@RequestParam(value = "description", required = false) String description,
|
@RequestParam(value = "description", required = false) String description,
|
||||||
@RequestParam(value = "abst", required = false) String abst,
|
@RequestParam(value = "abst", required = false) String abst,
|
||||||
@ -87,21 +96,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,pageable);
|
return datasetService.getDatasetsByOptionalCriteria(title, description, author, abst, type, rating, category,
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -4,7 +4,6 @@ import org.springframework.stereotype.Service;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class CategoryService {
|
public class CategoryService {
|
||||||
private CategoryRepository categoryRepository;
|
private CategoryRepository categoryRepository;
|
||||||
@ -25,8 +24,11 @@ public class CategoryService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public CategoryDto getCategoryById(UUID id) {
|
public CategoryDto getCategoryById(UUID id) {
|
||||||
return CategoryDtoMapper.toDto(categoryRepository.getCategoryById(id));
|
Category c = categoryRepository.getCategoryById(id);
|
||||||
|
if (c == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return CategoryDtoMapper.toDto(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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));
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,6 @@ import org.springframework.data.domain.Pageable;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class DatasetService {
|
public class DatasetService {
|
||||||
private dataRepository datasetRepository;
|
private dataRepository datasetRepository;
|
||||||
@ -28,9 +27,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) {
|
||||||
@ -91,20 +90,22 @@ public class DatasetService {
|
|||||||
public Page<Dataset> getDatasetsByOptionalCriteria(String title, String description, String author, String abst,
|
public Page<Dataset> getDatasetsByOptionalCriteria(String title, String description, String author, String abst,
|
||||||
Type type, Float raiting, Category category, Pageable pageable) {
|
Type type, Float raiting, Category category, Pageable pageable) {
|
||||||
return datasetRepository.findByOptionalCriteria(Optional.ofNullable(title), Optional.ofNullable(description),
|
return datasetRepository.findByOptionalCriteria(Optional.ofNullable(title), Optional.ofNullable(description),
|
||||||
Optional.ofNullable(author), Optional.ofNullable(abst), Optional.ofNullable(type), Optional.ofNullable(category),
|
Optional.ofNullable(author), Optional.ofNullable(abst), Optional.ofNullable(type),
|
||||||
|
Optional.ofNullable(category),
|
||||||
Optional.ofNullable(raiting), pageable);
|
Optional.ofNullable(raiting), pageable);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Page<Dataset> searchByOptionalCriteria(String search, String categories, String type, Pageable pageable) {
|
public Page<Dataset> searchByOptionalCriteria(String search, String categories, String type, Pageable pageable) {
|
||||||
//TODO: make it not Crash
|
Category category = categories.equals("%") ? null
|
||||||
//TODO: make it do useful stuff
|
: categoryRepository.getCategoryById(UUID.fromString(categories));
|
||||||
Category category = categories.equals("%") ? null : categoryRepository.getCategoryById(UUID.fromString(categories)) ;
|
|
||||||
Type t = type.equals("%") ? null : Type.valueOf(type);
|
Type t = type.equals("%") ? null : Type.valueOf(type);
|
||||||
|
|
||||||
if(category == null){
|
if (category == null) {
|
||||||
return datasetRepository.searchByOptionalCriteria(Optional.ofNullable(search), Optional.ofNullable(t),pageable);
|
return datasetRepository.searchByOptionalCriteria(Optional.ofNullable(search), Optional.ofNullable(t),
|
||||||
|
pageable);
|
||||||
}
|
}
|
||||||
return datasetRepository.searchByOptionalCriteriaWithCategory(Optional.ofNullable(search), category, Optional.ofNullable(t),pageable);
|
return datasetRepository.searchByOptionalCriteriaWithCategory(Optional.ofNullable(search), category,
|
||||||
|
Optional.ofNullable(t), pageable);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user