feat: Add ability to add categories
This commit is contained in:
parent
a883d8217e
commit
7554eefae4
@ -1,18 +1,43 @@
|
||||
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.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.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
|
||||
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/categories")
|
||||
public class CategoryController {
|
||||
@Autowired
|
||||
private CategoryService categoryService;
|
||||
|
||||
@GetMapping
|
||||
public Category[] getMethodName() {
|
||||
return Category.values();
|
||||
public List<CategoryDto> getMethodName() {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,14 +1,45 @@
|
||||
package de.uni_passau.fim.PADAS.group3.DataDash.model;
|
||||
|
||||
public enum Category {
|
||||
HEALTH,
|
||||
ENVIRONMENT,
|
||||
ECONOMY,
|
||||
POLITICS,
|
||||
TECHNOLOGY,
|
||||
SPORTS,
|
||||
SCIENCE,
|
||||
CULTURE,
|
||||
EDUCATION,
|
||||
OTHER
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToMany;
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
|
||||
}
|
@ -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));
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -11,6 +11,7 @@ import jakarta.persistence.Enumerated;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class Dataset {
|
||||
@ -39,7 +40,8 @@ public class Dataset {
|
||||
private int upvotes;
|
||||
|
||||
private URL url;
|
||||
@Enumerated(EnumType.STRING)
|
||||
|
||||
@ManyToOne
|
||||
private Category categorie;
|
||||
|
||||
public Dataset(String title, String abst, String description, String author, URL url, Category categories, Type type) {
|
||||
|
@ -13,9 +13,11 @@ import org.springframework.data.domain.Page;
|
||||
@Service
|
||||
public class DatasetService {
|
||||
private dataRepository datasetRepository;
|
||||
private CategoryRepository categoryRepository;
|
||||
|
||||
public DatasetService(dataRepository datasetRepository) {
|
||||
public DatasetService(dataRepository datasetRepository, CategoryRepository categoryRepository) {
|
||||
this.datasetRepository = datasetRepository;
|
||||
this.categoryRepository = categoryRepository;
|
||||
}
|
||||
|
||||
public List<Dataset> getAllDatasets() {
|
||||
@ -95,9 +97,14 @@ public class DatasetService {
|
||||
|
||||
public Page<Dataset> searchByOptionalCriteria(String search, String categories, String type, Pageable pageable) {
|
||||
//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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
@ -16,17 +16,21 @@ public class LoadDummyDatabase {
|
||||
private static final org.slf4j.Logger log = LoggerFactory.getLogger(LoadDummyDatabase.class);
|
||||
|
||||
@Bean
|
||||
CommandLineRunner initDatabase(dataRepository repository) {
|
||||
CommandLineRunner initDatabase(dataRepository repository, CategoryRepository categoryRepository) {
|
||||
|
||||
|
||||
|
||||
return args -> {
|
||||
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++) {
|
||||
dataset.upvote();
|
||||
}
|
||||
log.info("Preloading" + repository.save(dataset));
|
||||
log.info("Preloading" + categoryRepository.save(category));
|
||||
}
|
||||
List<Dataset> s = repository.findByTitleLike("%Title%");
|
||||
log.info("Found Entry with ID: " + s.get(1).getId());};
|
||||
|
@ -65,10 +65,19 @@ public interface dataRepository extends JpaRepository<Dataset, UUID> {
|
||||
"((LOWER(d.title) LIKE LOWER(:search)) OR " +
|
||||
"(LOWER(d.description) LIKE LOWER(:search)) OR " +
|
||||
"(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)")
|
||||
Page<Dataset> searchByOptionalCriteria(@Param("search") Optional<String> search,
|
||||
@Param("categorie") Optional<Category> categories,
|
||||
Page<Dataset> searchByOptionalCriteriaWithCategory(@Param("search") Optional<String> search,
|
||||
@Param("categorie") Category categories,
|
||||
@Param("type") Optional<Type> type,
|
||||
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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user