updating the competencies

This commit is contained in:
2026-03-08 16:37:57 +00:00
parent a225ab2993
commit 4a2171c38d
3 changed files with 40 additions and 48 deletions

View File

@@ -1,9 +1,7 @@
package org.zaine.app.controller; package org.zaine.app.controller;
import java.lang.System.Logger; import java.lang.System.Logger;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.zaine.app.dto.CompetenciesDTO; import org.zaine.app.dto.CompetenciesDTO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@@ -11,28 +9,31 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.zaine.app.model.Competencies; import org.zaine.app.model.Competencies;
import org.zaine.app.service.CompetenciesService; import org.zaine.app.service.CompetenciesService;
@RestController @RestController
@RequestMapping("/api/competencies") @RequestMapping("/api/competencies")
public class CompetenciesController { public class CompetenciesController {
private static final Logger logger = System.getLogger(CompetenciesController.class.getName()); private static final Logger logger = System.getLogger(CompetenciesController.class.getName());
@Autowired @Autowired
private CompetenciesService competenciesService; private CompetenciesService competenciesService;
@GetMapping("/items") @GetMapping("/items")
public List<Competencies> getAllCompetencies() { public List<Competencies> getAllCompetencies(
@RequestParam(value = "group", required = false) String group) {
return competenciesService.getAllCompetencies(); if (group != null && !group.isBlank()) {
} return competenciesService.getCompetenciesByGroup(group);
}
return competenciesService.getAllCompetencies();
}
@GetMapping("/item/{id}") @GetMapping("/item/{id}")
public Competencies getCompetencyById(@PathVariable Integer id) { public Competencies getCompetencyById(@PathVariable Integer id) {
return competenciesService.getCompetencyById(id); return competenciesService.getCompetencyById(id);
} }
@PostMapping( @PostMapping(
@@ -45,5 +46,4 @@ public class CompetenciesController {
) { ) {
competenciesService.updateCompetencyState(id, request.getState()); competenciesService.updateCompetencyState(id, request.getState());
} }
} }

View File

@@ -1,9 +1,10 @@
package org.zaine.app.repositories; package org.zaine.app.repositories;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.zaine.app.model.Competencies; import org.zaine.app.model.Competencies;
public interface CompetenciesRepository extends JpaRepository<Competencies, Integer> { public interface CompetenciesRepository extends JpaRepository<Competencies, Integer> {
List<Competencies> findByGroup(String group);
} }

View File

@@ -1,5 +1,4 @@
package org.zaine.app.service; package org.zaine.app.service;
import java.lang.System.Logger; import java.lang.System.Logger;
import java.util.List; import java.util.List;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -8,45 +7,37 @@ import org.zaine.app.repositories.CompetenciesRepository;
@Service @Service
public class CompetenciesService { public class CompetenciesService {
private static final Logger logger = System.getLogger(CompetenciesService.class.getName());
private final CompetenciesRepository competenciesRepository;
private static final List<String> ALLOWED_STATES =
List.of("completed", "manager_review", "in_progress", "not_started", "comments");
private static final Logger logger = System.getLogger(CompetenciesService.class.getName()); public CompetenciesService(CompetenciesRepository competenciesRepository) {
this.competenciesRepository = competenciesRepository;
}
private final CompetenciesRepository competenciesRepository; public List<Competencies> getAllCompetencies() {
return competenciesRepository.findAll();
}
private static final List<String> ALLOWED_STATES = public List<Competencies> getCompetenciesByGroup(String group) {
List.of("completed", "manager_review", "in_progress", "not_started", "comments"); return competenciesRepository.findByGroup(group);
}
public Competencies getCompetencyById(Integer id) {
logger.log(System.Logger.Level.INFO, "Entering getCompetencyById with id: " + id);
return competenciesRepository.findById(id).get();
}
public CompetenciesService(CompetenciesRepository competenciesRepository) { public void updateCompetencyState(Integer id, String newState) {
this.competenciesRepository = competenciesRepository; if (newState == null) {
} throw new IllegalArgumentException("State must not be null");
}
public List<Competencies> getAllCompetencies() { if (!ALLOWED_STATES.contains(newState)) {
return competenciesRepository.findAll(); throw new IllegalArgumentException("Invalid state: " + newState);
}
} Competencies competency = getCompetencyById(id);
competency.setState(newState);
public Competencies getCompetencyById(Integer id) { competenciesRepository.save(competency);
}
logger.log(System.Logger.Level.INFO, "Entering getCompetencyById with id: " + id);
return competenciesRepository.findById(id).get();
}
public void updateCompetencyState(Integer id, String newState) {
if (newState == null) {
throw new IllegalArgumentException("State must not be null");
}
if (!ALLOWED_STATES.contains(newState)) {
throw new IllegalArgumentException("Invalid state: " + newState);
}
Competencies competency = getCompetencyById(id);
competency.setState(newState);
competenciesRepository.save(competency);
}
} }