|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +Spring Boot REST API for a blog platform with JWT authentication. Stack: Spring Boot 2.1.8, MySQL, Spring Security, JPA/Hibernate, Lombok. |
| 8 | + |
| 9 | +## Development Commands |
| 10 | + |
| 11 | +### Build and Run |
| 12 | + |
| 13 | +```bash |
| 14 | +# Build the project |
| 15 | +mvn clean install |
| 16 | + |
| 17 | +# Run the application |
| 18 | +mvn spring-boot:run |
| 19 | + |
| 20 | +# Run tests |
| 21 | +mvn test |
| 22 | + |
| 23 | +# Package the application |
| 24 | +mvn package |
| 25 | +``` |
| 26 | + |
| 27 | +### Docker |
| 28 | + |
| 29 | +```bash |
| 30 | +# Start services (MySQL + Application) |
| 31 | +docker-compose up -d |
| 32 | + |
| 33 | +# Stop services |
| 34 | +docker-compose down |
| 35 | + |
| 36 | +# Rebuild application container |
| 37 | +docker-compose up -d --build application |
| 38 | +``` |
| 39 | + |
| 40 | +### Database Setup |
| 41 | + |
| 42 | +1. Create MySQL database: `CREATE DATABASE blogapi` |
| 43 | +2. Run initialization script: `src/main/resources/blogapi.sql` |
| 44 | +3. Configure credentials in `src/main/resources/application.yml`: |
| 45 | + - `spring.datasource.username` |
| 46 | + - `spring.datasource.password` |
| 47 | + |
| 48 | +Application runs on `http://localhost:8080` |
| 49 | + |
| 50 | +## Architecture |
| 51 | + |
| 52 | +### Layered Architecture |
| 53 | + |
| 54 | +The codebase follows a standard Spring Boot layered architecture: |
| 55 | + |
| 56 | +**Controller → Service → Repository → Database** |
| 57 | + |
| 58 | +- **Controllers** (`controller/`) - REST endpoints, request validation, response mapping |
| 59 | +- **Services** (`service/` and `service/impl/`) - Business logic, transaction management |
| 60 | +- **Repositories** (`repository/`) - JPA repositories extending `JpaRepository` |
| 61 | +- **Models** (`model/`) - JPA entities with relationships |
| 62 | + |
| 63 | +### Security Architecture |
| 64 | + |
| 65 | +- **JWT-based authentication** - Stateless session management |
| 66 | +- **Spring Security** - Configured in `config/SecutiryConfig.java` |
| 67 | +- **Authentication flow**: |
| 68 | + 1. User signs in via `/api/auth/signin` |
| 69 | + 2. `JwtTokenProvider` generates JWT token |
| 70 | + 3. `JwtAuthenticationFilter` validates token on subsequent requests |
| 71 | + 4. `UserPrincipal` represents authenticated user |
| 72 | +- **Authorization**: Method-level security using `@PreAuthorize` annotations |
| 73 | +- **Public endpoints**: GET requests and `/api/auth/**` are public; other endpoints require authentication |
| 74 | + |
| 75 | +### Key Patterns |
| 76 | + |
| 77 | +**DTO Pattern**: Request/Response objects in `payload/` package are separate from entities |
| 78 | +- Use `ModelMapper` bean to convert between entities and DTOs |
| 79 | +- Example: `Post` entity → `PostResponse` DTO |
| 80 | + |
| 81 | +**Audit Trail**: Entities extend `UserDateAudit` or `DateAudit` (`model/audit/`) |
| 82 | +- Automatically tracks `createdAt`, `updatedAt`, `createdBy`, `updatedBy` |
| 83 | +- Configured via `config/AuditingConfig.java` |
| 84 | + |
| 85 | +**Exception Handling**: Centralized in `exception/RestControllerExceptionHandler.java` |
| 86 | +- Custom exceptions: `ResourceNotFoundException`, `BadRequestException`, `UnauthorizedException`, etc. |
| 87 | +- Returns standardized `ExceptionResponse` payload |
| 88 | + |
| 89 | +**Pagination**: Uses `PagedResponse` wrapper for list endpoints |
| 90 | +- Configurable via `AppConstants.DEFAULT_PAGE_NUMBER`, `DEFAULT_PAGE_SIZE` |
| 91 | + |
| 92 | +### Entity Relationships |
| 93 | + |
| 94 | +- **User** (1) → (N) **Post**, **Album**, **Todo**, **Comment** |
| 95 | +- **Post** (N) ↔ (N) **Tag** (many-to-many via `post_tag` join table) |
| 96 | +- **Post** (N) → (1) **Category** |
| 97 | +- **Post** (1) → (N) **Comment** |
| 98 | +- **Album** (1) → (N) **Photo** |
| 99 | +- **User** (N) ↔ (N) **Role** (many-to-many) |
| 100 | +- **User** (1) → (1) **Address**, **Company** (embedded relationships) |
| 101 | + |
| 102 | +**Important**: Most relationships use `FetchType.LAZY` to avoid N+1 queries. Be mindful when adding new entity methods. |
| 103 | + |
| 104 | +### Technology Notes |
| 105 | + |
| 106 | +**Lombok**: Extensively used for boilerplate reduction |
| 107 | +- `@Data`, `@EqualsAndHashCode`, `@NoArgsConstructor`, etc. |
| 108 | +- Models use Lombok; manually override getters/setters when needed (e.g., for defensive copies) |
| 109 | + |
| 110 | +**Jackson**: Configured for Java 8 date/time serialization |
| 111 | +- `@JsonIdentityInfo` on entities to handle circular references |
| 112 | +- `@JsonIgnore` on lazy-loaded relationships |
| 113 | + |
| 114 | +**ModelMapper**: Bean configured in `BlogApiApplication.java` |
| 115 | +- Used throughout service layer for entity-DTO conversions |
| 116 | + |
| 117 | +## API Structure |
| 118 | + |
| 119 | +All APIs under `/api/**`: |
| 120 | +- `/api/auth/**` - Authentication (signup, signin) |
| 121 | +- `/api/users/**` - User management |
| 122 | +- `/api/posts/**` - Posts and comments |
| 123 | +- `/api/albums/**` - Albums and photos |
| 124 | +- `/api/todos/**` - Todo items |
| 125 | +- `/api/categories/**` - Post categories |
| 126 | +- `/api/tags/**` - Post tags |
| 127 | + |
| 128 | +Refer to README.md for complete API documentation with request/response examples. |
| 129 | + |
| 130 | +## Configuration |
| 131 | + |
| 132 | +- **application.yml**: Main configuration (database, JWT settings, CORS) |
| 133 | +- **JWT settings**: `app.jwtSecret`, `app.jwtExpirationInMs` |
| 134 | +- **CORS**: Configured via `cors.allowedOrings` (note: typo in property name) |
| 135 | +- **Hibernate**: `ddl-auto: none` - schema managed via SQL scripts, not auto-generated |
| 136 | + |
| 137 | +## Common Tasks |
| 138 | + |
| 139 | +When adding a new entity: |
| 140 | +1. Create entity class in `model/` (extend `UserDateAudit` if tracking user/date) |
| 141 | +2. Create repository interface in `repository/` |
| 142 | +3. Create request/response DTOs in `payload/` |
| 143 | +4. Create service interface and implementation in `service/` |
| 144 | +5. Create controller in `controller/` |
| 145 | +6. Add custom exceptions if needed in `exception/` |
| 146 | + |
| 147 | +When modifying security: |
| 148 | +- Update `SecutiryConfig.java` for endpoint permissions |
| 149 | +- JWT configuration in `security/JwtTokenProvider.java` |
| 150 | +- Current user retrieval via `@CurrentUser UserPrincipal` annotation |
0 commit comments