Skip to content

Commit

Permalink
Refactoring code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariana Azevedo committed Aug 23, 2020
1 parent 706fcd0 commit 6fe35a3
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web: java -jar -Dspring.profiles.active=prod -Dserver.port=$PORT target/financial-java-api-3.0.3-SNAPSHOT.jar
web: java -jar -Dspring.profiles.active=prod -Dserver.port=$PORT target/financial-java-api-3.1.0-SNAPSHOT.jar
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This API provides HTTP endpoint's and tools for the following:

`POST/financial/v1/transaction`

This endpoint is called to create a new transaction.
This end-point is called to create a new transaction.

**Body:**

Expand Down Expand Up @@ -59,12 +59,12 @@ Returns an empty body with one of the following:
* 404 - Not Found: The requested resource doesn't exist.
* 409 - Conflict: The request conflicts with another request (perhaps due to using the same idempotent key).
* 422 – Unprocessable Entity: if any of the fields are not parsable or the transaction date is in the future.
* 429 - Too Many Requests: Too many requests hit the API too quickly. We recommend an exponential backoff of your requests.
* 429 - Too Many Requests: Too many requests hit the API too quickly. We recommend an exponential back-off of your requests.
* 500, 502, 503, 504 - Server Errors: something went wrong on API end (These are rare).

`PUT/financial/v1/transaction/{id}`

This endpoint is called to update a transaction.
This end-point is called to update a transaction.

**Body:**

Expand Down Expand Up @@ -103,19 +103,19 @@ the one that was updated.

`GET/financial/v1/transactions`

This endpoint returns all transactions created.
This end-point returns all transactions created.

`DELETE/financial/v1/transaction/{id}`

This endpoint causes a transaction for a specific id to be deleted, accepting an empty request body and return a 204 status code.
This end-point causes a transaction for a specific id to be deleted, accepting an empty request body and return a 204 status code.

**Where:**

`id` - transaction id to be deleted.

`GET/financial/v1/statistics`

This endpoint returns the statistics based on the transactions created.
This end-point returns the statistics based on the transactions created.

**Returns:**

Expand Down Expand Up @@ -151,7 +151,7 @@ This endpoint returns the statistics based on the transactions created.

`links` - self-linking URL for the statistic. It is automatically generated.

All `BigDecimal` values always contain exactly two decimal places, eg: `15.385` is returned as `15.39`.
All `BigDecimal` values always contain exactly two decimal places, e.g: `15.385` is returned as `15.39`.

### Technologies used

Expand Down Expand Up @@ -179,7 +179,7 @@ The API also was developed to run with an `jar`. In order to generate this `jar`
mvn package
```

It will clean, compile and generate a `jar` at target directory, e.g. `financial-java-api-3.0.3-SNAPSHOT.jar`
It will clean, compile and generate a `jar` at target directory, e.g. `financial-java-api-3.1.0-SNAPSHOT.jar`

### Execution

Expand All @@ -196,7 +196,7 @@ spring.datasource.username=
spring.datasource.password=
```

When the application is running **Flyway** will create the necessary tables for the creation of the words and the execution of the compare between the endpoints. In the test profile, the application uses **H2** database (data in memory).
When the application is running **Flyway** will create the necessary tables for the creation of the words and the execution of the compare between the end-points. In the test profile, the application uses **H2** database (data in memory).

### Test

Expand All @@ -217,7 +217,7 @@ mvn integration-test
In order to run the API, run the jar simply as following:

```bash
java -jar financial-java-api-3.0.3-SNAPSHOT.jar --spring.profiles.active=prod
java -jar financial-java-api-3.1.0-SNAPSHOT.jar --spring.profiles.active=prod
```

or
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.github.mariazevedo88</groupId>
<artifactId>financial-java-api</artifactId>
<version>3.0.3-SNAPSHOT</version>
<version>3.1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>financial-java-api</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import io.swagger.annotations.ApiOperation;

/**
* SpringBoot RestController that creates all service endpoints related to the statistics.
* SpringBoot RestController that creates all service end-points related to the statistics.
*
* @author Mariana Azevedo
* @since 09/09/2019
Expand Down Expand Up @@ -55,6 +55,8 @@ public StatisticController(StatisticService statisticService,
* @since 03/04/2020
*
* @param apiVersion
* @param apiKey
*
* @return ResponseEntity with a Response<StatisticDTO> object and the HTTP status
*
* HTTP Status:
Expand Down Expand Up @@ -96,7 +98,7 @@ public ResponseEntity<Response<StatisticDTO>> create(@RequestHeader(value=Financ
* @since 03/04/2020
*
* @param transactions
* @return a Statistic object
* @return a <code>Statistic</code> object
*/
private Statistic createStatistics(List<Transaction> transactions) {

Expand All @@ -123,7 +125,7 @@ private Statistic createStatistics(List<Transaction> transactions) {
* @since 03/04/2020
*
* @param statistic
* @return a StatisticDTO object
* @return a <code>StatisticDTO</code> object
*/
private StatisticDTO convertEntityToDTO(Statistic statistic) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import lombok.extern.log4j.Log4j2;

/**
* SpringBoot RestController that creates all service endpoints related to the transaction.
* SpringBoot RestController that creates all service end-points related to the transaction.
*
* @author Mariana Azevedo
* @since 08/09/2019
Expand All @@ -69,14 +69,15 @@ public TransactionController(TransactionService transactionService) {
* @author Mariana Azevedo
* @since 02/04/2020
*
* @param apiVersion
* @param apiVersion - API version at the moment
* @param apiKey - API Key to access the routes
* @param dto, where: id - transaction id; nsu - identification number of a sales transaction using cards. May be null if transaction was paid in cash;
* autorizationNumber - is a one-time code used in the processing of online transactions; amount – transaction amount; a string of arbitrary length that is
* authorizationNumber - is a one-time code used in the processing of online transactions; amount – transaction amount; a string of arbitrary length that is
* parsable as a BigDecimal; transactionDate – transaction time in the ISO 8601 format YYYY-MM-DDThh:mm:ss.sssZ in the Local timezone; type - transaction type:
* CARD (credit-card) or MONEY (paid in cash).
* @param result - Bind result
*
* @return ResponseEntity with a Response<TransactionDTO> object and the HTTP status
* @return ResponseEntity with a <code>Response<TransactionDTO></code> object and the HTTP status
*
* HTTP Status:
*
Expand All @@ -94,8 +95,8 @@ public TransactionController(TransactionService transactionService) {
@PostMapping
@ApiOperation(value = "Route to create a transaction")
public ResponseEntity<Response<TransactionDTO>> create(@RequestHeader(value=FinancialApiUtil.HEADER_FINANCIAL_API_VERSION, defaultValue="${api.version}")
String apiVersion, @RequestHeader(value=FinancialApiUtil.HEADER_API_KEY, defaultValue="${api.key}") String apiKey, @Valid @RequestBody TransactionDTO dto,
BindingResult result) throws NotParsableContentException {
String apiVersion, @RequestHeader(value=FinancialApiUtil.HEADER_API_KEY, defaultValue="${api.key}") String apiKey,
@Valid @RequestBody TransactionDTO dto, BindingResult result) throws NotParsableContentException {

Response<TransactionDTO> response = new Response<>();

Expand Down Expand Up @@ -127,14 +128,15 @@ public ResponseEntity<Response<TransactionDTO>> create(@RequestHeader(value=Fina
* @author Mariana Azevedo
* @since 02/04/2020
*
* @param apiVersion
* @param dto, where: id - transaction id; nsu - identification number of a sales transaction using cards. May be null if transaction was paid in cash;
* @param apiVersion - API version at the moment
* @param apiKey - API Key to access the routes
* @param dto, where: id - transaction id; NSU - identification number of a sales transaction using cards. May be null if transaction was paid in cash;
* autorizationNumber - is a one-time code used in the processing of online transactions; amount – transaction amount; a string of arbitrary length that is
* parsable as a BigDecimal; transactionDate – transaction time in the ISO 8601 format YYYY-MM-DDThh:mm:ss.sssZ in the Local timezone; type - transaction type:
* CARD (credit-card) or MONEY (paid in cash).
* @param result - Bind result
*
* @return ResponseEntity with a Response<TransactionDTO> object and the HTTP status
* @return ResponseEntity with a <code>Response<TransactionDTO></code> object and the HTTP status
*
* HTTP Status:
*
Expand Down Expand Up @@ -191,7 +193,14 @@ public ResponseEntity<Response<TransactionDTO>> update(@RequestHeader(value=Fina
* @author Mariana Azevedo
* @since 02/04/2020
*
* @return ResponseEntity with a Response<Page<TransactionDTO>> object and the HTTP status
* @param apiVersion - API version at the moment
* @param apiKey - API Key to access the routes
* @param startDate - the start date of the search
* @param endDate - the end date of the search
* @param page - the page that will be return in the search
* @param order - the sort order that the results should be shown: ASC - ascending order; DESC - descending order
*
* @return ResponseEntity with a <code>Response<Page<TransactionDTO>></code> object and the HTTP status
*
* HTTP Status:
*
Expand All @@ -215,7 +224,7 @@ public ResponseEntity<Response<Page<TransactionDTO>>> findAllBetweenDates(@Reque
LocalDateTime endDateTime = FinancialApiUtil.convertLocalDateToLocalDateTime(endDate);

Page<Transaction> transactions = transactionService.findBetweenDates(startDateTime, endDateTime,
page, PageOrderEnum.getDirection(order));
page, PageOrderEnum.getSortDirection(order));

if (transactions.isEmpty()) {
throw new TransactionNotFoundException("There are no transactions registered between startDate=" + startDate
Expand All @@ -241,14 +250,16 @@ public ResponseEntity<Response<Page<TransactionDTO>>> findAllBetweenDates(@Reque
}

/**
* Method that search for all the transactions given a nsu.
* Method that search for all the transactions given a NSU.
*
* @author Mariana Azevedo
* @since 02/04/2020
*
* @param apiVersion
* @param transactionNSU
* @return ResponseEntity with a Response<String> object and the HTTP status
* @param apiVersion - API version at the moment
* @param apiKey - API Key to access the routes
* @param transactionNSU - the NSU of the transaction
*
* @return ResponseEntity with a <code>Response<String></code> object and the HTTP status
*
* HTTP Status:
*
Expand All @@ -264,8 +275,8 @@ public ResponseEntity<Response<Page<TransactionDTO>>> findAllBetweenDates(@Reque
@GetMapping(value = "/byNsu/{nsu}")
@ApiOperation(value = "Route to find transactions by the NSU in the API")
public ResponseEntity<Response<List<TransactionDTO>>> findByNsu(@RequestHeader(value=FinancialApiUtil.HEADER_FINANCIAL_API_VERSION, defaultValue="${api.version}")
String apiVersion, @RequestHeader(value=FinancialApiUtil.HEADER_API_KEY, defaultValue="${api.key}") String apiKey, @PathVariable("nsu") String transactionNSU)
throws TransactionNotFoundException {
String apiVersion, @RequestHeader(value=FinancialApiUtil.HEADER_API_KEY, defaultValue="${api.key}") String apiKey,
@PathVariable("nsu") String transactionNSU) throws TransactionNotFoundException {

Response<List<TransactionDTO>> response = new Response<>();
List<Transaction> transactions = transactionService.findByNsu(transactionNSU);
Expand Down Expand Up @@ -297,9 +308,11 @@ public ResponseEntity<Response<List<TransactionDTO>>> findByNsu(@RequestHeader(v
/**
* Method that search a transactions by the id.
*
* @param apiVersion
* @param transactionId
* @return ResponseEntity with a Response<TransactionDTO> object and the HTTP status
* @param apiVersion - API version at the moment
* @param apiKey - API Key to access the routes
* @param transactionId - the id of the transaction
*
* @return ResponseEntity with a <code>Response<TransactionDTO></code> object and the HTTP status
*
* HTTP Status:
*
Expand All @@ -315,8 +328,8 @@ public ResponseEntity<Response<List<TransactionDTO>>> findByNsu(@RequestHeader(v
@GetMapping(value = "/{id}")
@ApiOperation(value = "Route to find a transaction by your id in the API")
public ResponseEntity<Response<TransactionDTO>> findById(@RequestHeader(value=FinancialApiUtil.HEADER_FINANCIAL_API_VERSION, defaultValue="${api.version}")
String apiVersion, @RequestHeader(value=FinancialApiUtil.HEADER_API_KEY, defaultValue="${api.key}") String apiKey, @PathVariable("id") Long transactionId)
throws TransactionNotFoundException {
String apiVersion, @RequestHeader(value=FinancialApiUtil.HEADER_API_KEY, defaultValue="${api.key}") String apiKey,
@PathVariable("id") Long transactionId) throws TransactionNotFoundException {

Response<TransactionDTO> response = new Response<>();
Transaction transaction = transactionService.findById(transactionId);
Expand All @@ -338,25 +351,28 @@ public ResponseEntity<Response<TransactionDTO>> findById(@RequestHeader(value=Fi
* @author Mariana Azevedo
* @since 02/04/2020
*
* @param transactionId
* @return ResponseEntity with a Response<String> object and the HTTP status
* @param apiVersion - API version at the moment
* @param apiKey - API Key to access the routes
* @param transactionId - the id of the transaction
*
* @return ResponseEntity with a <code>Response<String></code> object and the HTTP status
*
* HTTP Status:
*
* 204 - OK: Everything worked as expected.
* 400 - Bad Request: The request was unacceptable, often due to missing a required parameter.
* 404 - Not Found: The requested resource doesn't exist.
* 409 - Conflict: The request conflicts with another request (perhaps due to using the same idempotent key).
* 429 - Too Many Requests: Too many requests hit the API too quickly. We recommend an exponential backoff of your requests.
* 429 - Too Many Requests: Too many requests hit the API too quickly. We recommend an exponential back-off of your requests.
* 500, 502, 503, 504 - Server Errors: something went wrong on Goldgem's end (These are rare).
*
* @throws TransactionNotFoundException
*/
@DeleteMapping(value = "/{id}")
@ApiOperation(value = "Route to delete a transaction in the API")
public ResponseEntity<Response<String>> delete(@RequestHeader(value=FinancialApiUtil.HEADER_FINANCIAL_API_VERSION, defaultValue="${api.version}")
String apiVersion, @RequestHeader(value=FinancialApiUtil.HEADER_API_KEY, defaultValue="${api.key}") String apiKey, @PathVariable("id") Long transactionId)
throws TransactionNotFoundException {
String apiVersion, @RequestHeader(value=FinancialApiUtil.HEADER_API_KEY, defaultValue="${api.key}") String apiKey,
@PathVariable("id") Long transactionId) throws TransactionNotFoundException {

Response<String> response = new Response<>();
Transaction transaction = transactionService.findById(transactionId);
Expand All @@ -372,13 +388,13 @@ public ResponseEntity<Response<String>> delete(@RequestHeader(value=FinancialApi
}

/**
* Method to convert an Transaction DTO to an Transaction entity.
* Method to convert an Transaction DTO to a Transaction entity.
*
* @author Mariana Azevedo
* @since 03/04/2020
*
* @param dto
* @return a Transaction object
* @return a <code>Transaction</code> object
*/
private Transaction convertDTOToEntity(TransactionDTO dto) {

Expand All @@ -387,13 +403,13 @@ private Transaction convertDTOToEntity(TransactionDTO dto) {
}

/**
* Method to convert an Transaction entity to an Transaction DTO.
* Method to convert an Transaction entity to a Transaction DTO.
*
* @author Mariana Azevedo
* @since 03/04/2020
*
* @param transaction
* @return a TransactionDTO object
* @return a <code>TransactionDTO</code> object
*/
private TransactionDTO convertEntityToDTO(Transaction transaction) {

Expand Down Expand Up @@ -421,12 +437,15 @@ private void createSelfLink(Transaction transaction, TransactionDTO transactionD
* @author Mariana Azevedo
* @since 03/04/2020
*
* @param apiVersion
* @param apiVersion - API version at the moment
* @param apiKey - API Key to access the routes
* @param transactionDTO
* @throws TransactionNotFoundException
*/
private void createSelfLinkInCollections(String apiVersion, String apiKey, final TransactionDTO transactionDTO) throws TransactionNotFoundException {
Link selfLink = linkTo(methodOn(TransactionController.class).findById(apiVersion, apiKey, transactionDTO.getId())).withSelfRel();
private void createSelfLinkInCollections(String apiVersion, String apiKey, final TransactionDTO transactionDTO)
throws TransactionNotFoundException {
Link selfLink = linkTo(methodOn(TransactionController.class).findById(apiVersion, apiKey,
transactionDTO.getId())).withSelfRel();
transactionDTO.add(selfLink);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package io.github.mariazevedo88.financialjavaapi.exception;

/**
* Class that implements DuplicateStatisticsException in the API
*
* @author Mariana Azevedo
* @since 23/08/2020
*/
public class DuplicateStatisticsException extends Exception {

private static final long serialVersionUID = 6082551323004629906L;
Expand Down
Loading

0 comments on commit 6fe35a3

Please sign in to comment.