Commit cf95dea6 authored by Samuel Kulisek's avatar Samuel Kulisek
Browse files

Add ApiResponse class, Wrap Pages returned from rest api with ApiResponse...

Add ApiResponse class, Wrap Pages returned from rest api with ApiResponse containing collection info
parent cf5ffaa5
Pipeline #140702 passed with stage
in 1 minute and 29 seconds
package cz.muni.fi.pa165.rest;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class ApiResponse {
private Object data;
private int totalPageCount;
private long totalElementCount;
}
......@@ -5,12 +5,14 @@ import cz.muni.fi.pa165.dto.AgentDetailDto;
import cz.muni.fi.pa165.dto.AgentListDto;
import cz.muni.fi.pa165.dto.SkillDto;
import cz.muni.fi.pa165.facade.AgentFacade;
import cz.muni.fi.pa165.rest.ApiResponse;
import cz.muni.fi.pa165.rest.ApiUris;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -33,10 +35,17 @@ public class AgentController {
@Operation(description = "Retrieve all agents of specified mission")
@GetMapping("/missions/{missionId}")
public final ResponseEntity<List<AgentListDto>> getMissionAgents(@PathVariable long missionId, int page, int pageSize) {
public final ResponseEntity<ApiResponse> getMissionAgents(@PathVariable long missionId, int page, int pageSize) {
//TODO logged in user logic
try {
return ResponseEntity.ok().body(agentFacade.getMissionAgents(missionId, page, pageSize).getContent());
Page<AgentListDto> result = agentFacade.getMissionAgents(missionId, 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 of specified mission", e);
return ResponseEntity.notFound().build();
......@@ -69,10 +78,17 @@ public class AgentController {
@Operation(description = "Retrieves agents with specified skill")
@GetMapping("/skills/{skill}")
public final ResponseEntity<List<AgentListDto>> getAgentWithSkill(@PathVariable String skill, int page, int pageSize) {
public final ResponseEntity<ApiResponse> getAgentWithSkill(@PathVariable String skill, int page, int pageSize) {
//TODO logged in user logic
try {
return ResponseEntity.ok().body(agentFacade.getAgentWithSkill(skill, page, pageSize).getContent());
Page<AgentListDto> result = agentFacade.getAgentWithSkill(skill, 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 specified skill", e);
return ResponseEntity.notFound().build();
......@@ -81,10 +97,17 @@ public class AgentController {
@Operation(description = "Retrieves agents with experience from specified country")
@GetMapping("/countries/{countryId}")
public final ResponseEntity<List<AgentListDto>> getAgentWithCountryXp(@PathVariable long countryId, int page, int pageSize) {
public final ResponseEntity<ApiResponse> getAgentWithCountryXp(@PathVariable long countryId, int page, int pageSize) {
//TODO logged in user logic
try {
return ResponseEntity.ok().body(agentFacade.getAgentWithCountryXp(countryId, page, pageSize).getContent());
Page<AgentListDto> result = agentFacade.getAgentWithCountryXp(countryId, 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", e);
return ResponseEntity.notFound().build();
......@@ -93,10 +116,17 @@ public class AgentController {
@Operation(description = "Retrieves all agents in the archive")
@GetMapping("/allAgents")
public final ResponseEntity<List<AgentListDto>> getAllAgents(int page, int pageSize) {
public final ResponseEntity<ApiResponse> getAllAgents(int page, int pageSize) {
//TODO logged in user logic
try {
return ResponseEntity.ok().body(agentFacade.getAllAgents(page, pageSize).getContent());
Page<AgentListDto> result = agentFacade.getAllAgents(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 all agents", e);
return ResponseEntity.notFound().build();
......@@ -105,18 +135,32 @@ public class AgentController {
@Operation(description = "Retrieves all skills of all agents in the archive")
@GetMapping("/allSkills")
public final ResponseEntity<List<SkillDto>> getAllSkills(int page, int pageSize) {
public final ResponseEntity<ApiResponse> getAllSkills(int page, int pageSize) {
//TODO logged in user logic
if (pageSize == 0) {
try {
return ResponseEntity.ok().body(agentFacade.getAllSkills());
List<SkillDto> result = agentFacade.getAllSkills();
ApiResponse response = new ApiResponse();
response.setData(result);
response.setTotalElementCount(result.size());
response.setTotalPageCount(1);
return ResponseEntity.ok().body(response);
} catch (Exception e) {
LOGGER.error("Could not retrieve all skills", e);
return ResponseEntity.notFound().build();
}
}
try {
return ResponseEntity.ok().body(agentFacade.getAllSkills(page, pageSize).getContent());
Page<SkillDto> result = agentFacade.getAllSkills(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 all skills", e);
return ResponseEntity.notFound().build();
......
......@@ -3,13 +3,16 @@ package cz.muni.fi.pa165.rest.controller;
import cz.muni.fi.pa165.dto.CountryDetailDto;
import cz.muni.fi.pa165.dto.CountryNameDto;
import cz.muni.fi.pa165.dto.MissionListDto;
import cz.muni.fi.pa165.dto.SkillDto;
import cz.muni.fi.pa165.facade.CountryFacade;
import cz.muni.fi.pa165.rest.ApiResponse;
import cz.muni.fi.pa165.rest.ApiUris;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -56,18 +59,32 @@ public class CountryController {
@Operation(description = "Retrieves all countries in the archive")
@GetMapping("/allCountries")
public final ResponseEntity<List<CountryNameDto>> getAllCountries(int page, int pageSize) {
public final ResponseEntity<ApiResponse> getAllCountries(int page, int pageSize) {
//TODO logged in user logic
if (pageSize == 0) {
try {
return ResponseEntity.ok().body(countryFacade.getAllCountries());
List<CountryNameDto> result = countryFacade.getAllCountries();
ApiResponse response = new ApiResponse();
response.setData(result);
response.setTotalElementCount(result.size());
response.setTotalPageCount(1);
return ResponseEntity.ok().body(response);
} catch (Exception e) {
LOGGER.error("Could not retrieve all countries", e);
return ResponseEntity.notFound().build();
}
}
try {
return ResponseEntity.ok().body(countryFacade.getAllCountries(page, pageSize).getContent());
Page<CountryNameDto> result = countryFacade.getAllCountries(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 all countries", e);
return ResponseEntity.notFound().build();
......
package cz.muni.fi.pa165.rest.controller;
import cz.muni.fi.pa165.dto.CountryNameDto;
import cz.muni.fi.pa165.dto.MissionDetailDto;
import cz.muni.fi.pa165.dto.MissionListDto;
import cz.muni.fi.pa165.dto.SkillDto;
import cz.muni.fi.pa165.facade.MissionFacade;
import cz.muni.fi.pa165.rest.ApiResponse;
import cz.muni.fi.pa165.rest.ApiUris;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -44,10 +47,17 @@ public class MissionController {
@Operation(description = "Returns missions of a specific agent")
@GetMapping("/{agentId}/missions")
public final ResponseEntity<List<MissionListDto>> getAgentMissions(@PathVariable long agentId, int page, int size) {
public final ResponseEntity<ApiResponse> getAgentMissions(@PathVariable long agentId, int page, int size) {
//TODO logged in user logic
try {
return ResponseEntity.ok().body(missionFacade.getAgentMissions(agentId, page, size).getContent());
Page<MissionListDto> result = missionFacade.getAgentMissions(agentId, page, size);
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 missions of specified agent", e);
return ResponseEntity.notFound().build();
......@@ -56,10 +66,17 @@ public class MissionController {
@Operation(description = "Retrieves all missions in the archive")
@GetMapping("/allMissions")
public final ResponseEntity<List<MissionListDto>> getAllMissions(int page, int pageSize) {
public final ResponseEntity<ApiResponse> getAllMissions(int page, int pageSize) {
//TODO logged in user logic
try {
return ResponseEntity.ok().body(missionFacade.getAllMissions(page, pageSize).getContent());
Page<MissionListDto> result = missionFacade.getAllMissions(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 all missions", e);
return ResponseEntity.notFound().build();
......
package cz.muni.fi.pa165.rest.controller;
import cz.muni.fi.pa165.dto.MissionListDto;
import cz.muni.fi.pa165.dto.ReportDetailDto;
import cz.muni.fi.pa165.facade.ReportFacade;
import cz.muni.fi.pa165.rest.ApiResponse;
import cz.muni.fi.pa165.rest.ApiUris;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -30,10 +33,17 @@ public class ReportController {
@Operation(description = "Retrieve all mission reports")
@GetMapping("/missions/{missionId}")
public final ResponseEntity<List<ReportDetailDto>> getAllMissionReports(@PathVariable long missionId, int page, int pageSize) {
public final ResponseEntity<ApiResponse> getAllMissionReports(@PathVariable long missionId, int page, int pageSize) {
//TODO logged in user logic
try {
return ResponseEntity.ok().body(reportFacade.getAllMissionReports(missionId, page, pageSize).getContent());
Page<ReportDetailDto> result = reportFacade.getAllMissionReports(missionId, 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 reports of specified mission", e);
return ResponseEntity.notFound().build();
......@@ -42,10 +52,17 @@ public class ReportController {
@Operation(description = "Retrieve all reports of specific mission of specified agent")
@GetMapping("/missions/{missionId}/agents/{agentId}")
public final ResponseEntity<List<ReportDetailDto>> getAllMissionAgentReports(@PathVariable long missionId, @PathVariable long agentId, int page, int pageSize) {
public final ResponseEntity<ApiResponse> getAllMissionAgentReports(@PathVariable long missionId, @PathVariable long agentId, int page, int pageSize) {
//TODO logged in user logic
try {
return ResponseEntity.ok().body(reportFacade.getAllMissionAgentReports(missionId, agentId, page, pageSize).getContent());
Page<ReportDetailDto> result = reportFacade.getAllMissionAgentReports(missionId, agentId, 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 reports of specified mission of specified agent", e);
return ResponseEntity.notFound().build();
......@@ -54,10 +71,17 @@ public class ReportController {
@Operation(description = "Retrieve all report of specified agent")
@GetMapping("/agents/{agentId}")
public final ResponseEntity<List<ReportDetailDto>> getAllAgentReports(@PathVariable long agentId, int page, int pageSize) {
public final ResponseEntity<ApiResponse> getAllAgentReports(@PathVariable long agentId, int page, int pageSize) {
//TODO logged in user logic
try {
return ResponseEntity.ok().body(reportFacade.getAllAgentReports(agentId, page, pageSize).getContent());
Page<ReportDetailDto> result = reportFacade.getAllAgentReports(agentId, 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 reports of specified agent", e);
return ResponseEntity.notFound().build();
......
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