chore: Refactor DatasetController and DatasetService, add upvote and downvote functionality and create new search quirey to use with request parameters

This commit is contained in:
Erik Foris 2024-06-18 13:35:15 +02:00
parent 4a09da87d3
commit 35fcd0513b
3 changed files with 37 additions and 20 deletions

View File

@ -6,42 +6,41 @@ 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 java.sql.Date;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
@RestController @RestController
@RequestMapping("/api/v1/datasets") @RequestMapping("/api/v1/datasets")
public class DatasetController { public class DatasetController {
@Autowired @Autowired
private DatasetService datasetService; private DatasetService datasetService;
@GetMapping // @GetMapping
public List<Dataset> getAllDatasets() { // public List<Dataset> getAllDatasets() {
return datasetService.getAllDatasets(); // return datasetService.getAllDatasets();
} // }
@GetMapping("/id/{id}") @GetMapping("/id/{id}")
public Dataset getDatasetById(@PathVariable("id") UUID id) { public Dataset getDatasetById(@PathVariable("id") UUID id) {
return datasetService.getDatasetById(id); return datasetService.getDatasetById(id);
} }
@PostMapping @PostMapping
public Dataset createDataset(@RequestBody Dataset dataset) { public Dataset createDataset(@RequestBody Dataset dataset) {
datasetService.addDataset(dataset); datasetService.addDataset(dataset);
//TODO: figure out what the fuck i need to do here // TODO: figure out what the fuck i need to do here
return null; return null;
} }
//@PutMapping("/{id}") // @PutMapping("/{id}")
//public Dataset updateDataset(@PathVariable("id") Long id, @RequestBody Dataset dataset) { // public Dataset updateDataset(@PathVariable("id") Long id, @RequestBody
// return datasetService.updateDataset(id, dataset); // Dataset dataset) {
//} // return datasetService.updateDataset(id, dataset);
// }
// //
@DeleteMapping("/id/{id}") @DeleteMapping("/id/{id}")
@ -63,7 +62,7 @@ public class DatasetController {
public List<Dataset> getByAuthor(@PathVariable("author") String author) { public List<Dataset> getByAuthor(@PathVariable("author") String author) {
return datasetService.getDatasetsByAuthorLike(author); return datasetService.getDatasetsByAuthorLike(author);
} }
@GetMapping("/abst/{abst}") @GetMapping("/abst/{abst}")
public List<Dataset> getByAbstract(@PathVariable("abst") String abst) { public List<Dataset> getByAbstract(@PathVariable("abst") String abst) {
return datasetService.getDatasetsByAbstLike(abst); return datasetService.getDatasetsByAbstLike(abst);
@ -81,15 +80,22 @@ public class DatasetController {
@PostMapping("/id/{id}/upvote") @PostMapping("/id/{id}/upvote")
public Dataset upvote(@PathVariable("id") UUID id) { public Dataset upvote(@PathVariable("id") UUID id) {
datasetService.upvoteDataset(id); datasetService.upvoteDataset(id);
return null; return null;
} }
@PostMapping("/id/{id}/downvote") @PostMapping("/id/{id}/downvote")
public Dataset downvote(@PathVariable("id") UUID id) { public Dataset downvote(@PathVariable("id") UUID id) {
datasetService.downvoteDataset(id); datasetService.downvoteDataset(id);
return null; //new ResponseEntity<>(null, HttpStatus.OK); return null; // new ResponseEntity<>(null, HttpStatus.OK);
} }
@GetMapping
public List<Dataset> getDatasetsByDateAfter(@RequestParam(value = "author", required = false, defaultValue = "%") String author,
@RequestParam(value = "title", required = false, defaultValue = "%") String title,
@RequestParam(value = "description", required = false, defaultValue = "%") String description,
@RequestParam(value = "abst", required = false, defaultValue = "%") String abst) {
return datasetService.getDatasetsBy(title, author, abst, description);
}
} }

View File

@ -72,4 +72,8 @@ public class DatasetService {
dataset.downvote(); dataset.downvote();
datasetRepository.save(dataset); datasetRepository.save(dataset);
} }
public List<Dataset> getDatasetsBy(String title, String author, String abst, String description) {
return datasetRepository.findBy(title, author, abst, description);
}
} }

View File

@ -5,6 +5,8 @@ import java.util.UUID;
import java.sql.Date; import java.sql.Date;
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.repository.query.Param;
public interface dataRepository extends JpaRepository<Dataset, UUID>{ public interface dataRepository extends JpaRepository<Dataset, UUID>{
@ -23,5 +25,10 @@ public interface dataRepository extends JpaRepository<Dataset, UUID>{
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(); List<Dataset> findAll();
@Query("SELECT d FROM Dataset d WHERE d.title LIKE :title AND d.author LIKE :author AND d.abst LIKE :abst AND d.description LIKE :description")
List<Dataset> findBy(@Param("title") String title, @Param("author") String author, @Param("abst") String abst, @Param("description") String description);
} }