refactor: Change balance type to bigdecimal for better precision

This commit is contained in:
Phan Huy Tran 2025-02-26 11:03:12 +01:00
parent 9c5e05f29d
commit aef0f09d09
5 changed files with 23 additions and 7 deletions

View file

@ -16,3 +16,12 @@ Authorization: Bearer {{token}}
"username": "john.doe"
}
### Deposit
POST http://localhost:8080/deposit/checkout
Content-Type: application/json
Origin: http://localhost:8080
Authorization: Bearer {{token}}
{
"amount": 60.12
}

View file

@ -8,6 +8,7 @@ import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.math.BigDecimal;
@Setter
@Getter
@ -20,9 +21,11 @@ public class UserEntity {
@Column(unique = true)
private String keycloakId;
private String username;
private float balance;
public UserEntity(String keycloakId, String username, float balance) {
@Column(precision = 19, scale = 2)
private BigDecimal balance;
public UserEntity(String keycloakId, String username, BigDecimal balance) {
this.keycloakId = keycloakId;
this.username = username;
this.balance = balance;

View file

@ -4,6 +4,8 @@ import de.szut.casino.user.dto.CreateUserDto;
import de.szut.casino.user.dto.GetUserDto;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
@Service
public class UserMappingService {
public GetUserDto mapToGetUserDto(UserEntity user) {
@ -11,7 +13,6 @@ public class UserMappingService {
}
public UserEntity mapToUserEntity(CreateUserDto createUserDto) {
return new UserEntity(createUserDto.getKeycloakId(), createUserDto.getUsername(), 0);
}
return new UserEntity(createUserDto.getKeycloakId(), createUserDto.getUsername(), BigDecimal.ZERO); }
}

View file

@ -5,6 +5,8 @@ import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.math.BigDecimal;
@Getter
@Setter
@AllArgsConstructor
@ -12,5 +14,5 @@ import lombok.Setter;
public class GetUserDto {
private String keycloakId;
private String username;
private float balance;
private BigDecimal balance;
}

View file

@ -21,10 +21,11 @@ export class UserService {
});
}
public async getOrCreateUser(userProfile: KeycloakProfile) {
public async getOrCreateUser(userProfile: KeycloakProfile): Promise<User | undefined | null> {
if (userProfile.id == null) {
return;
return null;
}
return await this.getUser(userProfile.id)
.toPromise()
.then(async (user) => {