Commit 6c6bc027 authored by Samuel Kulisek's avatar Samuel Kulisek Committed by Samuel Kulíšek
Browse files

Add rest api for Agents by Country and Skill, Change Agent api to search by...

Add rest api for Agents by Country and Skill, Change Agent api to search by Skill ID instead of name
parent e02a3aa9
Pipeline #140837 passed with stage
in 2 minutes and 1 second
......@@ -12,8 +12,9 @@ public interface AgentFacade {
Page<AgentListDto> getMissionAgents(Long missionId, int page, int pageSize);
AgentDetailDto getAgent(Long agentId);
AgentCountDto getAgentCount (Long missionId);
Page<AgentListDto> getAgentWithSkill(String skill, int page, int pageSize);
Page<AgentListDto> getAgentWithSkill(Long skillId, int page, int pageSize);
Page<AgentListDto> getAgentWithCountryXp(Long countryId, int page, int pageSize);
Page<AgentListDto> getAgentWithCountryXpAndSkill(Long countryId, Long skillId, int page, int pageSize);
Page<AgentListDto> getAllAgents(int page, int pageSize);
Page<SkillDto> getAllSkills(int page, int pageSize);
List<SkillDto> getAllSkills();
......
......@@ -77,11 +77,11 @@ public class AgentController {
}
@Operation(description = "Retrieves agents with specified skill")
@GetMapping("/skills/{skill}")
public final ResponseEntity<ApiResponse> getAgentWithSkill(@PathVariable String skill, int page, int pageSize) {
@GetMapping("/skills/{skillId}")
public final ResponseEntity<ApiResponse> getAgentWithSkill(@PathVariable Long skillId, int page, int pageSize) {
//TODO logged in user logic
try {
Page<AgentListDto> result = agentFacade.getAgentWithSkill(skill, page, pageSize);
Page<AgentListDto> result = agentFacade.getAgentWithSkill(skillId, page, pageSize);
ApiResponse response = new ApiResponse();
response.setData(result.getContent());
......@@ -114,6 +114,25 @@ public class AgentController {
}
}
@Operation(description = "Retrieves agents with experience from specified country and a specified skill")
@GetMapping("/countries/{countryId}/skills/{skillId}")
public final ResponseEntity<ApiResponse> getAgentWithCountryXpAndSkill(@PathVariable long countryId, @PathVariable long skillId, int page, int pageSize) {
//TODO logged in user logic
try {
Page<AgentListDto> result = agentFacade.getAgentWithCountryXpAndSkill(countryId, skillId, page, pageSize);
ApiResponse response = new ApiResponse();
response.setData(result.getContent());
response.setTotalElementCount(result.getTotalElements());
response.setTotalPageCount(result.getTotalPages());
return ResponseEntity.ok().body(response);
} catch (Exception e) {
LOGGER.error("Could not retrieve agents with experience from specified country or the specified skill", e);
return ResponseEntity.notFound().build();
}
}
@Operation(description = "Retrieves all agents in the archive")
@GetMapping("/allAgents")
public final ResponseEntity<ApiResponse> getAllAgents(int page, int pageSize) {
......
......@@ -84,10 +84,10 @@ public class AgentFacadeImpl implements AgentFacade {
}
@Override
public Page<AgentListDto> getAgentWithSkill(String skill, int page, int pageSize) {
public Page<AgentListDto> getAgentWithSkill(Long skillId, int page, int pageSize) {
Pageable pageable = PageRequest.of(page, pageSize);
Skill foundSkill = skillService.findByName(skill);
Skill foundSkill = skillService.findById(skillId);
Page<Agent> agents = agentService.findAllBySkill(foundSkill, pageable);
......@@ -116,6 +116,30 @@ public class AgentFacadeImpl implements AgentFacade {
return createPage(agentsWithXpList, pageable);
}
@Override
public Page<AgentListDto> getAgentWithCountryXpAndSkill(Long countryId, Long skillId, int page, int pageSize) {
Country country = countryService.findById(countryId);
List<Mission> missions = missionService.
findAll()
.stream()
.filter(mission -> mission.getCountry().equals(country))
.collect(Collectors.toList());
Set<Agent> agentsWithXp = new HashSet<>();
for (Mission mission : missions) {
Set<Agent> missionAgents = mission.getAssignments().stream().map(AgentMissionAssignment::getAgent).collect(Collectors.toSet());
agentsWithXp.addAll(missionAgents);
}
Skill skill = skillService.findById(skillId);
List<Agent> filteredAgents = agentsWithXp
.stream()
.filter(agent -> agent.getSkills().contains(skill))
.collect(Collectors.toList());
return createPage(filteredAgents, PageRequest.of(page, pageSize));
}
@Override
public Page<AgentListDto> getAllAgents(int page, int pageSize) {
Pageable pageable = PageRequest.of(page, pageSize);
......
......@@ -145,16 +145,16 @@ public class AgentFacadeTest {
skill.setName("German");
skill.setType(SkillType.LANGUAGE);
skill.setId(1L);
Mockito.doReturn(skill).when(skillService).findByName("German");
Mockito.doReturn(skill).when(skillService).findById(1L);
Mockito.doReturn(new PageImpl<>(List.of(), pageable, 0)).when(agentService).findAllBySkill(skill, PageRequest.of(0,5));
Assertions.assertThat(agentFacade.getAgentWithSkill("German",0,5)).isEmpty();
Assertions.assertThat(agentFacade.getAgentWithSkill(1L,0,5)).isEmpty();
agent4.setSkills(Set.of(skill));
agent2.setSkills(Set.of(skill));
Mockito.doReturn(new PageImpl<>(List.of(agent2, agent4), pageable, 2)).when(agentService).findAllBySkill(skill, PageRequest.of(0,5));
SkillDto skillDto = new SkillDto(1L,"German",null,SkillType.LANGUAGE);
AgentListDto agentDto2 = new AgentListDto(2L,"Maceta", List.of(skillDto), new ArrayList<>());
AgentListDto agentDto4 = new AgentListDto(4L,"Delta", List.of(skillDto), new ArrayList<>());
Assertions.assertThat(agentFacade.getAgentWithSkill("German",0,5)).hasSize(2).hasSameElementsAs(List.of(agentDto2,agentDto4));
Assertions.assertThat(agentFacade.getAgentWithSkill(1L,0,5)).hasSize(2).hasSameElementsAs(List.of(agentDto2,agentDto4));
}
@Test
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment