📌 Overview
This project is a Spring Boot REST API built using Clean Architecture principles. It manages electricity usage data and demonstrates proper layered design, DTO mapping, and business logic handling such as carbon footprint calculation.
- Track daily electricity usage
- Retrieve highest consumption day
- Carbon footprint calculation
- Soft delete support
- Pagination for large datasets
- Full CRUD operations
- Clean layered architecture
- DTO + Mapper (MapStruct)
| Category | Technology |
|---|---|
| Language | Java 21 |
| Framework | Spring Boot |
| Database | PostgreSQL / H2 |
| ORM | Spring Data JPA |
| Mapping | MapStruct |
| Build Tool | Maven |
| Boilerplate | Lombok |
| API Docs | Swagger (Springdoc OpenAPI) |
The project follows a Clean Architecture approach:
Controller → UseCase → Repository → Database
Layers:
- Controller Layer
- Handles HTTP requests and responses
- UseCase Layer
- Contains business logic (e.g., carbon footprint calculation)
- Domain Layer
- Core business models
- Repository Layer
- Handles data persistence (via JPA)
- Infrastructure Layer
- Database implementation and configurations
🚀 Features
- CRUD operations for electricity usage
- Pagination support for listing usage data
- Soft delete using Hibernate
- Auditing (createdAt, updatedAt)
- Clean Architecture implementation
- DTO-based request/response handling
- MapStruct for object mapping
- Derived field: Carbon Footprint Calculation
electricity-consumption-system/
├── src/
│ ├── main/
│ │ ├── java/lk/simple/electricity_consumption_system/
│ │ │ ├── domain/
│ │ │ │ ├── model/
│ │ │ │ │ └── ElectricityUsage.java
│ │ │ │ └── repository/
│ │ │ │ └── ElectricityUsageRepository.java
│ │ │ │
│ │ │ ├── usecase/
│ │ │ │ ├── ElectricityUsageUseCase.java
│ │ │ │ └── ElectricityUsageUseCaseImpl.java
│ │ │ │
│ │ │ ├── infrastructure/
│ │ │ │ ├── config/
│ │ │ │ │ └── BeanConfig.java
│ │ │ │ └── persistence/
│ │ │ │ ├── ElectricityUsageEntity.java
│ │ │ │ ├── ElectricityUsageImpl.java
│ │ │ │ └── JpaElectricityUsageRepository.java
│ │ │ │
│ │ │ ├── web/
│ │ │ │ ├── controllers/
│ │ │ │ │ └── ElectricityUsageController.java
│ │ │ │ ├── DTOs/
│ │ │ │ │ ├── ElectricityUsageRequestDTO.java
│ │ │ │ │ └── ElectricityUsageResponseDTO.java
│ │ │ │ └── mappers/
│ │ │ │ └── ElectricityUsageMapper.java
│ │ │ │
│ │ │ └── ElectricityConsumptionApplication.java
│ │ │
│ │ └── resources/
│ │ ├── application.properties
│ │ └── example-application.properties
│ │
│ └── test/
│ └── ElectricityConsumptionApplicationTests.java
│
├── pom.xml
├── mvnw
└── mvnw.cmd- domain → Core business models and repository interfaces
- usecase → Business logic and application rules
- infrastructure → Database layer and implementations
- web → Controllers, DTOs, and API layer
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/v1/usage/all |
Get all usage (pagination) |
| POST | /api/v1/usage |
Create new usage |
| PUT | /api/v1/usage/{id} |
Update usage |
| DELETE | /api/v1/usage/{id} |
Soft delete |
| GET | /api/v1/usage/highest-consumption |
Highest usage day |
{
"date": "2026-04-01",
"unitConsumed": 120,
"category": "Household"
}{
"id": 1,
"date": "2026-04-01",
"unitConsumed": 120,
"category": "Household",
"carbonFootPrint": 54.0
}Carbon Footprint = unitConsumed × 0.45
git clone https://github.com/yahan-botheju/electricity-consumption-system.git
cd electricity-consumption-system
mvn spring-boot:run
http://localhost:5000/api/v1/usage
Swagger UI available at:
http://localhost:5000/swagger-ui.html
📌 Key Concepts Demonstrated
- Clean Architecture principles
- Separation of concerns
- Dependency inversion
- DTO pattern
- Business logic isolation
- Mapping with MapStruct
This project was built as part of learning & practicing modern backend development with Java & Spring Boot, focusing on writing clean, maintainable, & scalable code.
This project was built to practice:
- Clean Architecture
- REST API development
- Backend best practices
This is a learning project designed with real-world architecture in mind and can be extended into a production-ready system.