Skip to content

Juste120/family-tree

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌳 Family Tree - Genealogy Application

Swagger UI

A modern Spring Boot application to manage a family tree using Neo4j, leveraging Java Records for DTOs and following best development practices.

πŸ“‹ Table of Contents

✨ Features

  • βœ… Java 17 with Records for DTOs
  • βœ… Spring Boot 3.2 with layered architecture
  • βœ… Neo4j as a graph database
  • βœ… OpenAPI/Swagger for documentation
  • βœ… Bean Validation with Jakarta Validation
  • βœ… Centralized exception handling
  • βœ… Unit and integration tests
  • βœ… Docker multi-stage build
  • βœ… Docker Compose for orchestration
  • βœ… Structured logging with SLF4J
  • βœ… Typed enums for Gender and Relationships
  • βœ… Mappers for Entity ↔ DTO conversion
  • βœ… Service interface + implementation

πŸ“¦ Prerequisites

  • Java 17 or higher
  • Maven 3.9+
  • Docker & Docker Compose (recommended)
  • Neo4j 5.15 (for local installation)

πŸš€ Installation

  1. Clone the repository
    git clone https://github.com/Juste120/family-tree.git
    cd family-tree
  2. Copy the environment file
    cp .env.example .env
  3. Modify variables if necessary
    # Edit .env with your settings
    nano .env

βš™οΈ Configuration

Spring Profiles

  • dev: Development with detailed logs
  • prod: Production with limited logs
  • test: Testing with a dedicated Neo4j database

Environment Variables

SPRING_PROFILE=dev
NEO4J_URI=bolt://localhost:7687
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=password
SERVER_PORT=8080

πŸƒ Running the Application

Option 1: With Docker Compose (Recommended)

# Start all services
docker-compose up -d

# View logs
docker-compose logs -f

# Stop services
docker-compose down

The application will be accessible at:

  • API: http://localhost:8080
  • Swagger UI: http://localhost:8080/swagger-ui.html
  • Neo4j Browser: http://localhost:7474

Option 2: Local Development

# Start Neo4j with Docker
docker run -d \
  --name neo4j \
  -p 7474:7474 -p 7687:7687 \
  -e NEO4J_AUTH=neo4j/password \
  neo4j:5.15.0

# Run the application
mvn spring-boot:run -Dspring-boot.run.profiles=dev

Option 3: Build and Run the JAR

# Compile
mvn clean package -DskipTests

# Run
java -jar target/family-tree.jar

πŸ“‘ API Endpoints

Persons (CRUD)

# List all persons
GET /api/v1/persons

# Get a person by ID
GET /api/v1/persons/{id}

# Create a person
POST /api/v1/persons

# Update a person
PUT /api/v1/persons/{id}

# Delete a person
DELETE /api/v1/persons/{id}

Relationships

# Add a parent-child relationship
POST /api/v1/persons/{parentId}/children/{childId}

# Add a marriage relationship
POST /api/v1/persons/{person1Id}/spouse/{person2Id}

# Remove relationships
DELETE /api/v1/persons/{parentId}/children/{childId}
DELETE /api/v1/persons/{person1Id}/spouse/{person2Id}

Tree Navigation

# Descendants
GET /api/v1/persons/{id}/descendants

# Ancestors
GET /api/v1/persons/{id}/ancestors

# Siblings
GET /api/v1/persons/{id}/siblings

# Grandchildren
GET /api/v1/persons/{id}/grandchildren

# Grandparents
GET /api/v1/persons/{id}/grandparents

# Count descendants
GET /api/v1/persons/{id}/descendants/count

# Path between two persons
GET /api/v1/persons/path?from={id1}&to={id2}

Search and Statistics

# Search by name
GET /api/v1/persons/search?lastName=Smith

# Statistics
GET /api/v1/persons/stats/count
GET /api/v1/persons/stats/alive

🎯 Usage Examples

Create a person

curl -X POST http://localhost:8080/api/v1/persons \
  -H "Content-Type: application/json" \
  -d '{
    "firstName": "Alice",
    "lastName": "Martin",
    "birthDate": "1990-05-15",
    "gender": "F"
  }'

Add a relationship

# Parent (ID 1) β†’ Child (ID 2)
curl -X POST http://localhost:8080/api/v1/persons/1/children/2

πŸ§ͺ Testing

Run all tests

mvn test

Run a specific test

mvn test -Dtest=PersonServiceImplTest

Coverage with JaCoCo

mvn clean verify
# Report in target/site/jacoco/index.html

🐳 Docker

Build the image

# Production
docker build -t family-tree:latest .

# Development
docker build -f Dockerfile.dev -t family-tree:dev .

Run the container

docker run -d \
  -p 8080:8080 \
  -e SPRING_PROFILE=prod \
  -e NEO4J_URI=bolt://neo4j:7687 \
  --name family-tree-app \
  family-tree:latest

Docker Compose

# Start
docker-compose up -d

# Logs
docker-compose logs -f family-tree-app

# Rebuild
docker-compose up -d --build

# Stop and delete
docker-compose down -v

πŸ—οΈ Architecture

src/main/java/com/example/familytree/
β”œβ”€β”€ FamilyTreeApplication.java
β”œβ”€β”€ config/                    # Spring Configuration
β”‚   β”œβ”€β”€ Neo4jConfig.java
β”‚   β”œβ”€β”€ OpenApiConfig.java
β”‚   └── DataInitializer.java
β”œβ”€β”€ model/                     # Neo4j Entities
β”‚   └── Person.java
β”œβ”€β”€ dto/                       # DTOs (Java Records)
β”‚   β”œβ”€β”€ PersonDTO.java
β”‚   β”œβ”€β”€ PersonSummaryDTO.java
β”‚   β”œβ”€β”€ PersonCreateDTO.java
β”‚   β”œβ”€β”€ PersonUpdateDTO.java
β”‚   └── ...
β”œβ”€β”€ enums/                     # Enumerations
β”‚   β”œβ”€β”€ Gender.java
β”‚   └── RelationType.java
β”œβ”€β”€ mapper/                    # Entity ↔ DTO Conversion
β”‚   └── PersonMapper.java
β”œβ”€β”€ repository/                # Neo4j Data Access
β”‚   └── PersonRepository.java
β”œβ”€β”€ service/                   # Business Logic
β”‚   β”œβ”€β”€ PersonService.java
β”‚   └── impl/
β”‚       └── PersonServiceImpl.java
β”œβ”€β”€ controller/                # REST API
β”‚   └── PersonController.java
└── exception/                 # Error Handling
    β”œβ”€β”€ GlobalExceptionHandler.java
    β”œβ”€β”€ PersonNotFoundException.java
    └── ...

πŸ“š Best Practices Applied

  1. Layered Architecture
    • Clear separation of concerns
    • Controller β†’ Service β†’ Repository
    • DTOs to decouple the API from the model
  2. Java Records for DTOs
    • Immutable by default
    • Concise code (-47% lines)
    • Thread-safe
  3. Validation
    • Bean Validation on DTOs
    • Business validation in the service layer
    • Clear error messages
  4. Custom Exceptions
    • GlobalExceptionHandler for centralization
    • Standardized JSON responses
    • Structured logs
  5. Testing
    • Unit tests (Mockito)
    • Integration tests (MockMvc)
    • Coverage > 80%
  6. Docker
    • Optimized multi-stage build
    • Lightweight Alpine image
    • Non-root user for security
    • Health checks
  7. Documentation
    • Integrated OpenAPI/Swagger
    • JavaDoc on public methods
    • Comprehensive README
  8. Security
    • Input validation
    • Non-root user in Docker
    • No sensitive data in logs

πŸ“Š Useful Cypher Queries

// Visualize the whole family
MATCH path = (p:Person)-[*]-(p2:Person)
RETURN path
LIMIT 100

// Find the person with the most descendants
MATCH (p:Person)-[:PARENT_OF*]->(d:Person)
WITH p, count(d) as descendants
RETURN p.firstName, p.lastName, descendants
ORDER BY descendants DESC
LIMIT 1

// Calculate the tree depth
MATCH path = (root:Person)-[:PARENT_OF*]->(leaf:Person)
WHERE NOT (leaf)-[:PARENT_OF]->()
RETURN max(length(path)) as maxDepth

🀝 Contributing

  1. Fork the project
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘₯ Authors

πŸ™ Acknowledgements

  • Spring Boot Team
  • Neo4j Community
  • OpenAPI Initiative

Made with ❀️ and β˜• by the Family Tree Team

About

Family Tree application with Neo4j

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published