Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redis issue business code #20

Open
Junobee25 opened this issue Mar 2, 2024 · 0 comments
Open

redis issue business code #20

Junobee25 opened this issue Mar 2, 2024 · 0 comments

Comments

@Junobee25
Copy link
Owner

Junobee25 commented Mar 2, 2024

1. 
@PostMapping("/redis-stock/purchase")
    public Response<Void> purchase(@RequestBody StockAdapterDto request) {
        redisStockService.purchase(request);
        return Response.success();
    }
2.
public void purchase(StockAdapterDto stockAdapterDto) {
        redisStockRepository.decrementStock(stockAdapterDto.productId(), stockAdapterDto.quantity());
    }

3.
public void decrementStock(Long productId, Integer quantity) {
        String key = getKey(productId);
        RedisStock redisStock = redisTemplate.opsForValue().get(key);

        redisStock.verifyRemainAvailability(quantity);
        redisStock.setRemain(redisStock.getRemain() - quantity);
        redisTemplate.opsForValue().set(key, redisStock);
    }

Redis inventory classes are:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class RedisStock implements Serializable {

    private Long productId;
    private Integer remain;

    public void verifyRemainAvailability(final Integer quantity) {
        if ((remain - quantity) < 0) {
            throw new IllegalArgumentException();
        }
        remain -= quantity;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant