feat: Add ability to add categories

This commit is contained in:
Erik Foris 2024-07-02 10:41:52 +02:00
parent a883d8217e
commit 7554eefae4
10 changed files with 189 additions and 24 deletions

View File

@ -1,18 +1,43 @@
package de.uni_passau.fim.PADAS.group3.DataDash.controler; package de.uni_passau.fim.PADAS.group3.DataDash.controler;
import java.util.List;
import java.util.UUID;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import de.uni_passau.fim.PADAS.group3.DataDash.model.Category; 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.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@RestController @RestController
@RequestMapping("/api/v1/categories") @RequestMapping("/api/v1/categories")
public class CategoryController { public class CategoryController {
@Autowired
private CategoryService categoryService;
@GetMapping @GetMapping
public Category[] getMethodName() { public List<CategoryDto> getMethodName() {
return Category.values(); return categoryService.getAllCategories() ;
} }
@GetMapping("/id/{id}")
public CategoryDto getMethodName(@PathVariable("id") UUID id) {
return categoryService.getCategoryById(id);
}
@PostMapping
public String postMethodName(@RequestBody CategoryDto dto) {
categoryService.addCategory(dto);
return null;
}
} }

View File

@ -1,14 +1,45 @@
package de.uni_passau.fim.PADAS.group3.DataDash.model; package de.uni_passau.fim.PADAS.group3.DataDash.model;
public enum Category { import java.util.List;
HEALTH, import java.util.UUID;
ENVIRONMENT,
ECONOMY, import org.springframework.context.annotation.Lazy;
POLITICS,
TECHNOLOGY, import jakarta.persistence.Entity;
SPORTS, import jakarta.persistence.GeneratedValue;
SCIENCE, import jakarta.persistence.GenerationType;
CULTURE, import jakarta.persistence.Id;
EDUCATION, import jakarta.persistence.OneToMany;
OTHER
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;
private String name;
@Lazy
@OneToMany(mappedBy = "categorie")
private List<Dataset> datasets;
public Category(String name) {
this.name = name;
}
public Category() {
}
public UUID getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
} }

View File

@ -0,0 +1,27 @@
package de.uni_passau.fim.PADAS.group3.DataDash.model;
import java.util.UUID;
public class CategoryDto {
private String name;
private UUID id;
public CategoryDto() {
}
CategoryDto(String name, UUID id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public UUID getId() {
return id;
}
}

View File

@ -0,0 +1,10 @@
package de.uni_passau.fim.PADAS.group3.DataDash.model;
public class CategoryDtoMapper {
public static CategoryDto toDto(Category category) {
CategoryDto dto = new CategoryDto(category.getName(), category.getId());
return dto;
}
}

View File

@ -0,0 +1,18 @@
package de.uni_passau.fim.PADAS.group3.DataDash.model;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CategoryRepository extends JpaRepository<Category, UUID>{
Category getCategoryById(UUID id);
List<Category> findAll();
List<Category> findByName(String name);
Optional<Category> findById(UUID id);
}

View File

@ -0,0 +1,32 @@
package de.uni_passau.fim.PADAS.group3.DataDash.model;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.UUID;
@Service
public class CategoryService {
private CategoryRepository categoryRepository;
public CategoryService(CategoryRepository categoryRepository) {
this.categoryRepository = categoryRepository;
}
public void addCategory(CategoryDto category) {
Category cat = new Category(category.getName());
categoryRepository.save(cat);
}
public List<CategoryDto> getAllCategories() {
List<Category> tmp = categoryRepository.findAll();
List<CategoryDto> s = tmp.stream().map(CategoryDtoMapper::toDto).toList();
return s;
}
public CategoryDto getCategoryById(UUID id) {
return CategoryDtoMapper.toDto(categoryRepository.getCategoryById(id));
}
}

View File

@ -11,6 +11,7 @@ import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue; import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType; import jakarta.persistence.GenerationType;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
@Entity @Entity
public class Dataset { public class Dataset {
@ -39,7 +40,8 @@ public class Dataset {
private int upvotes; private int upvotes;
private URL url; private URL url;
@Enumerated(EnumType.STRING)
@ManyToOne
private Category categorie; private Category categorie;
public Dataset(String title, String abst, String description, String author, URL url, Category categories, Type type) { public Dataset(String title, String abst, String description, String author, URL url, Category categories, Type type) {

View File

@ -13,9 +13,11 @@ import org.springframework.data.domain.Page;
@Service @Service
public class DatasetService { public class DatasetService {
private dataRepository datasetRepository; private dataRepository datasetRepository;
private CategoryRepository categoryRepository;
public DatasetService(dataRepository datasetRepository) { public DatasetService(dataRepository datasetRepository, CategoryRepository categoryRepository) {
this.datasetRepository = datasetRepository; this.datasetRepository = datasetRepository;
this.categoryRepository = categoryRepository;
} }
public List<Dataset> getAllDatasets() { public List<Dataset> getAllDatasets() {
@ -95,9 +97,14 @@ public class DatasetService {
public Page<Dataset> searchByOptionalCriteria(String search, String categories, String type, Pageable pageable) { public Page<Dataset> searchByOptionalCriteria(String search, String categories, String type, Pageable pageable) {
//TODO: make it not Crash //TODO: make it not Crash
Category category = categories.equals("%") ? null : Category.valueOf(categories); //TODO: make it do useful stuff
Category category = categories.equals("%") ? null : categoryRepository.getCategoryById(UUID.fromString(categories)) ;
Type t = type.equals("%") ? null : Type.valueOf(type); Type t = type.equals("%") ? null : Type.valueOf(type);
return datasetRepository.searchByOptionalCriteria(Optional.ofNullable(search), Optional.ofNullable(category), 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);
} }
} }

View File

@ -16,17 +16,21 @@ public class LoadDummyDatabase {
private static final org.slf4j.Logger log = LoggerFactory.getLogger(LoadDummyDatabase.class); private static final org.slf4j.Logger log = LoggerFactory.getLogger(LoadDummyDatabase.class);
@Bean @Bean
CommandLineRunner initDatabase(dataRepository repository) { CommandLineRunner initDatabase(dataRepository repository, CategoryRepository categoryRepository) {
return args -> { return args -> {
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
Dataset dataset = new Dataset("Title" + i, "Abst" + i, "Description" + i, "Author" + i,null, Category.EDUCATION, Type.API); Category category = new Category("Category" + i);
log.info("Preloading" + categoryRepository.save(category));
Dataset dataset = new Dataset("Title" + i, "Abst" + i, "Description" + i, "Author" + i,null, category, Type.API);
for (int j = 0; j < new Random().nextInt(50); j++) { for (int j = 0; j < new Random().nextInt(50); j++) {
dataset.upvote(); dataset.upvote();
} }
log.info("Preloading" + repository.save(dataset)); log.info("Preloading" + repository.save(dataset));
log.info("Preloading" + categoryRepository.save(category));
} }
List<Dataset> s = repository.findByTitleLike("%Title%"); List<Dataset> s = repository.findByTitleLike("%Title%");
log.info("Found Entry with ID: " + s.get(1).getId());}; log.info("Found Entry with ID: " + s.get(1).getId());};

View File

@ -65,10 +65,19 @@ public interface dataRepository extends JpaRepository<Dataset, UUID> {
"((LOWER(d.title) LIKE LOWER(:search)) OR " + "((LOWER(d.title) LIKE LOWER(:search)) OR " +
"(LOWER(d.description) LIKE LOWER(:search)) OR " + "(LOWER(d.description) LIKE LOWER(:search)) OR " +
"(LOWER(d.author) LIKE LOWER(:search))) AND" + "(LOWER(d.author) LIKE LOWER(:search))) AND" +
"(:categorie IS NULL OR d.categorie = :categorie) AND" + "(d.categorie = :categorie) AND" +
"(:type IS NULL OR d.type = :type)") "(:type IS NULL OR d.type = :type)")
Page<Dataset> searchByOptionalCriteria(@Param("search") Optional<String> search, Page<Dataset> searchByOptionalCriteriaWithCategory(@Param("search") Optional<String> search,
@Param("categorie") Optional<Category> categories, @Param("categorie") Category categories,
@Param("type") Optional<Type> type, @Param("type") Optional<Type> type,
Pageable pageable); 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))) AND" +
"(:type IS NULL OR d.type = :type)")
Page<Dataset> searchByOptionalCriteria(@Param("search") Optional<String> search,
@Param("type") Optional<Type> type,
Pageable pageable);
}