Skip to content

Commit

Permalink
Removed lots of unused code (especially unused API endpoints)
Browse files Browse the repository at this point in the history
  • Loading branch information
Niklas345 committed Nov 13, 2023
1 parent 6dd95f5 commit ab70916
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 162 deletions.
50 changes: 2 additions & 48 deletions src/main/java/de/hbt/power/controller/CategoryController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package de.hbt.power.controller;

import de.hbt.power.exception.SkillServiceException;
import de.hbt.power.model.Skill;
import de.hbt.power.model.SkillCategory;
import de.hbt.power.repo.SkillCategoryRepository;
import de.hbt.power.repo.SkillRepository;
import de.hbt.power.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
Expand All @@ -29,15 +27,13 @@
allowCredentials = "true")
public class CategoryController {
private final SkillCategoryRepository skillCategoryRepository;
private final SkillRepository skillRepository;

private final CategoryService categoryService;


@Autowired
CategoryController(SkillCategoryRepository skillCategoryRepository, SkillRepository skillRepository, CategoryService categoryService, SkillRepository skillRepository1, CategoryService categoryService1) {
CategoryController(SkillCategoryRepository skillCategoryRepository, CategoryService categoryService1) {
this.skillCategoryRepository = skillCategoryRepository;
this.skillRepository = skillRepository1;
this.categoryService = categoryService1;
}

Expand All @@ -48,25 +44,6 @@ public ResponseEntity<List<Integer>> getAllCategoryIds() {
return ResponseEntity.ok(categories);
}

/**
* Returns all root categories -> categories without a parent
*/
@ApiOperation(value = "Returns all root IDs",
notes = "Returns all available root categories ids. A root category has no direct parent. It might happen that the" +
"returned list is empty, indicating a server or database problem. (The list should not be empty, there are always root categories.)",
response = Integer.class,
responseContainer = "List",
httpMethod = "GET",
produces = "application/json")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Returns the root category ids in response. May be empty.", response = List.class),
})
@GetMapping(value = "/root")
public ResponseEntity<List<Integer>> getRootCategoryIds() {
final List<Integer> ids = skillCategoryRepository.findAllByCategoryIsNull().stream().map(SkillCategory::getId).collect(Collectors.toList());
return ResponseEntity.ok(ids);
}

@ApiOperation(value = "Returns all child IDs",
notes = "For a given category ID, returns the IDs of all categories that are direct children to this category.",
response = Integer.class,
Expand All @@ -86,21 +63,6 @@ public ResponseEntity<List<Integer>> getChildrenIds(@PathVariable("id") Integer
return ResponseEntity.ok(childIds);
}

@ApiOperation(value = "Returns all child Skills",
notes = "For a given category ID, returns all skills that are direct children to that category.",
response = Skill.class,
responseContainer = "List",
httpMethod = "GET",
produces = "application/json")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Child Skills returned in response. May be empty.")
})
@GetMapping(value = "/{id}/skills")
public ResponseEntity<List<Skill>> getChildSkills(@PathVariable("id") Integer id) {
return ResponseEntity.ok(skillRepository.findAllByCategory_Id(id));
}


@ApiOperation(value = "Returns a category",
notes = "Returns the category identified by the given ID.",
response = SkillCategory.class,
Expand Down Expand Up @@ -167,7 +129,7 @@ public ResponseEntity<SkillCategory> createCategory(@RequestBody() SkillCategory
})
@DeleteMapping(value = "/{id}")
@Transactional
public ResponseEntity deleteCategory(@PathVariable("id") Integer categoryId) {
public ResponseEntity<Void> deleteCategory(@PathVariable("id") Integer categoryId) {
SkillCategory skillCategory = getCategory(categoryId);

log.info("Deleting the " + skillCategory.toString());
Expand Down Expand Up @@ -312,14 +274,6 @@ public ResponseEntity<SkillCategory> deleteFromBlackList(@PathVariable("id") Int
return ResponseEntity.ok(skillCategory);
}


@GetMapping(value = "/blacklist")
public ResponseEntity<List<Integer>> getAllBlacklistedCategoryIds() {
List<SkillCategory> blacklisted = skillCategoryRepository.findAllByBlacklistedTrue();
List<Integer> ids = blacklisted.stream().map(SkillCategory::getId).collect(Collectors.toList());
return ResponseEntity.ok(ids);
}

private SkillCategory getCategory(Integer categoryId) {
return skillCategoryRepository.findById(categoryId)
.orElseThrow(() -> SkillServiceException.categoryNotFound(categoryId));
Expand Down
62 changes: 10 additions & 52 deletions src/main/java/de/hbt/power/controller/SkillController.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand All @@ -31,11 +30,6 @@
*/
@RestController()
@RequestMapping("/skill")
/*
@CrossOrigin(origins = "*",
methods = {RequestMethod.PUT, RequestMethod.GET, RequestMethod.PATCH, RequestMethod.POST, RequestMethod.DELETE},
allowedHeaders = {"origin", "content-type", "accept", "authorization", "X-Requested-With"},
allowCredentials = "true")*/
@CrossOrigin
@Log4j2
public class SkillController {
Expand Down Expand Up @@ -94,26 +88,6 @@ public ResponseEntity<Skill> moveSkill(@PathVariable("id") Integer skillId, @Pat
return ResponseEntity.ok(skill);
}


@ApiOperation(value = "Returns all skill IDs", response = Integer.class, responseContainer = "List")
@GetMapping
public ResponseEntity<List<Integer>> getAllSkillIds() {
final List<Integer> result = new ArrayList<>();
skillRepository.findAll().forEach(skill -> result.add(skill.getId()));
return ResponseEntity.ok(result);
}


@ApiOperation(value = "Updates a skill", response = Skill.class)
@PutMapping("/{id}")
public ResponseEntity<Skill> updateSkill(@PathVariable Integer id, @RequestBody Skill skill) {
Skill s = requireSkill(id);
skill.setId(s.getId());
skillRepository.save(s);
return ResponseEntity.ok().build();
}


@ApiOperation(value = "Searches and returns a skill by qualifier", response = Skill.class)
@GetMapping("/byName")
public ResponseEntity<Skill> findSkillByQualifier(@RequestParam("qualifier") String qualifier) {
Expand All @@ -134,22 +108,14 @@ public ResponseEntity<Skill> findSkillByQualifier(@RequestParam("qualifier") Str
@ApiOperation(value = "Fuzzy search for skill names", response = String.class, responseContainer = "List")
@GetMapping(value = "/search")
public ResponseEntity<List<String>> searchSkill(@RequestParam("searchterm") String searchTerm, @RequestParam Integer maxResults) {
if (maxResults == null) maxResults = DEFAULT_MAX_RESULTS;
if (maxResults == null) {
maxResults = DEFAULT_MAX_RESULTS;
}
List<String> suggestions = skillSearcherService.searchSkill(searchTerm, maxResults);
return ResponseEntity.ok(suggestions);
}


@ApiOperation(value = "Attempty to automatically group a skill into a category", response = Skill.class)
@PatchMapping("/{id}")
public ResponseEntity<Skill> categorizeSkill(@PathVariable Integer id) {
Skill responseSkill = requireSkill(id);
log.info("Categorizing " + responseSkill.toString());
responseSkill = categoryService.categorizeSkill(responseSkill);
return ResponseEntity.ok(responseSkill);
}


/**
* Returns the category for the requested SkillId, categorizes the Skill if no SkillCategory was set before.
*
Expand All @@ -159,12 +125,10 @@ public ResponseEntity<Skill> categorizeSkill(@PathVariable Integer id) {
@PostMapping
public ResponseEntity<SkillCategory> updateAndGetCategory(@RequestParam("qualifier") String qualifier) {
Skill skill = skillRepository.findOneByQualifier(qualifier)
.orElseGet(() -> skillService.createSkill(qualifier));
if (skill.getCategory() == null) {
log.info("Categorizing " + skill.toString());
skill = categoryService.categorizeSkill(skill);
}
skillRepository.save(skill);
.orElseGet(() -> {
Skill toCreate = Skill.of(qualifier);
return skillService.createSkillInCategory(toCreate, categoryService.getOther());
});
return ResponseEntity.ok(skill.getCategory());
}

Expand All @@ -185,7 +149,7 @@ public ResponseEntity<Skill> createSkillInCategory(@RequestBody Skill skill, @Pa
if (concurrent.isPresent()) {
throw SkillServiceException.skillAlreadyExists(concurrent.get());
}
log.info("Creating " + skill.toString() + " in " + skillCategory.toString());
log.info("Creating " + skill + " in " + skillCategory.toString());
skill.setId(null);
skillService.createSkillInCategory(skill, skillCategory);
return ResponseEntity.ok(skill);
Expand All @@ -194,7 +158,7 @@ public ResponseEntity<Skill> createSkillInCategory(@RequestBody Skill skill, @Pa

@ApiOperation(value = "Permanently deletes a skill")
@DeleteMapping("/{id}")
public ResponseEntity deleteSkill(@PathVariable("id") Integer skillId) {
public ResponseEntity<Void> deleteSkill(@PathVariable("id") Integer skillId) {
skillRepository.findById(skillId)
.map(peek(s -> log.info("Deleting skill " + s)))
.ifPresent(skillRepository::delete);
Expand Down Expand Up @@ -231,7 +195,7 @@ public ResponseEntity<Set<String>> addVersion(@PathVariable("id") Integer skillI

@ApiOperation(value = "Deletes a version from a skill")
@DeleteMapping("{id}/version")
public ResponseEntity deleteVersion(@PathVariable("id") Integer skillId, @RequestBody String version) {
public ResponseEntity<Void> deleteVersion(@PathVariable("id") Integer skillId, @RequestBody String version) {
Skill skill = requireSkill(skillId);
skillService.deleteVersion(skill, version);
return ResponseEntity.ok().build();
Expand All @@ -242,10 +206,4 @@ public ResponseEntity deleteVersion(@PathVariable("id") Integer skillId, @Reques
public ResponseEntity<TCategoryNode> getTree() {
return ResponseEntity.ok(skillTreeMappingService.buildSkillTree());
}

@ApiOperation(value = "Returns a model of the skill tree. Returned value is the root node.", response = TCategoryNode.class)
@GetMapping("/tree/debug")
public ResponseEntity<TCategoryNode> getTreeDebug() {
return ResponseEntity.ok(skillTreeMappingService.buildSkillTreeDebug());
}
}
7 changes: 0 additions & 7 deletions src/main/java/de/hbt/power/repo/SkillCategoryRepository.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.hbt.power.repo;

import de.hbt.power.model.SkillCategory;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -12,12 +11,6 @@
public interface SkillCategoryRepository extends JpaRepository<SkillCategory, Integer> {
Optional<SkillCategory> findOneByQualifier(String qualifier);

List<SkillCategory> findAllByCategoryIsNull();

List<SkillCategory> findAllByCategory(SkillCategory skillCategory);

List<SkillCategory> findAllByBlacklistedTrue();
List<SkillCategory> findTop10ById(Integer id);
List<SkillCategory> findFirst10ByOrderById();

}
11 changes: 0 additions & 11 deletions src/main/java/de/hbt/power/repo/SkillRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import de.hbt.power.model.Skill;
import de.hbt.power.model.SkillCategory;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
Expand All @@ -15,18 +14,8 @@
public interface SkillRepository extends JpaRepository<Skill, Integer>, CrudRepository<Skill, Integer> {
Optional<Skill> findOneByQualifier(String qualifier);

Optional<Skill> getByQualifiersContaining(String localizedQualifiers);

List<Skill> findAllByCategory_Id(Integer categoryId);

List<Skill> findAllByCategory(SkillCategory category);

void deleteAllByCategory(SkillCategory category);

List<Skill> findTop100ById(Integer id);

List<Skill> findFirst50ByOrderById();

@Query("select s from Skill s WHERE not s.category is null")
List<Skill> findForTree();
}
8 changes: 2 additions & 6 deletions src/main/java/de/hbt/power/service/CategoryService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.hbt.power.service;

import de.hbt.power.exception.SkillServiceException;
import de.hbt.power.model.Skill;
import de.hbt.power.model.SkillCategory;
import de.hbt.power.repo.SkillCategoryRepository;
import de.hbt.power.repo.SkillRepository;
Expand Down Expand Up @@ -92,12 +91,9 @@ public void setIsDisplay(SkillCategory skillCategory, Boolean isDisplay) {
}


public Skill categorizeSkill(Skill toCategorize) {
// Skill Provider has been removed, for now, we are simply assuming 'Other'
SkillCategory other = skillCategoryRepository.findOneByQualifier(OTHER_CATEGORY_NAME)
public SkillCategory getOther() {
return skillCategoryRepository.findOneByQualifier(OTHER_CATEGORY_NAME)
.orElseThrow(() -> new RuntimeException("Category 'Other' is missing. This should not happen!"));
toCategorize.setCategory(other);
return toCategorize;
}

public SkillCategory createSkillCategory(SkillCategory category, Integer parentId) {
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/de/hbt/power/service/SkillService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ public Skill moveSkillToCategory(Skill toMove, SkillCategory newParent) {
return toMove;
}

public Skill createSkill(String name) {
Skill skill = new Skill();
skill.setCustom(true);
skill.setQualifier(name);
return skillRepository.save(skill);
}

@Transactional
public Skill createSkillInCategory(Skill skill, SkillCategory skillCategory) {
skill.setCustom(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ public TCategoryNode buildSkillTree() {
return buildSkillTree(skillCategoryRepository.findAll(), skillRepository.findForTree());
}

public TCategoryNode buildSkillTreeDebug() {
return buildSkillTree(skillCategoryRepository.findAll(), skillRepository.findAll());
}

public TCategoryNode buildSkillTree(List<SkillCategory> categories, List<Skill> skills) {
log.info("categories: " + categories.size() + " skills: " + skills.size());
return new SkillTreeMapper(categories, skills).map();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@ public void setUp() {
skillToTest.setQualifier("Test");
}

@Test
@DirtiesContext
public void testCategorizeSkill() {
skillCategoryRepository.save(SkillCategory.builder().qualifier("Other").build());
Skill result = categoryService.categorizeSkill(skillToTest);
assertThat(result.getCategory().getQualifier()).isEqualTo("Other");
}

@Test
@DirtiesContext
@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@
@ActiveProfiles("test")
public class SkillSearcherService_ExpectedResultITest {

@Autowired
private SkillService skillService;

@Autowired
private SkillRepository skillRepository;

@Autowired
private SkillSearcherService skillSearcherService;

private Skill persistentSkill(String name) {
return skillService.createSkill(name);
private void persistentSkill(String name) {
skillRepository.save(Skill.of(name));
}

@Test
Expand Down
Loading

0 comments on commit ab70916

Please sign in to comment.