Skip to content
This repository was archived by the owner on Nov 23, 2025. It is now read-only.

Latest commit

Β 

History

History
335 lines (262 loc) Β· 10.6 KB

File metadata and controls

335 lines (262 loc) Β· 10.6 KB

πŸ—„οΈ DATABASE CONNECTION - QUICK SUMMARY

βœ… Your Time Logging Service Already Has Database Setup!


πŸ“Š What's Already Working

1. βœ… Database Configuration

File: src/main/resources/application.properties

spring.datasource.url=jdbc:postgresql://localhost:5432/techtorque_timelogs
spring.datasource.username=techtorque
spring.datasource.password=techtorque123
spring.jpa.hibernate.ddl-auto=update  # Auto-creates tables!

2. βœ… Connection Check on Startup

File: src/main/java/.../config/DatabasePreflightInitializer.java

  • Tests database connection BEFORE app starts
  • Exits with error if database is unavailable
  • Same pattern as Auth service

3. βœ… Automatic Table Creation

When you start the service, Hibernate automatically creates:

CREATE TABLE time_logs (
    id VARCHAR(255) PRIMARY KEY,
    employee_id VARCHAR(255) NOT NULL,
    service_id VARCHAR(255),
    project_id VARCHAR(255),
    hours DOUBLE PRECISION NOT NULL,
    date DATE NOT NULL,
    description TEXT,
    work_type VARCHAR(255),
    created_at TIMESTAMP NOT NULL,
    updated_at TIMESTAMP NOT NULL
);

🌱 NEW: Data Seeder (Just Added!)

What It Does

File: src/main/java/.../config/DataSeeder.java

βœ… Runs automatically when app starts
βœ… Only in dev profile (not production)
βœ… Creates sample time logs for testing
βœ… Skips if data already exists

Sample Data Created

  • 3 Employees: EMP001, EMP002, EMP003
  • 7 Days of time logs (excluding weekends)
  • ~30-40 entries with realistic data
  • Various work types: Development, Testing, Meetings, etc.

πŸ”„ How It All Works Together

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  1. Application Starts                                      β”‚
β”‚     .\mvnw.cmd spring-boot:run                              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  2. DatabasePreflightInitializer                            β”‚
β”‚     βœ“ Reads application.properties                          β”‚
β”‚     βœ“ Tries to connect to PostgreSQL                        β”‚
β”‚     βœ“ SUCCESS β†’ Continue | FAIL β†’ Exit                      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  3. Hibernate Schema Creation                               β”‚
β”‚     βœ“ Reads @Entity TimeLog                                 β”‚
β”‚     βœ“ Compares with database                                β”‚
β”‚     βœ“ Creates/Updates tables automatically                  β”‚
β”‚     βœ“ CREATE TABLE IF NOT EXISTS time_logs...              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  4. DataSeeder.run() [NEW!]                                 β”‚
β”‚     βœ“ Check if dev profile active                           β”‚
β”‚     βœ“ Check if data exists                                  β”‚
β”‚     βœ“ Insert sample time logs (if empty)                    β”‚
β”‚     βœ“ Log statistics                                        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  5. Service Ready! πŸŽ‰                                        β”‚
β”‚     http://localhost:8085                                   β”‚
β”‚     Database: techtorque_timelogs                           β”‚
β”‚     API endpoints accepting requests                        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ†š Comparison with Auth Service

Feature Auth Service Time Logging Service
Database Config βœ… application.properties βœ… application.properties
Preflight Check βœ… Yes βœ… Yes
Auto Schema βœ… Hibernate DDL βœ… Hibernate DDL
Data Seeder βœ… Seeds Users & Roles βœ… Seeds Time Logs
Dev Profile Only βœ… Yes βœ… Yes
Idempotent βœ… Checks before insert βœ… Checks before insert

Your service follows the EXACT SAME pattern! βœ…


πŸš€ How to Use

Start Service (Database Auto-Setup)

cd D:\TechTorque\Time_Logging_Service\time-logging-service
.\mvnw.cmd spring-boot:run

What Happens:

  1. βœ… Connects to PostgreSQL
  2. βœ… Creates time_logs table (if not exists)
  3. βœ… Seeds sample data (if empty)
  4. βœ… Service ready on port 8085

Check Seeded Data

# Via API
curl http://localhost:8085/api/time-logs/employee/EMP001

# Via Database
psql -U techtorque -d techtorque_timelogs
SELECT COUNT(*) FROM time_logs;

🎯 Key Differences from Auth Service

Auth Service Seeds:

  • Users (superadmin, admin, employee, customer)
  • Roles (SUPER_ADMIN, ADMIN, EMPLOYEE, CUSTOMER)
  • User-Role Relationships

Time Logging Service Seeds:

  • Time Log Entries (work records)
  • Sample Projects (PRJ001, PRJ002, etc.)
  • Sample Services (SRV001, SRV002, etc.)
  • Various Work Types (Development, Testing, etc.)

Both follow the same pattern: Check profile β†’ Check existing data β†’ Insert if needed


πŸ“‹ DataSeeder Code Flow

@Component
public class DataSeeder implements CommandLineRunner {
    
    @Override
    public void run(String... args) {
        // Step 1: Check if dev profile
        if (!isDevProfile()) {
            return; // Skip in production
        }
        
        // Step 2: Check if data exists
        if (timeLogRepository.count() > 0) {
            return; // Already seeded
        }
        
        // Step 3: Insert sample data
        seedSampleTimeLogs();
    }
    
    private void seedSampleTimeLogs() {
        // Create logs for 3 employees
        // Over 7 days (excluding weekends)
        // Morning + afternoon sessions
        // Total: ~30-40 entries
    }
}

βœ… What You Get

After starting the service with the DataSeeder:

βœ… Service Running on Port: 8085
βœ… Database: techtorque_timelogs (connected)
βœ… Tables: time_logs (auto-created)
βœ… Sample Data: ~30-40 time log entries
βœ… 3 Test Employees: EMP001, EMP002, EMP003
βœ… Date Range: Last 7 days (weekdays only)
βœ… Work Types: Development, Testing, Meetings, etc.

Sample Data Example:

{
  "id": "uuid-here",
  "employeeId": "EMP001",
  "projectId": "PRJ001",
  "serviceId": "SRV001",
  "hours": 4.5,
  "date": "2025-10-31",
  "description": "Implemented new feature for customer dashboard",
  "workType": "Development",
  "createdAt": "2025-10-31T10:00:00",
  "updatedAt": "2025-10-31T10:00:00"
}

πŸ› οΈ Manual Database Setup (Optional)

If you want to create the database manually first:

-- Connect to PostgreSQL
psql -U postgres

-- Create database
CREATE DATABASE techtorque_timelogs;

-- Create user
CREATE USER techtorque WITH PASSWORD 'techtorque123';

-- Grant permissions
GRANT ALL PRIVILEGES ON DATABASE techtorque_timelogs TO techtorque;

But this is OPTIONAL! The service can create everything automatically.


πŸ”§ Configuration Options

Environment Variables (Production)

# Override defaults in production
export DB_HOST=production-server.com
export DB_PORT=5432
export DB_NAME=timelogs_prod
export DB_USER=secure_user
export DB_PASS=secure_password
export DB_MODE=validate          # Don't auto-modify schema
export SPRING_PROFILE=prod       # No data seeding

Development (Default)

# Uses defaults from application.properties
DB_HOST=localhost
DB_PORT=5432
DB_NAME=techtorque_timelogs
DB_USER=techtorque
DB_PASS=techtorque123
DB_MODE=update                   # Auto-update schema
SPRING_PROFILE=dev               # Enable data seeding

πŸ“š Files Reference

File Purpose
application.properties Database connection config
DatabasePreflightInitializer.java Connection check on startup
TimeLog.java Entity β†’ defines table structure
TimeLogRepository.java Database operations
DataSeeder.java Sample data population [NEW!]

πŸŽ‰ Summary

βœ… What You Already Had:

  1. Database connection configuration
  2. Preflight check (connection verification)
  3. Automatic table creation
  4. Working CRUD operations

πŸ†• What I Just Added:

  1. DataSeeder.java - Populates sample data for testing
  2. DATABASE_SETUP_GUIDE.md - Comprehensive documentation
  3. THIS SUMMARY - Quick reference

🎯 Result:

Your Time Logging Service now has the EXACT SAME database setup pattern as the Auth Service!

βœ… Connection config
βœ… Preflight check
βœ… Auto schema creation
βœ… Data seeding (dev only)
βœ… Production-ready

Everything follows Spring Boot best practices and mirrors the Auth service architecture!


🚦 Next Steps

  1. Start the service:

    .\mvnw.cmd spring-boot:run
  2. Verify seeding:

    curl http://localhost:8085/api/time-logs/employee/EMP001
  3. Check database:

    psql -U techtorque -d techtorque_timelogs
    SELECT * FROM time_logs LIMIT 5;

That's it! Your database setup is complete and matches the Auth service pattern. πŸŽ‰