updating the competencies
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
package org.zaine.app.controller;
|
||||
|
||||
import java.lang.System.Logger;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.zaine.app.dto.CompetenciesDTO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.zaine.app.model.Competencies;
|
||||
import org.zaine.app.service.CompetenciesService;
|
||||
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/competencies")
|
||||
public class CompetenciesController {
|
||||
|
||||
private static final Logger logger = System.getLogger(CompetenciesController.class.getName());
|
||||
|
||||
@Autowired
|
||||
private CompetenciesService competenciesService;
|
||||
|
||||
@GetMapping("/items")
|
||||
public List<Competencies> getAllCompetencies() {
|
||||
|
||||
return competenciesService.getAllCompetencies();
|
||||
}
|
||||
public List<Competencies> getAllCompetencies(
|
||||
@RequestParam(value = "group", required = false) String group) {
|
||||
if (group != null && !group.isBlank()) {
|
||||
return competenciesService.getCompetenciesByGroup(group);
|
||||
}
|
||||
return competenciesService.getAllCompetencies();
|
||||
}
|
||||
|
||||
@GetMapping("/item/{id}")
|
||||
public Competencies getCompetencyById(@PathVariable Integer id) {
|
||||
return competenciesService.getCompetencyById(id);
|
||||
return competenciesService.getCompetencyById(id);
|
||||
}
|
||||
|
||||
@PostMapping(
|
||||
@@ -45,5 +46,4 @@ public class CompetenciesController {
|
||||
) {
|
||||
competenciesService.updateCompetencyState(id, request.getState());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package org.zaine.app.repositories;
|
||||
|
||||
import java.util.List;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.zaine.app.model.Competencies;
|
||||
|
||||
|
||||
public interface CompetenciesRepository extends JpaRepository<Competencies, Integer> {
|
||||
|
||||
List<Competencies> findByGroup(String group);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
package org.zaine.app.service;
|
||||
|
||||
import java.lang.System.Logger;
|
||||
import java.util.List;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -8,45 +7,37 @@ import org.zaine.app.repositories.CompetenciesRepository;
|
||||
|
||||
@Service
|
||||
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 =
|
||||
List.of("completed", "manager_review", "in_progress", "not_started", "comments");
|
||||
public List<Competencies> getCompetenciesByGroup(String group) {
|
||||
return competenciesRepository.findByGroup(group);
|
||||
}
|
||||
|
||||
|
||||
public CompetenciesService(CompetenciesRepository competenciesRepository) {
|
||||
this.competenciesRepository = competenciesRepository;
|
||||
}
|
||||
|
||||
public List<Competencies> getAllCompetencies() {
|
||||
return competenciesRepository.findAll();
|
||||
|
||||
}
|
||||
|
||||
public Competencies getCompetencyById(Integer id) {
|
||||
|
||||
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);
|
||||
}
|
||||
public Competencies getCompetencyById(Integer id) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user