Skip to content

Commit

Permalink
Merge pull request #4 from franklaercio/fix-error-in-statement
Browse files Browse the repository at this point in the history
Improvement application with code style, terms, documentation and tests
  • Loading branch information
franklaercio committed Feb 25, 2024
2 parents d129fab + 0f3ed58 commit da9915c
Show file tree
Hide file tree
Showing 25 changed files with 671 additions and 651 deletions.
124 changes: 117 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,123 @@
# Rinha de Backend 2024 Q1

__Nome__: Frank Laércio
## Description

This project, Rinha de Backend 2024 Q1, is a banking system simulation developed in Java using the
Spring Boot framework. It provides functionalities such as account management, transaction handling,
and balance inquiries. The system is designed to handle multiple accounts and transactions
concurrently, ensuring data consistency and
reliability. The project uses an SQL database for data persistence and Maven for dependency
management. It's designed for developers who want to understand how to build a robust and scalable
banking system using Spring Boot and Java.

If you want to check more about of the challenge, please check the [Rinha de Backend 2024 Q1](https://github.com/franklaercio/rinha-de-backend-2024-q1).

## Technologies Used

__Tecnologias__:
- Java
- Spring WebFlux
- PostgreSQL
- NGINX
- Spring Boot
- Maven
- SQL
- JUnit
- Postgres
- Docker
- Nginx

## Learning Objectives

__In this project, you will:__
- Learn how to build a banking system using Spring Boot and Java.
- Understand how to handle transactions and account management in a banking system.
- Learn how to use an SQL database for data persistence.
- Understand how to use Docker to run a database.
- Learn how to use Nginx as a reverse proxy server.

__Concurrent programming learning:__
- Understand how to handle concurrent transactions and account management.
- Learn how to ensure data consistency and reliability in a concurrent environment.
- Understand how to handle multiple requests concurrently.
- Learn how to use locks and synchronization to ensure data consistency.
- Understand how to use transactions to ensure data reliability.
- Learn about problems of deadlocks and how to avoid them.
- Understand how to use thread pools to handle multiple requests concurrently.

__Prerequisites:__
- Basic knowledge of Java and Spring Boot.
- Basic knowledge of Postgres.
- Basic knowledge of Docker.
- Basic knowledge of Nginx.
- Basic knowledge of Maven.

__Study Materials:__
- [Spring Boot Documentation](https://spring.io/projects/spring-boot)
- [Java Documentation](https://docs.oracle.com/en/java/)
- [Postgres Documentation](https://www.postgresql.org/docs/)
- [Docker Documentation](https://docs.docker.com/)
- [Nginx Documentation](https://nginx.org/en/docs/)
- [Concurrency in Java](https://www.baeldung.com/java-concurrency)
- [Java Multithreading](https://www.geeksforgeeks.org/multithreading-in-java/)
- [Java Thread Pools](https://www.baeldung.com/thread-pool-java-and-guava)
- [Java Synchronization](https://www.baeldung.com/java-synchronized)
- [Java Transactions](https://www.baeldung.com/java-transactions)
- [Concurrency Programming](https://www.geeksforgeeks.org/java-util-concurrent-package)
- [Concurrency Problems](https://www.geeksforgeeks.org/concurrency-in-operating-system/)

## Setup and Installation

To run this project, you need to have Java 11 and Maven installed on your machine. You also need to
have a Postgres database running. You can use Docker to run the database. Here are the steps to
install and run the project:

1. Clone the repository: `bash git clone https://github.com/franklaercio/rinha-de-backend-java`
2. Navigate to the project directory: `bash cd rinha-de-backend-2024-q1`
3. Run the database using Docker: `bash docker-compose up -d`
4. Run the project using Maven: `bash mvn spring-boot:run`
5. The project will start running on `http://localhost:8080`.
You can use Postman or any other API client to test the endpoints.
6. To stop the database, run:
7. `bash docker-compose down`
8. To run the tests, run:
9. `bash mvn test`

## Usage

- To create a transaction, send a POST request to `http://localhost:8080/clientes/{id}/transacoes`
with the following JSON body:

```json
{
"valor": "1000",
"tipo": "d",
"descricao": "test"
}
```

- To get a statement, send a GET request to `http://localhost:8080/clientes/{id}/extrato`.

## Contributing

If you want to contribute to this project, you can follow these steps:

1. Fork the project.
2. Create a new branch with your changes:
```bash
git checkout -b feature/my-feature
```
3. Save your changes and create a commit message telling what you did:
```bash
git commit -m "My changes"
```
4. Push your branch:
5. ```bash
git push origin feature/my-feature
```
6. Open a pull request with a description of your changes.
7. After your changes are approved, they will be merged into the project.

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

__Link Repositório__: https://github.com/franklaercio/rinha-de-backend-java
## Contact Information

__LinkedIn__: https://www.linkedin.com/in/frank-laercio/
If you have any questions about this project, please contact me at e-mail.
7 changes: 0 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.16.1</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand Down
18 changes: 10 additions & 8 deletions script.sql
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
CREATE TABLE IF NOT EXISTS CUSTOMER
CREATE TABLE IF NOT EXISTS account
(
id SERIAL PRIMARY KEY,
max_limit INT NOT NULL,
balance INT NOT NULL
);

CREATE TABLE IF NOT EXISTS CUSTOMER_TRANSACTION
CREATE TABLE IF NOT EXISTS transactions
(
id SERIAL PRIMARY KEY,
id_customer INTEGER NOT NULL REFERENCES CUSTOMER,
id_account INTEGER NOT NULL REFERENCES account,
amount INT NOT NULL,
kind VARCHAR(1) NOT NULL,
description VARCHAR(255) NOT NULL,
created_at TIMESTAMP NOT NULL
);

INSERT INTO CUSTOMER (max_limit, balance) VALUES (100000, 0);
INSERT INTO CUSTOMER (max_limit, balance) VALUES (80000, 0);
INSERT INTO CUSTOMER (max_limit, balance) VALUES (1000000, 0);
INSERT INTO CUSTOMER (max_limit, balance) VALUES (10000000, 0);
INSERT INTO CUSTOMER (max_limit, balance) VALUES (500000, 0);
CREATE INDEX IF NOT EXISTS idx_account_transaction_created_at ON transactions (created_at);

INSERT INTO account (max_limit, balance) VALUES (100000, 0);
INSERT INTO account (max_limit, balance) VALUES (80000, 0);
INSERT INTO account (max_limit, balance) VALUES (1000000, 0);
INSERT INTO account (max_limit, balance) VALUES (10000000, 0);
INSERT INTO account (max_limit, balance) VALUES (500000, 0);
52 changes: 52 additions & 0 deletions src/main/java/com/github/rinha/controllers/AccountController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.github.rinha.controllers;

import com.github.rinha.domain.Account;
import com.github.rinha.domain.Statement;
import com.github.rinha.domain.Transact;
import com.github.rinha.services.AccountService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
public class AccountController {

private final AccountService accountService;

public AccountController(AccountService accountService) {
this.accountService = accountService;
}

@GetMapping("/clientes/{id}/extrato")
public Mono<ResponseEntity<Statement>> retrieveStatement(@PathVariable int id) {
if (id < 1 || id > 5) {
return Mono.just(ResponseEntity.status(HttpStatus.NOT_FOUND).build());
}

return accountService.retrieveStatement(id)
.map(ResponseEntity::ok)
.onErrorReturn(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build());
}

@PostMapping("/clientes/{id}/transacoes")
public Mono<ResponseEntity<Account>> transact(@PathVariable int id,
@RequestBody Transact transact) {
if (id < 1 || id > 5) {
return Mono.just(ResponseEntity.status(HttpStatus.NOT_FOUND).build());
}

if (!transact.isRequestValid()) {
return Mono.just(ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).build());
}

return accountService.processTransaction(id, transact.parseValueToInt(), transact.getKind(),
transact.getDescription())
.map(ResponseEntity::ok)
.onErrorReturn(ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).build());
}
}
40 changes: 0 additions & 40 deletions src/main/java/com/github/rinha/controllers/CustomerController.java

This file was deleted.

44 changes: 44 additions & 0 deletions src/main/java/com/github/rinha/domain/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.github.rinha.domain;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Account {

@JsonProperty("limite")
private int maxLimit;

@JsonProperty("saldo")
private int balance;

public Account() {
}

public Account(int maxLimit, int balance) {
this.maxLimit = maxLimit;
this.balance = balance;
}

public boolean hasEnoughBalance(int valor) {
return this.balance + this.maxLimit <= valor;
}

public int getMaxLimit() {
return maxLimit;
}

public void setMaxLimit(int maxLimit) {
this.maxLimit = maxLimit;
}

public int getBalance() {
return balance;
}

public void setBalance(int balance) {
this.balance = balance;
}
}
62 changes: 62 additions & 0 deletions src/main/java/com/github/rinha/domain/Balance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.github.rinha.domain;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.rinha.domain.utils.TimeUtils;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Balance {

@JsonProperty("total")
private int amount;

@JsonProperty("data_extrato")
private String date;

@JsonProperty("limite")
private int limit;

public Balance() {
}

public Balance(int amount, int limit) {
this.amount = amount;
this.date = TimeUtils.nowUTCToString();
this.limit = limit;
}

public int getAmount() {
return amount;
}

public void setAmount(int amount) {
this.amount = amount;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public int getLimit() {
return limit;
}

public void setLimit(int limit) {
this.limit = limit;
}

@Override
public String toString() {
return "{" +
"\"amount\"=" + amount +
", \"date\"='" + date + '\'' +
", \"limit\"=" + limit +
'}';
}
}
Loading

0 comments on commit da9915c

Please sign in to comment.