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 org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
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.CategoryService;
|
||||
|
||||
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.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@ -29,14 +32,17 @@ public class CategoryController {
|
||||
}
|
||||
|
||||
@GetMapping("/id/{id}")
|
||||
public CategoryDto getMethodName(@PathVariable("id") UUID id) {
|
||||
return categoryService.getCategoryById(id);
|
||||
public ResponseEntity<?> fetchCategoryById(@PathVariable("id") UUID 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
|
||||
public String postMethodName(@RequestBody CategoryDto dto) {
|
||||
public void createCategory(@RequestBody CategoryDto 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.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,58 +27,64 @@ 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<Dataset> 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<Dataset> 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<Dataset> 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<>(datasetService.getDatasetById(id), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PutMapping("/id/{id}/vote")
|
||||
public Dataset postMethodName(@PathVariable("id") UUID id,
|
||||
public ResponseEntity<Dataset> 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,
|
||||
@ -87,21 +96,32 @@ public class DatasetController {
|
||||
@RequestParam(value = "direction", required = false, defaultValue = "desc") String direction,
|
||||
@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);
|
||||
Sort.by(Sort.Direction.fromString(direction), sort));
|
||||
return datasetService.getDatasetsByOptionalCriteria(title, description, author, abst, type, rating, category,
|
||||
pageable);
|
||||
}
|
||||
|
||||
@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 = "size", required = false, defaultValue = "20") int size,
|
||||
@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){
|
||||
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);
|
||||
@RequestParam(value = "type", required = false, defaultValue = "%") String type) {
|
||||
Pageable pageable = null;
|
||||
if (!Dataset.getSort().contains(sort))
|
||||
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.UUID;
|
||||
|
||||
|
||||
@Service
|
||||
public class CategoryService {
|
||||
private CategoryRepository categoryRepository;
|
||||
@ -25,8 +24,11 @@ public class CategoryService {
|
||||
}
|
||||
|
||||
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.time.LocalDate;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.sql.Date;
|
||||
|
||||
@ -41,6 +43,8 @@ public class Dataset {
|
||||
|
||||
private URL url;
|
||||
|
||||
private static final List<String> sortable = Arrays.asList("author", "title", "upvotes", "date");
|
||||
|
||||
@ManyToOne
|
||||
private Category categorie;
|
||||
|
||||
@ -111,6 +115,10 @@ public class Dataset {
|
||||
return url;
|
||||
}
|
||||
|
||||
public static List<String> getSort() {
|
||||
return sortable;
|
||||
}
|
||||
|
||||
public void setAbst(String abst) {
|
||||
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.data.domain.Page;
|
||||
|
||||
|
||||
@Service
|
||||
public class DatasetService {
|
||||
private dataRepository datasetRepository;
|
||||
@ -28,9 +27,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) {
|
||||
@ -91,20 +90,22 @@ public class DatasetService {
|
||||
public Page<Dataset> getDatasetsByOptionalCriteria(String title, String description, String author, String abst,
|
||||
Type type, Float raiting, Category category, Pageable pageable) {
|
||||
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);
|
||||
}
|
||||
|
||||
public Page<Dataset> searchByOptionalCriteria(String search, String categories, String type, Pageable pageable) {
|
||||
//TODO: make it not Crash
|
||||
//TODO: make it do useful stuff
|
||||
Category category = categories.equals("%") ? null : categoryRepository.getCategoryById(UUID.fromString(categories)) ;
|
||||
Category category = categories.equals("%") ? null
|
||||
: categoryRepository.getCategoryById(UUID.fromString(categories));
|
||||
Type t = type.equals("%") ? null : Type.valueOf(type);
|
||||
|
||||
if(category == null){
|
||||
return datasetRepository.searchByOptionalCriteria(Optional.ofNullable(search), Optional.ofNullable(t),pageable);
|
||||
if (category == null) {
|
||||
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