Merge branch '11-add-api-for-getting-home-page-data' into 22-integrate-api-and-frontend

This commit is contained in:
Erik Foris 2024-07-04 22:59:19 +02:00
commit c73f195c2a
15 changed files with 321 additions and 67 deletions

View File

@ -52,8 +52,8 @@
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>

View File

@ -1,18 +1,49 @@
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.ResponseStatus;
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.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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 ResponseEntity<?> fetchCategoryById(@PathVariable("id") UUID id) {
CategoryDto category = categoryService.getCategoryById(id);
if(category == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(category, HttpStatus.OK);
}
@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public void createCategory(@RequestBody CategoryDto dto) {
categoryService.addCategory(dto);
}
}

View File

@ -9,8 +9,11 @@ import de.uni_passau.fim.PADAS.group3.DataDash.model.Category;
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 org.springframework.data.domain.Pageable;
import org.springframework.data.web.config.EnableSpringDataWebSupport;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Sort;
import java.util.UUID;
@ -24,58 +27,64 @@ public class DatasetController {
@Autowired
private DatasetService datasetService;
// @GetMapping
// public List<Dataset> getAllDatasets() {
// return datasetService.getAllDatasets();
// }
@GetMapping("/id/{id}")
public Dataset getDatasetById(@PathVariable("id") UUID id) {
return datasetService.getDatasetById(id);
public ResponseEntity<Dataset> getDatasetById(@PathVariable("id") UUID id) {
Dataset d = datasetService.getDatasetById(id);
if (d == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(d, HttpStatus.OK);
}
@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public Dataset createDataset(@RequestBody Dataset dataset) {
datasetService.addDataset(dataset);
// TODO: figure out what the fuck i need to do here
return null;
return datasetService.addDataset(dataset);
}
// @PutMapping("/{id}")
// public Dataset updateDataset(@PathVariable("id") Long id, @RequestBody
// Dataset dataset) {
// return datasetService.updateDataset(id, dataset);
// }
//
@DeleteMapping("/id/{id}")
public void deleteDataset(@PathVariable("id") UUID id) {
public ResponseEntity<?> deleteDataset(@PathVariable("id") UUID id) {
if (datasetService.getDatasetById(id) == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
datasetService.deleteDataset(id);
return new ResponseEntity<>(HttpStatus.OK);
}
@PutMapping("/id/{id}/upvote")
public Dataset upvote(@PathVariable("id") UUID id) {
public ResponseEntity<Dataset> upvote(@PathVariable("id") UUID id) {
if (datasetService.getDatasetById(id) == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
datasetService.upvoteDataset(id);
return datasetService.getDatasetById(id);
return new ResponseEntity<>(datasetService.getDatasetById(id), HttpStatus.OK);
}
@PutMapping("/id/{id}/downvote")
public Dataset downvote(@PathVariable("id") UUID id) {
public ResponseEntity<Dataset> downvote(@PathVariable("id") UUID id) {
if (datasetService.getDatasetById(id) == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
datasetService.downvoteDataset(id);
return getDatasetById(id); // new ResponseEntity<>(null, HttpStatus.OK);
return new ResponseEntity<>(datasetService.getDatasetById(id), HttpStatus.OK);
}
@PutMapping("/id/{id}/stars")
public Dataset postMethodName(@PathVariable("id") UUID id,
public ResponseEntity<Dataset> postMethodName(@PathVariable("id") UUID id,
@RequestParam("stars") int stars) {
if (stars > 0 && stars < 6) {
datasetService.voteDataset(id, stars);
if (datasetService.getDatasetById(id) == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return datasetService.getDatasetById(id);
if (!(stars > 0 && stars < 6)) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
datasetService.voteDataset(id, stars);
return new ResponseEntity<>(datasetService.getDatasetById(id), HttpStatus.OK);
}
@GetMapping
public Page<Dataset> getDatasetsByDateAfter(@RequestParam(value = "author", required = false) String author,
public Page<Dataset> getDatasetsByDateAfter(@RequestParam(value = "author", required = false) String author,
@RequestParam(value = "title", required = false) String title,
@RequestParam(value = "description", required = false) String description,
@RequestParam(value = "abst", required = false) String abst,
@ -87,21 +96,32 @@ public class DatasetController {
@RequestParam(value = "direction", required = false, defaultValue = "desc") String direction,
@RequestParam(value = "category", required = false) Category category) {
Pageable pageable = PageRequest.of(page, size,
Sort.by(direction.equals("asc") ? Sort.Direction.ASC : Sort.Direction.DESC, sort));
return datasetService.getDatasetsByOptionalCriteria(title, description, author, abst, type, rating, category,pageable);
Sort.by(Sort.Direction.fromString(direction), sort));
return datasetService.getDatasetsByOptionalCriteria(title, description, author, abst, type, rating, category,
pageable);
}
@GetMapping("/search")
public Page<Dataset> search(@RequestParam(value = "search", required = false, defaultValue = "%") String search,
public ResponseEntity<Page<Dataset>> search(
@RequestParam(value = "search", required = false, defaultValue = "%") String search,
@RequestParam(value = "page", required = false, defaultValue = "0") int page,
@RequestParam(value = "size", required = false, defaultValue = "20") int size,
@RequestParam(value = "sort", required = false, defaultValue = "upvotes") String sort,
@RequestParam(value = "direction", required = false, defaultValue = "desc") String direction,
@RequestParam(value = "category", required = false, defaultValue = "%") String category,
@RequestParam(value = "type", required = false, defaultValue = "%") String type){
Pageable pageable = PageRequest.of(page, size,
Sort.by(direction.equals("asc") ? Sort.Direction.ASC : Sort.Direction.DESC, sort));
return datasetService.searchByOptionalCriteria(search, category, type, pageable);
@RequestParam(value = "type", required = false, defaultValue = "%") String type) {
Pageable pageable = null;
if (!Dataset.getSort().contains(sort))
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
try {
pageable = PageRequest.of(page, size,
Sort.by(Sort.Direction.fromString(direction), sort));
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(datasetService.searchByOptionalCriteria(search, category, type, pageable),
HttpStatus.OK);
}
}
}

View File

@ -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;
}
}

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,20 @@
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);
@SuppressWarnings("null")
List<Category> findAll();
List<Category> findByName(String name);
@SuppressWarnings("null")
Optional<Category> findById(UUID id);
}

View File

@ -0,0 +1,34 @@
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) {
Category c = categoryRepository.getCategoryById(id);
if (c == null) {
return null;
}
return CategoryDtoMapper.toDto(c);
}
}

View File

@ -2,6 +2,8 @@ package de.uni_passau.fim.PADAS.group3.DataDash.model;
import java.net.URL;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.sql.Date;
@ -11,6 +13,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 +42,10 @@ public class Dataset {
private int upvotes;
private URL url;
@Enumerated(EnumType.STRING)
private static final List<String> sortable = Arrays.asList("author", "title", "upvotes", "date");
@ManyToOne
private Category categorie;
public Dataset(String title, String abst, String description, String author, URL url, Category categories, Type type) {
@ -109,6 +115,10 @@ public class Dataset {
return url;
}
public static List<String> getSort() {
return sortable;
}
public void setAbst(String abst) {
this.abst = abst.substring(0, Math.min(abst.length(), 100));
}

View File

@ -9,13 +9,14 @@ import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
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() {
@ -26,9 +27,9 @@ public class DatasetService {
return datasetRepository.getDatasetById(id);
}
public void addDataset(Dataset dataset) {
public Dataset addDataset(Dataset dataset) {
dataset.setDate(LocalDate.now());
datasetRepository.save(dataset);
return datasetRepository.save(dataset);
}
public void updateDatasetTitle(UUID id, String title) {
@ -89,15 +90,22 @@ public class DatasetService {
public Page<Dataset> getDatasetsByOptionalCriteria(String title, String description, String author, String abst,
Type type, Float raiting, Category category, Pageable pageable) {
return datasetRepository.findByOptionalCriteria(Optional.ofNullable(title), Optional.ofNullable(description),
Optional.ofNullable(author), Optional.ofNullable(abst), Optional.ofNullable(type), Optional.ofNullable(category),
Optional.ofNullable(author), Optional.ofNullable(abst), Optional.ofNullable(type),
Optional.ofNullable(category),
Optional.ofNullable(raiting), pageable);
}
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);
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);
}
}

View File

@ -15,18 +15,22 @@ public class LoadDummyDatabase {
private static final org.slf4j.Logger log = LoggerFactory.getLogger(LoadDummyDatabase.class);
@Bean
CommandLineRunner initDatabase(dataRepository repository) {
//@Bean
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());};

View File

@ -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);
}

View File

@ -1 +1,14 @@
spring.application.name=DataDash
# Datasource configuration
spring.datasource.url=jdbc:h2:mem:studentcoursedb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.sql.init.mode=always
spring.jpa.hibernate.ddl-auto=none
# Uncomment for web console
#spring.h2.console.enabled=true
#spring.h2.console.path=/h2-console
#spring.datasource.username=sa
#spring.datasource.password=pwd

View File

@ -0,0 +1,30 @@
-- Insert sample data into category
INSERT INTO category (id, name) VALUES
('123e4567-e89b-12d3-a456-426614174003', 'Business'),
('123e4567-e89b-12d3-a456-426614174004', 'Education'),
('123e4567-e89b-12d3-a456-426614174005', 'Sports'),
('123e4567-e89b-12d3-a456-426614174006', 'Entertainment'),
('123e4567-e89b-12d3-a456-426614174007', 'Art'),
('123e4567-e89b-12d3-a456-426614174000', 'Science'),
('123e4567-e89b-12d3-a456-426614174001', 'Technology'),
('123e4567-e89b-12d3-a456-426614174002', 'Health');
-- Insert sample data into dataset
INSERT INTO dataset (date, raiting, upvotes, votes, categorie_id, id, abst, author, description, title, url, type) VALUES
('2023-01-01', 4.5, 100, 120, '123e4567-e89b-12d3-a456-426614174000', '123e4567-e89b-12d3-a456-426614174100', 'Abstract 1', 'Author 1', 'Description 1', 'Title 1', 'http://example.com/1', 'API'),
('2023-01-02', 4.7, 150, 170, '123e4567-e89b-12d3-a456-426614174001', '123e4567-e89b-12d3-a456-426614174101', 'Abstract 2', 'Author 2', 'Description 2', 'Title 2', 'http://example.com/2', 'DATASET'),
('2023-01-03', 4.9, 200, 220, '123e4567-e89b-12d3-a456-426614174002', '123e4567-e89b-12d3-a456-426614174102', 'Abstract 3', 'Author 3', 'Description 3', 'Title 3', 'http://example.com/3', 'API'),
('2023-01-04', 4.2, 80, 100, '123e4567-e89b-12d3-a456-426614174003', '123e4567-e89b-12d3-a456-426614174103', 'Abstract 4', 'Author 4', 'Description 4', 'Title 4', 'http://example.com/4', 'DATASET'),
('2023-01-05', 4.6, 120, 140, '123e4567-e89b-12d3-a456-426614174004', '123e4567-e89b-12d3-a456-426614174104', 'Abstract 5', 'Author 5', 'Description 5', 'Title 5', 'http://example.com/5', 'API');
-- Insert 10 more sample data into dataset
INSERT INTO dataset (date, raiting, upvotes, votes, categorie_id, id, abst, author, description, title, url, type) VALUES
('2023-01-06', 4.8, 180, 200, '123e4567-e89b-12d3-a456-426614174005', '123e4567-e89b-12d3-a456-426614174105', 'Abstract 6', 'Author 6', 'Description 6', 'Title 6', 'http://example.com/6', 'API'),
('2023-01-07', 4.3, 90, 110, '123e4567-e89b-12d3-a456-426614174006', '123e4567-e89b-12d3-a456-426614174106', 'Abstract 7', 'Author 7', 'Description 7', 'Title 7', 'http://example.com/7', 'DATASET'),
('2023-01-08', 4.7, 150, 170, '123e4567-e89b-12d3-a456-426614174007', '123e4567-e89b-12d3-a456-426614174107', 'Abstract 8', 'Author 8', 'Description 8', 'Title 8', 'http://example.com/8', 'API'),
('2023-01-09', 4.9, 200, 220, '123e4567-e89b-12d3-a456-426614174000', '123e4567-e89b-12d3-a456-426614174108', 'Abstract 9', 'Author 9', 'Description 9', 'Title 9', 'http://example.com/9', 'DATASET'),
('2023-01-10', 4.2, 80, 100, '123e4567-e89b-12d3-a456-426614174001', '123e4567-e89b-12d3-a456-426614174109', 'Abstract 10', 'Author 10', 'Description 10', 'Title 10', 'http://example.com/10', 'API'),
('2023-11-11', 4.6, 120, 140, '123e4567-e89b-12d3-a456-426614174002', '123e4567-e89b-12d3-a456-426614174110', 'Abstract 11', 'Author 11', 'Description 11', 'Title 11', 'http://example.com/11', 'DATASET'),
('2023-09-12', 4.8, 180, 200, '123e4567-e89b-12d3-a456-426614174003', '123e4567-e89b-12d3-a456-426614174111', 'Abstract 12', 'Author 12', 'Description 12', 'Title 12', 'http://example.com/12', 'API'),
('2023-03-13', 4.3, 90, 110, '123e4567-e89b-12d3-a456-426614174004', '123e4567-e89b-12d3-a456-426614174112', 'Abstract 13', 'Author 13', 'Description 13', 'Title 13', 'http://example.com/13', 'DATASET'),
('2021-01-14', 4.7, 150, 170, '123e4567-e89b-12d3-a456-426614174005', '123e4567-e89b-12d3-a456-426614174113', 'Abstract 14', 'Author 14', 'Description 14', 'Title 14', 'http://example.com/14', 'API'),
('2024-01-15', 4.9, 200, 220, '123e4567-e89b-12d3-a456-426614174006', '123e4567-e89b-12d3-a456-426614174114', 'Abstract 15', 'Author 15', 'Description 15', 'Title 15', 'http://example.com/15', 'DATASET');

View File

@ -0,0 +1,7 @@
DROP TABLE IF EXISTS dataset;
DROP TABLE IF EXISTS category;
create table category (id uuid not null, name varchar(255), primary key (id));
create table dataset (date date, raiting float(24) not null, upvotes integer not null, votes integer not null, categorie_id uuid, id uuid not null, abst varchar(255), author varchar(255), description varchar(255), title varchar(255), url varchar(255), type enum ('API','DATASET'), primary key (id));
alter table if exists dataset add constraint FKq6qwq6u473f89h71s7rf97ruy foreign key (categorie_id) references category;