Skip to content

Commit

Permalink
Added pagination to List response, wrapped into ocpi response format
Browse files Browse the repository at this point in the history
  • Loading branch information
klymenko-galyna-ew committed Nov 23, 2023
1 parent 7aa4f1a commit 67ec4ea
Show file tree
Hide file tree
Showing 22 changed files with 368 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.extrawest.ocpi.model.dto.ClientInfoDto;
import com.extrawest.ocpi.service.ClientInfoService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Size;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -28,23 +28,21 @@ protected ClientInfoController(@Autowired ClientInfoService clientInfoService) {
*/
@GetMapping("/{country_code}/{party_id}")
public ResponseEntity<ClientInfoDto> getHubClientInfo(
@PathVariable(value = "country_code") @Max(2) String countryCode,
@PathVariable(value = "party_id") @Max(3) String partyId
) {
@PathVariable(value = "country_code") @Size(min = 2, max = 2) String countryCode,
@PathVariable(value = "party_id") @Size(min = 3, max = 3) String partyId) {
return ResponseEntity.ok(clientInfoService.getHubClientInfo(countryCode, partyId));
};
}

/**
* Push new/updated ClientInfo object to the connect client.
*
* @param countryCode Country code of the eMSP sending this PUT request to the CPO system.
* @param partyId Party ID (Provider ID) of the eMSP sending this PUT request to the CPO system.
* @param partyId Party ID (Provider ID) of the eMSP sending this PUT request to the CPO system.
*/
@PutMapping("/{country_code}/{party_id}")
public void putHubClientInfo(
@PathVariable(value = "country_code") @Max(2) String countryCode,
@PathVariable(value = "party_id") @Max(3) String partyId
) {
@PathVariable(value = "country_code") @Size(min = 2, max = 2) String countryCode,
@PathVariable(value = "party_id") @Size(min = 3, max = 3) String partyId) {
clientInfoService.putHubClientInfo(countryCode, partyId);
};

}
}
31 changes: 25 additions & 6 deletions src/main/java/com/extrawest/ocpi/controller/CpoCdrController.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.extrawest.ocpi.controller;

import com.extrawest.ocpi.model.dto.ResponseFormat;
import com.extrawest.ocpi.model.dto.cdr.CDRDto;
import com.extrawest.ocpi.model.enums.status_codes.OcpiStatusCode;
import com.extrawest.ocpi.service.CpoCdrService;
import com.extrawest.ocpi.service.pagination.PaginationService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -19,9 +24,12 @@
public class CpoCdrController {

protected final CpoCdrService cpoCdrService;
protected final PaginationService paginationService;

protected CpoCdrController(@Autowired CpoCdrService cpoCdrService) {
protected CpoCdrController(@Autowired CpoCdrService cpoCdrService,
@Autowired PaginationService paginationService) {
this.cpoCdrService = cpoCdrService;
this.paginationService = paginationService;
}

/**
Expand All @@ -35,12 +43,23 @@ protected CpoCdrController(@Autowired CpoCdrService cpoCdrService) {
* will contain the pagination related headers.
*/
@GetMapping
public ResponseEntity<List<CDRDto>> getCdr(
public ResponseEntity<ResponseFormat<List<CDRDto>>> getCdr(
@RequestParam(value = "date_from") LocalDateTime dateFrom,
@RequestParam(value = "date_to") LocalDateTime dateTo,
@RequestParam(value = "offset") Integer offset,
@RequestParam(value = "limit") Integer limit
) {
return ResponseEntity.ok(cpoCdrService.getCdr(dateFrom, dateTo, offset, limit));
};
@RequestParam(value = "limit") Integer limit,
HttpServletRequest request) {
int adjustedLimit = paginationService.adjustLimitByMax(limit);

List<CDRDto> cdrs = cpoCdrService.getCdr(dateFrom, dateTo, offset, limit);
long totalCount = cpoCdrService.getTotalCount(dateFrom, dateTo);

ResponseFormat<List<CDRDto>> responseFormat = new ResponseFormat<List<CDRDto>>()
.build(OcpiStatusCode.SUCCESS, cdrs);
HttpHeaders responseHeaders = paginationService.buildHeader(offset, adjustedLimit, request, totalCount);

return ResponseEntity.ok()
.headers(responseHeaders)
.body(responseFormat);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.extrawest.ocpi.controller;

import com.extrawest.ocpi.model.dto.charging_profile.ChargingProfile;
import com.extrawest.ocpi.model.dto.ResponseFormat;
import com.extrawest.ocpi.model.dto.charging_profile.ChargingProfileResponse;
import com.extrawest.ocpi.model.dto.charging_profile.SetChargingProfile;
import com.extrawest.ocpi.model.enums.status_codes.OcpiStatusCode;
import com.extrawest.ocpi.service.CpoChargingProfilesService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -33,12 +35,17 @@ protected CpoChargingProfilesController(@Autowired CpoChargingProfilesService cp
* This is not the response by the Charge Point.
*/
@GetMapping("/{session_id}/{duration}/{response_url}")
public ResponseEntity<ChargingProfile> getChargingProfile(
public ResponseEntity<ResponseFormat<ChargingProfileResponse>> getChargingProfile(
@PathVariable(value = "session_id") String sessionId,
@PathVariable(value = "duration") Integer duration,
@PathVariable(value = "response_url") String responseUrl
) {
return ResponseEntity.ok(cpoChargingProfilesService.getChargingProfile(sessionId, duration, responseUrl));
ChargingProfileResponse chargingProfileResponse =
cpoChargingProfilesService.getChargingProfile(sessionId, duration, responseUrl);

ResponseFormat<ChargingProfileResponse> responseFormat = new ResponseFormat<ChargingProfileResponse>()
.build(OcpiStatusCode.SUCCESS, chargingProfileResponse);
return ResponseEntity.ok(responseFormat);
}

/**
Expand All @@ -52,29 +59,32 @@ public ResponseEntity<ChargingProfile> getChargingProfile(
* This is not the response by the Charge Point.
*/
@PutMapping("/{session_id}")
public ResponseEntity<ChargingProfile> putChargingProfile(
public ResponseEntity<ResponseFormat> putChargingProfile(
@PathVariable(value = "session_id") String sessionId,
@RequestBody @Valid SetChargingProfile setChargingProfileRequestDTO
) {
@RequestBody @Valid SetChargingProfile setChargingProfileRequestDTO) {
return ResponseEntity.ok(cpoChargingProfilesService.putChargingProfile(sessionId, setChargingProfileRequestDTO));
}

/**
* Cancels an existing ChargingProfile for a specific charging session.
*
* @param sessionId The unique id that identifies the session in the CPO platform.
* @param responseUrl URL that the ClearProfileResult POST should be send to. This URL might contain an
* @param responseUrl URL that the ClearProfileResult POST should be sent to. This URL might contain
* unique ID to be able to distinguish between DELETE ChargingProfile requests.
* @return Result of the ChargingProfile DELETE request, by the CPO (not the location/EVSE). So this indicates
* if the CPO understood the ChargingProfile DELETE request and was able to send it to the EVSE. This is
* not the response by the Charge Point.
*/
@DeleteMapping("/{session_id}/{response_url}")
public ResponseEntity<ChargingProfile> deleteChargingProfile(
public ResponseEntity<ResponseFormat<ChargingProfileResponse>> deleteChargingProfile(
@PathVariable(value = "session_id") String sessionId,
@PathVariable(value = "response_url") String responseUrl
) {
return ResponseEntity.ok(cpoChargingProfilesService.deleteChargingProfile(sessionId, responseUrl));
};
@PathVariable(value = "response_url") String responseUrl) {
ChargingProfileResponse chargingProfileResponse =
cpoChargingProfilesService.deleteChargingProfile(sessionId, responseUrl);

ResponseFormat<ChargingProfileResponse> responseFormat = new ResponseFormat<ChargingProfileResponse>()
.build(OcpiStatusCode.SUCCESS, chargingProfileResponse);
return ResponseEntity.ok(responseFormat);
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.extrawest.ocpi.controller;

import com.extrawest.ocpi.model.dto.ResponseFormat;
import com.extrawest.ocpi.model.dto.command.AbstractCommand;
import com.extrawest.ocpi.model.dto.command.CommandResponse;
import com.extrawest.ocpi.model.enums.CommandType;
import com.extrawest.ocpi.model.enums.status_codes.OcpiStatusCode;
import com.extrawest.ocpi.service.CpoCommandsService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cpo/api/2.2.1/commands")
@Tag(name = "CPOCommands")
@Validated
public class CpoCommandsController {

protected final CpoCommandsService cpoCommandsService;
Expand All @@ -31,10 +35,13 @@ protected CpoCommandsController(@Autowired CpoCommandsService cpoCommandsService
* the command request and was able to send it to the Charge Point. This is not the response by the Charge Point
*/
@PostMapping("/{command}")
public ResponseEntity<CommandResponse> postCommand(
public ResponseEntity<ResponseFormat<CommandResponse>> postCommand(
@PathVariable(value = "command") CommandType command,
@RequestBody @Valid AbstractCommand requestedCommand
) {
return ResponseEntity.ok(cpoCommandsService.postCommand(command, requestedCommand));
};
@RequestBody @Valid AbstractCommand requestedCommand) {
CommandResponse commandResponse = cpoCommandsService.postCommand(command, requestedCommand);

ResponseFormat<CommandResponse> responseFormat = new ResponseFormat<CommandResponse>()
.build(OcpiStatusCode.SUCCESS, commandResponse);
return ResponseEntity.ok(responseFormat);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.extrawest.ocpi.controller;

import com.extrawest.ocpi.model.dto.CredentialsDto;
import com.extrawest.ocpi.model.dto.ResponseFormat;
import com.extrawest.ocpi.model.enums.status_codes.OcpiStatusCode;
import com.extrawest.ocpi.service.CpoCredentialsService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/cpo/api/2.2.1/credentials")
@Tag(name = "CPOCredentials")
@Validated
public class CpoCredentialsController {

protected final CpoCredentialsService cpoCredentialsService;
Expand All @@ -26,18 +29,29 @@ protected CpoCredentialsController(@Autowired CpoCredentialsService cpoCredentia
* @return CredentialsDTO
*/
@GetMapping
public ResponseEntity<CredentialsDto> getCredentials() {
return ResponseEntity.ok(cpoCredentialsService.getCredentials());
public ResponseEntity<ResponseFormat<CredentialsDto>> getCredentials() {
CredentialsDto credentials = cpoCredentialsService.getCredentials();

ResponseFormat<CredentialsDto> responseFormat = new ResponseFormat<CredentialsDto>()
.build(OcpiStatusCode.SUCCESS, credentials);
return ResponseEntity.ok(responseFormat);
}

/**
* Provides the server with a credentials object to access the client’s system (i.e. register).
*
* @param credentialsDTO - credentials
* @param credentialsToClient - credentials to access the client’s system
* @return current client's credentials to access the server’s system with newly generated token
*/
@PostMapping
public void postCredentials(@RequestBody @Valid CredentialsDto credentialsDTO) {
cpoCredentialsService.postCredentials(credentialsDTO);
public ResponseEntity<ResponseFormat<CredentialsDto>> postCredentials(
@RequestBody @Valid CredentialsDto credentialsToClient) {

CredentialsDto credentialsToServer = cpoCredentialsService.postCredentials(credentialsToClient);

ResponseFormat<CredentialsDto> responseFormat = new ResponseFormat<CredentialsDto>()
.build(OcpiStatusCode.SUCCESS, credentialsToServer);
return ResponseEntity.ok(responseFormat);
}

/**
Expand All @@ -46,7 +60,7 @@ public void postCredentials(@RequestBody @Valid CredentialsDto credentialsDTO) {
* @param credentialsDTO - credentials
*/
@PutMapping
public void putCredentials(@RequestBody @Valid CredentialsDto credentialsDTO) {
public void putCredentials(@RequestBody @jakarta.validation.Valid CredentialsDto credentialsDTO) {
cpoCredentialsService.putCredentials(credentialsDTO);
}

Expand All @@ -58,5 +72,5 @@ public void putCredentials(@RequestBody @Valid CredentialsDto credentialsDTO) {
@DeleteMapping
public void deleteCredentials(@RequestBody @Valid CredentialsDto credentialsDTO) {
cpoCredentialsService.deleteCredentials(credentialsDTO);
};
}
}
Loading

0 comments on commit 67ec4ea

Please sign in to comment.