Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ddouddle/limit-management

Repository files navigation

Limit Management System

A Spring Boot application for managing user limits. Supports multiple limit types, limit initialization, increase, decrease, occupy, release, and query operations.

Features

  • Multi-type Limit Management: Each user can have one limit account per limit type (CREDIT, LOAN, WITHDRAWAL, TRANSFER)
  • Limit Operations: Initialize, increase, decrease, occupy, release, and query limits
  • Concurrency Control: Pessimistic locking for safe concurrent operations
  • Scheduled Tasks: Simulates multi-user operations automatically
  • RESTful API: Clean API design with consistent response format

Technology Stack

  • Java 17
  • Spring Boot 3.5.9
  • Spring Data JPA
  • SQLite Database
  • Lombok
  • JUnit 5 & MockMvc

Project Structure

src/main/java/com/credit/limit_management/
├── LimitManagementApplication.java # Main application entry
├── controller/
│ └── LimitController.java # REST API endpoints
├── service/
│ ├── LimitService.java # Service interface
│ └── impl/
│ └── LimitServiceImpl.java # Service implementation
├── repository/
│ └── LimitAccountRepository.java # Data access layer
├── entity/
│ ├── LimitAccount.java # Limit account entity
│ └── LimitType.java # Limit type enum
├── dto/
│ ├── LimitRequest.java # Request DTO
│ ├── LimitResponse.java # Response DTO
│ └── ApiResponse.java # Generic API response wrapper
├── exception/
│ ├── GlobalExceptionHandler.java # Global exception handler
│ ├── LimitNotFoundException.java
│ ├── InsufficientLimitException.java
│ ├── DuplicateLimitException.java
│ └── InvalidOperationException.java
└── scheduler/
 └── LimitScheduledTask.java # Scheduled task for simulation

Setup and Run

Prerequisites

  • Java 17 or higher
  • Maven 3.6 or higher

Build

mvn clean install

Run

mvn spring-boot:run

The application will start on http://localhost:8080

Run Tests

mvn test

API Endpoints

Initialize Limit

Create a new limit account for a user.

POST /api/limits/init
Content-Type: application/json
{
 "userId": 1,
 "limitType": "CREDIT",
 "amount": 10000.0
}

Increase Limit

Increase the total limit for a user's limit account.

PUT /api/limits/increase
Content-Type: application/json
{
 "userId": 1,
 "limitType": "CREDIT",
 "amount": 5000.0
}

Decrease Limit

Decrease the total limit for a user's limit account.

PUT /api/limits/decrease
Content-Type: application/json
{
 "userId": 1,
 "limitType": "CREDIT",
 "amount": 3000.0
}

Occupy Limit

Occupy (use) a portion of the available limit.

POST /api/limits/occupy
Content-Type: application/json
{
 "userId": 1,
 "limitType": "CREDIT",
 "amount": 2000.0
}

Release Limit

Release (restore) a portion of the used limit.

POST /api/limits/release
Content-Type: application/json
{
 "userId": 1,
 "limitType": "CREDIT",
 "amount": 1000.0
}

Query Limit

Query a specific limit account.

GET /api/limits/{userId}/{limitType}
Example: GET /api/limits/1/CREDIT

Query All Limits

Query all limit accounts for a user.

GET /api/limits/{userId}
Example: GET /api/limits/1

Response Format

All API responses follow a consistent format:

{
 "success": true,
 "message": "Operation successful",
 "data": {
 "id": 1,
 "userId": 1,
 "limitType": "CREDIT",
 "totalLimit": 10000.0,
 "usedLimit": 2000.0,
 "availableLimit": 8000.0,
 "createdAt": "2024年01月01日T10:00:00",
 "updatedAt": "2024年01月01日T10:30:00"
 }
}

Limit Types

Type Description
CREDIT Credit limit for purchases
LOAN Loan limit for borrowing
WITHDRAWAL Withdrawal limit for cash
TRANSFER Transfer limit for transfers

Scheduled Task

The application includes a scheduled task that runs every 10 seconds to simulate multi-user operations:

  • Randomly selects a user (ID 1-5)
  • Randomly selects a limit type
  • Randomly selects an operation (INIT, INCREASE, DECREASE, OCCUPY, RELEASE)
  • Randomly generates an amount (100-1000)

Check the console logs to see the scheduled task execution.

Test Coverage

Unit Tests (LimitServiceTest)

  • Initialize limit - success and duplicate cases
  • Increase limit - success and not found cases
  • Decrease limit - success and insufficient limit cases
  • Occupy limit - success and insufficient limit cases
  • Release limit - success and exceeds used cases
  • Query limit - success and not found cases

Integration Tests (LimitControllerIntegrationTest)

  • All API endpoints tested with MockMvc
  • Validation error handling
  • Error response testing

Database

The application uses SQLite database stored in limit_management.db file in the project root directory.

Schema

CREATE TABLE limit_account (
 id INTEGER PRIMARY KEY AUTOINCREMENT,
 user_id BIGINT NOT NULL,
 limit_type VARCHAR(20) NOT NULL,
 total_limit DOUBLE NOT NULL,
 used_limit DOUBLE NOT NULL,
 available_limit DOUBLE NOT NULL,
 created_at TIMESTAMP NOT NULL,
 updated_at TIMESTAMP NOT NULL,
 version BIGINT,
 UNIQUE(user_id, limit_type)
);

Quick Verification

1. Run All Tests

mvn clean test

2. Start Application

mvn spring-boot:run

3. Test API (Open Another Terminal)

Initialize a limit:

curl -X POST http://localhost:8080/api/limits/init \
 -H "Content-Type: application/json" \
 -d '{"userId":1,"limitType":"CREDIT","amount":10000}'

Occupy some limit:

curl -X POST http://localhost:8080/api/limits/occupy \
 -H "Content-Type: application/json" \
 -d '{"userId":1,"limitType":"CREDIT","amount":2000}'

Query limit:

curl -X GET http://localhost:8080/api/limits/1/CREDIT

Release limit:

curl -X POST http://localhost:8080/api/limits/release \
 -H "Content-Type: application/json" \
 -d '{"userId":1,"limitType":"CREDIT","amount":1000}'

Increase limit:

curl -X PUT http://localhost:8080/api/limits/increase \
 -H "Content-Type: application/json" \
 -d '{"userId":1,"limitType":"CREDIT","amount":5000}'

Query all limits for user:

curl -X GET http://localhost:8080/api/limits/1

License

This project is for assessment purposes.

About

for stori assessment

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

Contributors

Languages

AltStyle によって変換されたページ (->オリジナル) /