Skip to content

theodorismeko/demo.restapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Match Betting REST API

A comprehensive Spring Boot REST API for managing sports matches and betting odds with modern development practices, and test coverage.

πŸš€ Features

  • Full CRUD operations for matches and match odds
  • Multi-sport support (Football and Basketball)
  • PostgreSQL database with Docker support
  • RESTful API design with proper HTTP status codes
  • Docker containerization with Docker Compose
  • OpenAPI 3.0 documentation with interactive Swagger UI
  • Input validation with Bean Validation
  • Global exception handling with custom error responses
  • Comprehensive logging with configurable levels
  • Database migration support with Hibernate DDL
  • πŸ†• Complete test suite with unit and integration tests
  • πŸ†• Code coverage reporting with JaCoCo
  • πŸ†• Multiple database support (PostgreSQL for production, H2 for testing)

πŸ“‹ Requirements

  • Java 21 (LTS)
  • Maven 3.6+
  • Docker & Docker Compose (recommended for development)
  • PostgreSQL 15+ (handled by Docker Compose)

πŸ—οΈ Architecture

The application follows a clean layered architecture:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Controllers   β”‚ ← REST endpoints & validation
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚    Services     β”‚ ← Business logic & transactions
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Repositories   β”‚ ← Data access layer
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚    Entities     β”‚ ← JPA entities & relationships
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ—„οΈ Database Schema

Entities

  • Match: Core match information (teams, date, time, sport)
  • MatchOdds: Betting odds associated with matches
  • Sport: Enumeration for supported sports (FOOTBALL, BASKETBALL)

Relationships

  • One Match can have multiple MatchOdds (One-to-Many)
  • Cascading delete: removing a match removes all associated odds

πŸ—„οΈ Database Configuration

PostgreSQL Setup

The application is configured to use PostgreSQL as the primary database. When running with Docker Compose, PostgreSQL is automatically set up with:

  • Database name: matches
  • Username: postgres
  • Password: password
  • Port: 5432

πŸ†• Multi-Database Support

  • Production: PostgreSQL with full persistence
  • Testing: H2 in-memory database for fast test execution

Database Initialization

A custom initialization script is mounted at /docker/init.sql that runs when the PostgreSQL container first starts. You can add custom SQL commands for:

  • Creating initial tables
  • Inserting seed data
  • Setting up database extensions

Application Properties

The key database configurations in application.properties:

spring.datasource.url=jdbc:postgresql://localhost:5432/matches
spring.jpa.hibernate.ddl-auto=update

πŸš€ Quick Start

Option 1: Docker Compose (Recommended)

  1. Clone the repository

    git clone <repository-url>
    cd demo.restapi
  2. Start all services

    docker-compose up -d
  3. Access the application

  4. View logs

    docker-compose logs -f app
  5. Stop services

    docker-compose down

Option 2: Local Development

  1. Start PostgreSQL

    docker-compose up -d postgres
  2. Run the application

    ./mvnw spring-boot:run
  3. Access the application at http://localhost:8088

πŸ“š API Documentation

Interactive Documentation

Endpoints Overview

Method Endpoint Description
POST /api/matches Create a new match
GET /api/matches Get all matches
GET /api/matches/{id} Get match by ID
PUT /api/matches/{id} Update match
DELETE /api/matches/{id} Delete match
POST /api/match-odds Create match odds
GET /api/match-odds/{id} Get odds by ID
GET /api/match-odds/match/{matchId} Get odds by match ID
PUT /api/match-odds/{id} Update match odds
DELETE /api/match-odds/{id} Delete match odds

πŸ§ͺ Sample API Usage

Create a Match

curl -X POST http://localhost:8088/api/matches \
  -H "Content-Type: application/json" \
  -d '{
    "description": "OSFP-PAO",
    "matchDate": "2021-03-31",
    "matchTime": "12:00",
    "teamA": "OSFP",
    "teamB": "PAO",
    "sport": "FOOTBALL"
  }'

Create Match Odds

curl -X POST http://localhost:8088/api/match-odds \
  -H "Content-Type: application/json" \
  -d '{
    "matchId": 1,
    "specifier": "1",
    "odd": 2.5
  }'

Get All Matches

curl http://localhost:8088/api/matches

πŸ—‚οΈ Project Structure

demo.restapi/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main/
β”‚   β”‚   β”œβ”€β”€ java/com/meko/restapi/
β”‚   β”‚   β”‚   β”œβ”€β”€ Application.java           # Main Spring Boot application
β”‚   β”‚   β”‚   β”œβ”€β”€ config/
β”‚   β”‚   β”‚   β”‚   └── OpenAPIConfig.java     # Swagger/OpenAPI configuration
β”‚   β”‚   β”‚   β”œβ”€β”€ controller/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ MatchController.java   # Match REST endpoints
β”‚   β”‚   β”‚   β”‚   └── MatchOddsController.java # Match odds REST endpoints
β”‚   β”‚   β”‚   β”œβ”€β”€ dto/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ MatchDTO.java          # Match data transfer object
β”‚   β”‚   β”‚   β”‚   └── MatchOddsDTO.java      # Match odds data transfer object
β”‚   β”‚   β”‚   β”œβ”€β”€ entity/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ Match.java             # Match JPA entity
β”‚   β”‚   β”‚   β”‚   └── MatchOdds.java         # Match odds JPA entity
β”‚   β”‚   β”‚   β”œβ”€β”€ enumeration/
β”‚   β”‚   β”‚   β”‚   └── Sport.java             # Sport enumeration
β”‚   β”‚   β”‚   β”œβ”€β”€ exception/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ GlobalExceptionHandler.java # Global error handling
β”‚   β”‚   β”‚   β”‚   └── ResourceNotFoundException.java # Custom exception
β”‚   β”‚   β”‚   β”œβ”€β”€ repository/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ MatchRepository.java   # Match JPA repository
β”‚   β”‚   β”‚   β”‚   └── MatchOddsRepository.java # Match odds JPA repository
β”‚   β”‚   β”‚   β”œβ”€β”€ service/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ MatchService.java      # Service interface
β”‚   β”‚   β”‚   β”‚   └── impl/
β”‚   β”‚   β”‚   β”‚       └── MatchServiceImpl.java # Service implementation
β”‚   β”‚   β”‚   └── util/                      # πŸ†• Utility classes
β”‚   β”‚   └── resources/
β”‚   β”‚       β”œβ”€β”€ application.properties     # Application configuration
β”‚   β”‚       └── application-test.properties # πŸ†• Test configuration
β”‚   └── test/                             # πŸ†• Comprehensive test suite
β”‚       β”œβ”€β”€ java/com/meko/restapi/
β”‚       β”‚   β”œβ”€β”€ ApplicationTests.java     # Application context test
β”‚       β”‚   β”œβ”€β”€ controller/
β”‚       β”‚   β”‚   └── MatchControllerIntegrationTest.java # Integration tests
β”‚       β”‚   └── service/
β”‚       β”‚       └── MatchServiceTest.java # Unit tests
β”‚       └── resources/
β”‚           └── application-test.properties
β”œβ”€β”€ docker/
β”‚   └── init.sql                          # PostgreSQL initialization script
β”œβ”€β”€ target/                              # πŸ†• Build outputs
β”‚   β”œβ”€β”€ site/jacoco/                     # Code coverage reports
β”‚   β”œβ”€β”€ surefire-reports/                # Test reports
β”‚   └── test-classes/                    # Compiled test classes
β”œβ”€β”€ docker-compose.yml                   # Multi-container Docker app
β”œβ”€β”€ Dockerfile                          # Application container definition
β”œβ”€β”€ pom.xml                            # Maven dependencies and build config
β”œβ”€β”€ DESIGN_ARCHITECTURE.md            # Architecture and design decisions
└── README.md                         # This file

βš™οΈ Configuration

Database Configuration

The application uses PostgreSQL as the primary database. Key configurations in application.properties:

# PostgreSQL Database
spring.datasource.url=jdbc:postgresql://localhost:5432/matches
spring.datasource.username=postgres
spring.datasource.password=password

# JPA/Hibernate
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

πŸ†• Test Configuration

Separate test configuration in application-test.properties:

# H2 In-Memory Database for Testing
spring.datasource.url=jdbc:h2:mem:testdb
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=false

Environment Variables

When running with Docker, the following environment variables are used:

SPRING_DATASOURCE_URL=jdbc:postgresql://postgres:5432/matches
SPRING_DATASOURCE_USERNAME=postgres
SPRING_DATASOURCE_PASSWORD=password

πŸ§ͺ Testing

πŸ†• Comprehensive Test Suite

The project now includes a complete testing strategy:

Unit Tests

  • Service Layer Tests: MatchServiceTest.java
  • Business Logic Validation: Isolated component testing
  • Mock Dependencies: Using Mockito for external dependencies

Integration Tests

  • Controller Integration: MatchControllerIntegrationTest.java
  • End-to-End API Testing: Full request/response cycle testing
  • Database Integration: Using Testcontainers with real PostgreSQL

Test Commands

# Run all tests
./mvnw test

# Run tests with coverage
./mvnw clean test jacoco:report

# Run specific test class
./mvnw test -Dtest=MatchServiceTest

# Run integration tests only
./mvnw test -Dtest=*IntegrationTest

πŸ†• Code Coverage

  • JaCoCo Integration: Automatic code coverage reporting
  • Coverage Reports: Available in target/site/jacoco/index.html
  • Coverage Metrics: Line, branch, and method coverage tracking

View Coverage Report

# Generate coverage report
./mvnw clean test jacoco:report

# Open coverage report (Windows)
start target/site/jacoco/index.html

# Open coverage report (Linux/Mac)
open target/site/jacoco/index.html

πŸ› οΈ Development

Build the Application

./mvnw clean compile

Package as JAR

./mvnw clean package

Run with Maven

./mvnw spring-boot:run

πŸ†• Development with Coverage

# Run with test coverage monitoring
./mvnw spring-boot:run -Dspring.profiles.active=dev

Docker Build

docker build -t match-betting-api .

πŸ”§ Technology Stack

Category Technology
Framework Spring Boot 3.5.3
Language Java 21
Database PostgreSQL 15 / H2 (testing)
ORM Spring Data JPA / Hibernate
Build Tool Maven
Documentation SpringDoc OpenAPI 3
Containerization Docker & Docker Compose
Database Admin pgAdmin 4
Code Reduction Lombok
Validation Bean Validation (JSR-303)
πŸ†• Testing JUnit 5, Mockito
πŸ†• Code Coverage JaCoCo
πŸ†• Test Database H2 Database

πŸš€ Getting Started for Developers

1. Quick Setup

git clone <repository-url>
cd demo.restapi
docker-compose up -d

2. Development Workflow

# Start development
./mvnw spring-boot:run

# Run tests during development
./mvnw test -Dtest=*Test

# Check code coverage
./mvnw jacoco:report

3. Before Committing

# Run full test suite
./mvnw clean test

# Check code coverage
./mvnw jacoco:report

# Verify build
./mvnw clean package

πŸ‘€ Author

Theodoros Meko


πŸ”— Quick Links

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published