Skip to content

๐ŸŽฎ REST API for game shop management with inventory and virtual currency systems.

Notifications You must be signed in to change notification settings

Supakornn/game-shop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

35 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽฎ Game Shop API

A modern REST API for managing in-game item transactions, inventory, and virtual currency. Built with Go and clean architecture principles.

โœจ Key Features

  • ๐Ÿ›๏ธ 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

๐Ÿ—๏ธ Clean Architecture

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

๐Ÿงช Testing

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
    })
}

๐Ÿ“ API Documentation

Authentication

All endpoints require OAuth2 authentication with Google.

Item Shop Endpoints

List Items

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
    }
}

Buy Item

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"
}

Sell Item

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"
}

Inventory Endpoints

List Player Inventory

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
        }
    ]
}

Player Coin Endpoints

Get Coin Balance

GET /v1/player-coin
Authorization: Required

Response:
{
    "playerID": "player1",
    "coin": 1000
}

Add Coins

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"
}

Admin Endpoints

Create Item

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
}

Edit Item

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
}

Archive Item

DELETE /v1/item-managing/{itemID}
Authorization: Admin Required

Error Responses

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"
}

Authentication Endpoints

Player Login

GET /v1/oauth2/google/player/login

Admin Login

GET /v1/oauth2/google/admin/login

Logout

DELETE /v1/oauth2/logout

๐Ÿ› ๏ธ Built With

๐Ÿ‘ฅ Authors

โญ๏ธ Star us on GitHub โ€” it helps!

About

๐ŸŽฎ REST API for game shop management with inventory and virtual currency systems.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages