A modern REST API for managing in-game item transactions, inventory, and virtual currency. Built with Go and clean architecture principles.
- ๐๏ธ Item Shop System
- Purchase items with coin validation
- Sell items back for partial refunds
- Transaction history tracking
- Automatic inventory management
- ๐ Inventory Management
- Track player item ownership
- Automatic filling on purchase
- Safe removal on selling
- Item quantity validation
- ๐ฐ Virtual Currency
- Player coin balance tracking
- Secure transaction handling
- Automatic coin deduction/addition
- Balance validation
The project follows clean architecture with clear separation of concerns:
โโโ pkg/
โ โโโ inventory/
โ โ โโโ repository/ # Data access layer
โ โ โโโ service/ # Business logic
โ โ โโโ model/ # Data transfer objects
โ โ โโโ exception/ # Custom errors
โ โโโ itemShop/
โ โ โโโ controller/ # HTTP handlers
โ โ โโโ service/ # Business logic
โ โ โโโ repository/ # Data access
โ โ โโโ model/ # DTOs
โ โ โโโ exception/ # Custom errors
โ โโโ playerCoin/
โ โโโ repository/ # Data access layer
โ โโโ service/ # Business logic
โ โโโ model/ # DTOs
โโโ test/ # Integration tests
The service includes comprehensive test coverage using testify mocks:
func TestItemShopService_Buying(t *testing.T) {
mockItemShopRepo := new(_itemShopRepository.ItemShopRepositoryMock)
mockPlayerCoinRepo := new(_playerCoinRepository.PlayerCoinRepositoryMock)
mockInventoryRepo := new(_inventoryRepository.InventoryRepositoryMock)
logger := echo.New().Logger
service := _itemShopService.NewItemShopServiceImpl(
mockItemShopRepo,
mockPlayerCoinRepo,
mockInventoryRepo,
logger,
)
t.Run("should buy item successfully", func(t *testing.T) {
// Test implementation
})
t.Run("should fail when not enough coins", func(t *testing.T) {
// Test implementation
})
}
All endpoints require OAuth2 authentication with Google.
GET /v1/item-shop
Query Parameters:
- name (optional): Filter by item name
- description (optional): Filter by description
- page (required): Page number (min: 1)
- size (required): Items per page (min: 1, max: 20)
Response:
{
"items": [
{
"id": 1,
"name": "Sword",
"description": "A sword is a bladed melee weapon",
"picture": "https://example.com/sword.jpg",
"price": 100
}
],
"paginate": {
"page": 1,
"totalPage": 5
}
}
POST /v1/item-shop/buy
Content-Type: application/json
Request:
{
"itemID": 1,
"quantity": 2
}
Response:
{
"id": 123,
"playerID": "player1",
"amount": 800,
"createdAt": "2024-03-15T10:30:00Z"
}
POST /v1/item-shop/sell
Content-Type: application/json
Request:
{
"itemID": 1,
"quantity": 1
}
Response:
{
"id": 124,
"playerID": "player1",
"amount": 850,
"createdAt": "2024-03-15T10:35:00Z"
}
GET /v1/inventory
Authorization: Required
Response:
{
"items": [
{
"item": {
"id": 1,
"name": "Sword",
"description": "A sword is a bladed melee weapon",
"picture": "https://example.com/sword.jpg",
"price": 100
},
"quantity": 5
}
]
}
GET /v1/player-coin
Authorization: Required
Response:
{
"playerID": "player1",
"coin": 1000
}
POST /v1/player-coin
Authorization: Required
Content-Type: application/json
Request:
{
"amount": 100
}
Response:
{
"id": 125,
"playerID": "player1",
"amount": 1100,
"createdAt": "2024-03-15T10:40:00Z"
}
POST /v1/item-managing
Authorization: Admin Required
Content-Type: application/json
Request:
{
"name": "New Sword",
"description": "A powerful sword",
"picture": "https://example.com/new-sword.jpg",
"price": 200
}
PATCH /v1/item-managing/{itemID}
Authorization: Admin Required
Content-Type: application/json
Request:
{
"name": "Updated Sword",
"description": "An updated sword",
"picture": "https://example.com/updated-sword.jpg",
"price": 250
}
DELETE /v1/item-managing/{itemID}
Authorization: Admin Required
400 Bad Request
{
"error": "Invalid request parameters"
}
401 Unauthorized
{
"error": "Authentication required"
}
404 Not Found
{
"error": "Item not found"
}
422 Unprocessable Entity
{
"error": "coin not enough"
}
500 Internal Server Error
{
"error": "Internal server error"
}
GET /v1/oauth2/google/player/login
GET /v1/oauth2/google/admin/login
DELETE /v1/oauth2/logout
- Echo - Web framework
- GORM - ORM library
- Testify - Testing toolkit
- PostgreSQL - Database
- Supakorn - Initial work - supakornn
โญ๏ธ Star us on GitHub โ it helps!