Refactor DatasetController and DatasetService, add upvote and downvote functionality and create new search query to use with request parameters
This commit is contained in:
parent
7597652f57
commit
d616d193f1
@ -1,17 +1,23 @@
|
|||||||
package de.uni_passau.fim.PADAS.group3.DataDash.controler;
|
package de.uni_passau.fim.PADAS.group3.DataDash.controler;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
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.web.config.EnableSpringDataWebSupport;
|
||||||
|
import org.springframework.data.domain.Sort;
|
||||||
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/v1/datasets")
|
@RequestMapping("/api/v1/datasets")
|
||||||
|
@EnableSpringDataWebSupport
|
||||||
public class DatasetController {
|
public class DatasetController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private DatasetService datasetService;
|
private DatasetService datasetService;
|
||||||
@ -87,14 +93,27 @@ public class DatasetController {
|
|||||||
return null; // new ResponseEntity<>(null, HttpStatus.OK);
|
return null; // new ResponseEntity<>(null, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping("/find")
|
||||||
public List<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,
|
||||||
@RequestParam(value = "type", required = false) Type type,
|
@RequestParam(value = "type", required = false) Type type,
|
||||||
@RequestParam(value = "min-raiting", required = false) Float raiting) {
|
@RequestParam(value = "min-raiting", required = false) Float raiting,
|
||||||
return datasetService.getDatasetsByOptionalCriteria(title, description, author, abst, type, raiting);
|
@RequestParam(value = "page", required = false) int page,
|
||||||
|
@RequestParam(value = "size", required = false) int size) {
|
||||||
|
Pageable pageable = PageRequest.of(page, size);
|
||||||
|
return datasetService.getDatasetsByOptionalCriteria(title, description, author, abst, type, raiting, pageable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public 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){
|
||||||
|
Pageable pageable = PageRequest.of(page, size, Sort.by(direction.equals("asc") ? Sort.Direction.ASC : Sort.Direction.DESC, sort));
|
||||||
|
return datasetService.searchByOptionalCriteria(search, pageable);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package de.uni_passau.fim.PADAS.group3.DataDash.model;
|
package de.uni_passau.fim.PADAS.group3.DataDash.model;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
import java.sql.Date;
|
import java.sql.Date;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.Entity;
|
||||||
@ -35,6 +36,8 @@ public class Dataset {
|
|||||||
|
|
||||||
private int upvotes;
|
private int upvotes;
|
||||||
|
|
||||||
|
private URL url;
|
||||||
|
|
||||||
private String[] categories;
|
private String[] categories;
|
||||||
|
|
||||||
public Dataset(String title, String abst, String description, String author, Date date, String[] categories, Type type) {
|
public Dataset(String title, String abst, String description, String author, Date date, String[] categories, Type type) {
|
||||||
@ -49,6 +52,7 @@ public class Dataset {
|
|||||||
setDate(date);
|
setDate(date);
|
||||||
setCategories(categories);
|
setCategories(categories);
|
||||||
setType(type);
|
setType(type);
|
||||||
|
setUrl(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Dataset() {
|
public Dataset() {
|
||||||
@ -99,6 +103,10 @@ public class Dataset {
|
|||||||
return upvotes;
|
return upvotes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public URL getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
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));
|
||||||
}
|
}
|
||||||
@ -118,6 +126,10 @@ public class Dataset {
|
|||||||
public void setDescription(String description) {
|
public void setDescription(String description) {
|
||||||
this.description = description;
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setUrl(URL url) {
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
public void setTitle(String title) {
|
public void setTitle(String title) {
|
||||||
this.title = title.substring(0, Math.min(title.length(), 50));
|
this.title = title.substring(0, Math.min(title.length(), 50));
|
||||||
|
@ -4,7 +4,11 @@ import java.util.List;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.apache.juli.logging.Log;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class DatasetService {
|
public class DatasetService {
|
||||||
@ -75,11 +79,19 @@ public class DatasetService {
|
|||||||
datasetRepository.save(dataset);
|
datasetRepository.save(dataset);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<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) {
|
Type type, Float raiting, Pageable pageable) {
|
||||||
String[] categories = null;
|
String[] categories = null;
|
||||||
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(author), Optional.ofNullable(abst), Optional.ofNullable(type),
|
||||||
Optional.ofNullable(categories), Optional.ofNullable(raiting));
|
Optional.ofNullable(categories), Optional.ofNullable(raiting), pageable);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Page<Dataset> searchByOptionalCriteria(String search, Pageable pageable) {
|
||||||
|
if (search.equals("%")) {
|
||||||
|
System.out.println("searching for all datasets");
|
||||||
|
return datasetRepository.findAll(pageable);
|
||||||
|
}
|
||||||
|
return datasetRepository.searchByOptionalCriteria(Optional.ofNullable(search), pageable);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,10 +5,13 @@ import java.util.Optional;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.sql.Date;
|
import java.sql.Date;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.query.Param;
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
|
|
||||||
public interface dataRepository extends JpaRepository<Dataset, UUID>{
|
public interface dataRepository extends JpaRepository<Dataset, UUID>{
|
||||||
|
|
||||||
Dataset getDatasetById(UUID id);
|
Dataset getDatasetById(UUID id);
|
||||||
@ -25,7 +28,7 @@ public interface dataRepository extends JpaRepository<Dataset, UUID>{
|
|||||||
List<Dataset> findByDateAfter(Date date);
|
List<Dataset> findByDateAfter(Date date);
|
||||||
List<Dataset> findByDateBefore(Date date);
|
List<Dataset> findByDateBefore(Date date);
|
||||||
List<Dataset> findByDateBetween(Date date1, Date date2);
|
List<Dataset> findByDateBetween(Date date1, Date date2);
|
||||||
List<Dataset> findAll();
|
Page<Dataset> findAll(Pageable pageable);
|
||||||
|
|
||||||
@Query("SELECT d FROM Dataset d WHERE " +
|
@Query("SELECT d FROM Dataset d WHERE " +
|
||||||
"(COALESCE(:title, '') = '' OR d.title LIKE :title) AND " +
|
"(COALESCE(:title, '') = '' OR d.title LIKE :title) AND " +
|
||||||
@ -35,12 +38,21 @@ public interface dataRepository extends JpaRepository<Dataset, UUID>{
|
|||||||
"(:type IS NULL OR d.type = :type) AND"+
|
"(:type IS NULL OR d.type = :type) AND"+
|
||||||
"(:categories IS NULL OR d.categories = :categories) AND" +
|
"(:categories IS NULL OR d.categories = :categories) AND" +
|
||||||
"(:raiting IS NULL OR d.raiting > :raiting)")
|
"(:raiting IS NULL OR d.raiting > :raiting)")
|
||||||
List<Dataset> findByOptionalCriteria(@Param("title") Optional<String> title,
|
Page<Dataset>findByOptionalCriteria(@Param("title") Optional<String> title,
|
||||||
@Param("description") Optional<String> description,
|
@Param("description") Optional<String> description,
|
||||||
@Param("author") Optional<String> author,
|
@Param("author") Optional<String> author,
|
||||||
@Param("abst") Optional<String> abst,
|
@Param("abst") Optional<String> abst,
|
||||||
@Param("type") Optional<Type> type,
|
@Param("type") Optional<Type> type,
|
||||||
@Param("categories") Optional<String[]> categories,
|
@Param("categories") Optional<String[]> categories,
|
||||||
@Param("raiting") Optional<Float> raiting);
|
@Param("raiting") Optional<Float> raiting,
|
||||||
|
Pageable pageable);
|
||||||
|
|
||||||
|
|
||||||
|
@Query("SELECT d FROM Dataset d WHERE " +
|
||||||
|
"(LOWER(d.title) LIKE LOWER(:search)) OR " +
|
||||||
|
"(LOWER(d.description) LIKE LOWER(:search)) OR " +
|
||||||
|
"(LOWER(d.author) LIKE LOWER(:search))")
|
||||||
|
Page<Dataset>searchByOptionalCriteria(@Param("search") Optional<String> search,
|
||||||
|
Pageable pageable);
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user