StoHexGo is a hands-on learning project designed to explore the fundamentals of Go programming and the Hexagonal Architecture pattern. It tackles the practical problem of stock portfolio management, providing an engaging context to apply these concepts.
The core challenge is to build a system that manages a portfolio of stocks, enabling buying and selling operations. The system must adhere to the First In, First Out (FIFO) principle when selling shares, ensuring accurate profit calculations.
- FIFO Selling Rule: When selling shares, prioritize the oldest (first bought) shares.
- Stock Ownership: Prevent selling more shares than currently owned.
- Transaction Order: Process transactions chronologically, disallowing sales before purchases.
- Transaction Details: Each transaction includes its type ("buy" or "sell"), stock identifier, price, and quantity.
- Go Language: Leverages Go's simplicity, concurrency, and performance for efficient backend development.
- Hexagonal Architecture: Organizes the codebase into loosely coupled components (ports and adapters), promoting adaptability and testability.
- GORM (SQLite): Employs GORM for seamless database interactions, simplifying data persistence.
- RESTful API: Provides endpoints to process transactions and retrieve portfolio balance.
- Go Fundamentals: Gain hands-on experience with Go syntax, data structures, error handling, and concurrency.
- Hexagonal Architecture: Understand the principles and benefits of this architectural pattern, fostering clean code and maintainability.
- Practical Application: Apply theoretical knowledge to a real-world problem, solidifying understanding.
- Go 1.22+
- SQLite Database (managed via GORM)
-
Clone the repository:
git clone https://github.com/anxkhn/StoHexGo.git cd StoHexGo
-
Install dependencies:
go mod tidy
-
Set up the database:
- GORM will automatically handle database migrations when the application starts.
-
Run the application:
go run main.go
-
Test the API:
- Use tools like Postman, cURL, or any REST client to test the API endpoints.
Checks if the server is running.
Response:
{
"msg": "π Server is running!"
}
Processes a list of transactions, calculates the net profit, and returns it.
Request Body:
{
"transactions": [
{ "type": "buy", "stockId": "AAPL", "price": 100, "quantity": 10 },
{ "type": "buy", "stockId": "AAPL", "price": 150, "quantity": 15 },
{ "type": "sell", "stockId": "AAPL", "price": 120, "quantity": 10 },
{ "type": "buy", "stockId": "AAPL", "price": 110, "quantity": 10 },
{ "type": "sell", "stockId": "AAPL", "price": 130, "quantity": 25 },
{ "type": "buy", "stockId": "AAPL", "price": 140, "quantity": 20 },
{ "type": "sell", "stockId": "AAPL", "price": 150, "quantity": 15 },
{ "type": "buy", "stockId": "AAPL", "price": 135, "quantity": 10 },
{ "type": "sell", "stockId": "AAPL", "price": 160, "quantity": 15 }
]
}
Response:
{
"profit": 600
}
Retrieves the current inventory of stocks in the portfolio.
Response:
{
"AAPL": 20
}
Here's an overview of the project structure:
βββ cmd
β βββ main.go
βββ go.mod
βββ go.sum
βββ internal
β βββ adapters
β β βββ handlers
β β β βββ http_handler.go
β β βββ repositories
β β βββ sqlite_repository.go
β βββ config
β β βββ config.go
β βββ core
β βββ domain
β β βββ transactions.go
β βββ ports
β β βββ repositories.go
β β βββ services.go
β βββ services
β βββ portfolio_services.go
βββ portfolio.db
βββ README.md
Hexagonal Architecture, also known as the Ports and Adapters pattern, aims to create a flexible and maintainable system by separating the core business logic from external systems (like databases, web servers, etc.). Hereβs how it is implemented in StoHexGo:
-
cmd/main.go
: The entry point of the application. It initializes the server and other necessary components. -
internal/adapters
: Contains the adapters for communication with the external world.handlers
: Implements HTTP handlers that process incoming requests and send responses. For example,http_handler.go
manages REST API interactions.repositories
: Handles data persistence and retrieval.sqlite_repository.go
is responsible for interacting with the SQLite database using GORM.
-
internal/config
: Manages configuration settings of the application.config.go
provides configuration loading and management functionality. -
internal/core
: Contains the core business logic and the application's domain model.domain
: Defines the core domain models and entities.transactions.go
includes definitions related to stock transactions.ports
: Defines interfaces for interacting with the core logic.repositories.go
andservices.go
specify the contracts for repository and service layers.services
: Implements the core business logic.portfolio_services.go
includes the logic for managing stock portfolios and calculating profits.
-
portfolio.db
: The SQLite database file used for data persistence.
This project demonstrates how to implement a stock portfolio management system that adheres to FIFO rules using Go, GORM, and Hexagonal Architecture. The API allows you to manage transactions and calculate profits while maintaining a clean and scalable codebase.