Skip to content

Commit

Permalink
Merge pull request #1 from bifrurcated/another-feature
Browse files Browse the repository at this point in the history
Another feature
  • Loading branch information
bifrurcated authored Jan 17, 2024
2 parents f6ecf19 + dc2336d commit 2e7a66b
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 60 deletions.
77 changes: 37 additions & 40 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,58 @@ on:
branches: ["main"]

permissions:
contents: read
checks: write
id-token: write

jobs:
test:
name: Build and Run Tests
runs-on: ubuntu-latest

services:
postgres:
image: postgres:15.1-alpine
env:
POSTGRES_DB: wallet
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 123
ports:
- 5432:5432
options:
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5


steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v3
name: Check out code
- name: Setup Maven Action
uses: s4u/setup-maven-action@v1.11.0
with:
java-version: '17'
distribution: 'temurin'
cache: maven
env:
POSTGRES_HOST: postgres
- name: Build with Maven
run: mvn --batch-mode --update-snapshots verify
- name: Publish Test Report
uses: mikepenz/action-junit-report@v4
if: success() || failure() # always run even if the previous step fails
java-version: 17

- name: Run tests
run: mvn test --batch-mode --fail-at-end

- name: JUnit Report Action
uses: mikepenz/action-junit-report@v4.0.4
if: success() || failure()
with:
report_paths: '**/build/test-results/test/TEST-*.xml'
build-docker-image:

build-and-push-docker-image:
needs: test
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
name: Check out code

- name: Docker Build & Push Action
uses: mr-smithers-excellent/docker-build-push@v6
- name: Setup Maven Action
uses: s4u/setup-maven-action@v1.11.0
with:
java-version: 17

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
image: bifurcated/wallet
tags: latest
registry: docker.io
dockerfile: Dockerfile
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build image
run: mvn spring-boot:build-image -Ddockerfile=Dockerfile.layers -DskipTests

- name: Get project artifactId and version
run: |
artifactId=$(mvn -q -Dexec.executable=echo -Dexec.args='${project.artifactId}' exec:exec)
version=$(mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' exec:exec)
image_name="${artifactId}:${version}"
echo "IMAGE_NAME=${image_name}" >> $GITHUB_ENV
- name: print IMAGE_NAME
run: echo ${IMAGE_NAME}

- name: Push image
run: docker push ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}
19 changes: 19 additions & 0 deletions Dockerfile.layers
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#stage 1
#Start with a base image containing Java runtime
FROM openjdk:17-slim as build
WORKDIR application
ARG JAR_FILE=target/${project.build.finalName}.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract

#stage 2
#Same Java runtime
FROM openjdk:17-slim
WORKDIR application
COPY --from=build application/dependencies/ ./
COPY --from=build application/spring-boot-loader/ ./
COPY --from=build application/snapshot-dependencies/ ./
COPY --from=build application/application/ ./

#execute the application
ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} org.springframework.boot.loader.JarLauncher ${SPRING_OPTS}"]
21 changes: 21 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<name>wallet</name>
<description>Wallet</description>
<properties>
<docker.image.prefix>bifurcated</docker.image.prefix>
<java.version>17</java.version>
</properties>
<dependencies>
Expand Down Expand Up @@ -60,6 +61,16 @@
<version>1.18.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>6.0.11</version>
</dependency>
</dependencies>

<build>
Expand All @@ -68,6 +79,16 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<projectArtefactId>${project.artifactId}</projectArtefactId>
<projectVersion>${project.version}</projectVersion>
</systemPropertyVariables>
<image>
<name>${docker.image.prefix}/${project.artifactId}:${project.version}</name>
</image>
<layers>
<enabled>true</enabled>
</layers>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/com/bifurcated/wallet/data/WalletRepo.java

This file was deleted.

42 changes: 42 additions & 0 deletions src/main/java/com/bifurcated/wallet/repository/WalletRepo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.bifurcated.wallet.repository;

import com.bifurcated.wallet.data.Wallet;
import org.springframework.data.jdbc.repository.query.Modifying;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.relational.core.sql.LockMode;
import org.springframework.data.relational.repository.Lock;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

import java.util.Optional;
import java.util.UUID;

public interface WalletRepo extends CrudRepository<Wallet, UUID> {

@Query("""
UPDATE wallet
SET amount = (amount + :amount)
WHERE id = :id
RETURNING id, amount
""")
Optional<Wallet> updateAmount(UUID id, Float amount);

@Query("""
UPDATE wallet
SET amount = (amount - :amount)
WHERE id = :id
RETURNING id, amount
""")
Wallet updateAmountReduceReturning(UUID id, Float amount);

@Query("""
UPDATE wallet
SET amount = (amount - :amount)
WHERE id = :id
""")
@Modifying
void updateAmountReduce(UUID id, Float amount);

@Query("SELECT w.id, w.amount FROM wallet w WHERE id = :id FOR UPDATE")
Optional<Wallet> findByIdForUpdate(@Param("id") UUID id);
}
29 changes: 26 additions & 3 deletions src/main/java/com/bifurcated/wallet/service/WalletService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.bifurcated.wallet.service;

import com.bifurcated.wallet.data.Wallet;
import com.bifurcated.wallet.data.WalletRepo;
import com.bifurcated.wallet.repository.WalletRepo;
import com.bifurcated.wallet.errors.NotEnoughMoneyError;
import com.bifurcated.wallet.errors.WalletNotFoundError;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -22,14 +22,18 @@ public WalletService(WalletRepo walletRepo) {

@Transactional
public Wallet addAmount(UUID id, Float amount) {
var wallet = walletRepo.findById(id).orElseThrow(WalletNotFoundError::new);
Wallet wallet = walletRepo.findByIdForUpdate(id).orElseThrow(WalletNotFoundError::new);
wallet.setAmount(wallet.getAmount() + amount);
return walletRepo.save(wallet);
}

public Wallet addAmountOneOperation(UUID id, Float amount) {
return walletRepo.updateAmount(id, amount).orElseThrow(WalletNotFoundError::new);
}

@Transactional
public Wallet reduceAmount(UUID id, Float amount) {
var wallet = walletRepo.findById(id).orElseThrow(WalletNotFoundError::new);
var wallet = walletRepo.findByIdForUpdate(id).orElseThrow(WalletNotFoundError::new);
var reduce = wallet.getAmount() - amount;
if (reduce < 0) {
throw new NotEnoughMoneyError(wallet.getAmount(), amount);
Expand All @@ -38,6 +42,25 @@ public Wallet reduceAmount(UUID id, Float amount) {
return walletRepo.save(wallet);
}

public Wallet reduceAmountUpdateReturning(UUID id, Float amount) {
var wallet = walletRepo.findById(id).orElseThrow(WalletNotFoundError::new);
var reduce = wallet.getAmount() - amount;
if (reduce < 0) {
throw new NotEnoughMoneyError(wallet.getAmount(), amount);
}
return walletRepo.updateAmountReduceReturning(id, amount);
}

public Wallet reduceAmountUpdateFind(UUID id, Float amount) {
var wallet = walletRepo.findById(id).orElseThrow(WalletNotFoundError::new);
var reduce = wallet.getAmount() - amount;
if (reduce < 0) {
throw new NotEnoughMoneyError(wallet.getAmount(), amount);
}
walletRepo.updateAmountReduce(id, amount);
return walletRepo.findById(id).orElseThrow(WalletNotFoundError::new);
}

public Float amount(UUID id) {
return walletRepo.findById(id).orElseThrow(WalletNotFoundError::new).getAmount();
}
Expand Down
Loading

0 comments on commit 2e7a66b

Please sign in to comment.