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:
Erik Foris 2024-06-18 12:30:55 +02:00
parent 10e339f32b
commit 4a09da87d3
3 changed files with 48 additions and 12 deletions

View File

@ -1,25 +1,23 @@
package de.uni_passau.fim.PADAS.group3.DataDash.controler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
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.DatasetService;
import de.uni_passau.fim.PADAS.group3.DataDash.model.Type;
import java.util.List;
import java.util.UUID;
import de.uni_passau.fim.PADAS.group3.DataDash.model.Type;
import javax.swing.text.html.HTML;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@RestController
@RequestMapping("/api/datasets")
public class DatasetController {
@RestController
@RequestMapping("/api/v1/datasets")
public class DatasetController {
@Autowired
private DatasetService datasetService;
@ -81,4 +79,17 @@ import org.springframework.web.bind.annotation.RequestParam;
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);
}
}

View File

@ -33,12 +33,15 @@ public class Dataset {
private int votes;
private int upvotes;
private String[] categories;
public Dataset(String title, String abst, String description, String author, Date date, String[] categories, Type type) {
this.raiting = 0;
this.votes = 0;
this.upvotes = 0;
setTitle(title);
setAbst(abst);
setDescription(description);
@ -92,6 +95,10 @@ public class Dataset {
return votes;
}
public int getUpvotes() {
return upvotes;
}
public void setAbst(String abst) {
this.abst = abst.substring(0, Math.min(abst.length(), 100));
}
@ -124,4 +131,12 @@ public class Dataset {
raiting = (raiting*votes + stars) / (++votes);
}
public void upvote() {
upvotes++;
}
public void downvote() {
upvotes--;
}
}

View File

@ -2,14 +2,12 @@ package de.uni_passau.fim.PADAS.group3.DataDash.model;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DatasetService {
private dataRepository datasetRepository;
@Autowired
public DatasetService(dataRepository datasetRepository) {
this.datasetRepository = datasetRepository;
}
@ -62,4 +60,16 @@ public class DatasetService {
public List<Dataset> getDatasetsByRaitingGreaterThan(float 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);
}
}