chore: Refactor DatasetController and DatasetService
This commit refactors the `DatasetController` and `DatasetService` classes. It updates the request mappings in the `DatasetController` class to `/api/v1/datasets` and adds new methods for upvoting and downvoting datasets. In the `DatasetService` class, it adds the `upvoteDataset` and `downvoteDataset` methods to handle dataset upvoting and downvoting. This refactoring improves code organization and functionality.
This commit is contained in:
parent
10e339f32b
commit
4a09da87d3
@ -1,25 +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.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 java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import de.uni_passau.fim.PADAS.group3.DataDash.model.Type;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import javax.swing.text.html.HTML;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/api/datasets")
|
|
||||||
public class DatasetController {
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/datasets")
|
||||||
|
public class DatasetController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private DatasetService datasetService;
|
private DatasetService datasetService;
|
||||||
|
|
||||||
@ -81,4 +79,17 @@ import org.springframework.web.bind.annotation.RequestParam;
|
|||||||
return datasetService.getDatasetsByRaitingGreaterThan(rating);
|
return datasetService.getDatasetsByRaitingGreaterThan(rating);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/id/{id}/upvote")
|
||||||
|
public Dataset upvote(@PathVariable("id") UUID id) {
|
||||||
|
datasetService.upvoteDataset(id);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/id/{id}/downvote")
|
||||||
|
public Dataset downvote(@PathVariable("id") UUID id) {
|
||||||
|
datasetService.downvoteDataset(id);
|
||||||
|
return null; //new ResponseEntity<>(null, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -33,12 +33,15 @@ public class Dataset {
|
|||||||
|
|
||||||
private int votes;
|
private int votes;
|
||||||
|
|
||||||
|
private int upvotes;
|
||||||
|
|
||||||
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) {
|
||||||
|
|
||||||
this.raiting = 0;
|
this.raiting = 0;
|
||||||
this.votes = 0;
|
this.votes = 0;
|
||||||
|
this.upvotes = 0;
|
||||||
setTitle(title);
|
setTitle(title);
|
||||||
setAbst(abst);
|
setAbst(abst);
|
||||||
setDescription(description);
|
setDescription(description);
|
||||||
@ -92,6 +95,10 @@ public class Dataset {
|
|||||||
return votes;
|
return votes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getUpvotes() {
|
||||||
|
return upvotes;
|
||||||
|
}
|
||||||
|
|
||||||
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));
|
||||||
}
|
}
|
||||||
@ -124,4 +131,12 @@ public class Dataset {
|
|||||||
raiting = (raiting*votes + stars) / (++votes);
|
raiting = (raiting*votes + stars) / (++votes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void upvote() {
|
||||||
|
upvotes++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void downvote() {
|
||||||
|
upvotes--;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,12 @@ package de.uni_passau.fim.PADAS.group3.DataDash.model;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class DatasetService {
|
public class DatasetService {
|
||||||
private dataRepository datasetRepository;
|
private dataRepository datasetRepository;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public DatasetService(dataRepository datasetRepository) {
|
public DatasetService(dataRepository datasetRepository) {
|
||||||
this.datasetRepository = datasetRepository;
|
this.datasetRepository = datasetRepository;
|
||||||
}
|
}
|
||||||
@ -62,4 +60,16 @@ public class DatasetService {
|
|||||||
public List<Dataset> getDatasetsByRaitingGreaterThan(float raiting) {
|
public List<Dataset> getDatasetsByRaitingGreaterThan(float raiting) {
|
||||||
return datasetRepository.findByRaitingGreaterThan(raiting);
|
return datasetRepository.findByRaitingGreaterThan(raiting);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void upvoteDataset(UUID id) {
|
||||||
|
Dataset dataset = datasetRepository.getDatasetById(id);
|
||||||
|
dataset.upvote();
|
||||||
|
datasetRepository.save(dataset);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void downvoteDataset(UUID id) {
|
||||||
|
Dataset dataset = datasetRepository.getDatasetById(id);
|
||||||
|
dataset.downvote();
|
||||||
|
datasetRepository.save(dataset);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user