Add employee code assignment function

This commit is contained in:
2026-03-02 09:38:03 +08:00
parent 58b1ac1126
commit c29cbdf847
9 changed files with 962 additions and 9 deletions

View File

@@ -38,12 +38,51 @@ swag init -g main.go # Alternative command
## Code Style Guidelines
## Code Style Guidelines
### Project Structure
- **controllers/**: HTTP request handlers, use helper functions (GetCurrentUser, BindJSON, ErrorResponse, SuccessResponse)
- **services/**: Business logic layer, return errors to controllers
- **models/**: Data models with JSON and GORM tags, DTOs for API
- **middleware/**: JWT authentication and admin role checks
- **database/**: SQLite/PostgreSQL connection and migrations
```
backend-go/
├── config/ # Configuration management
│ └── config.go # Config loading (.env, config.yaml, env vars)
├── controllers/ # HTTP request handlers
│ ├── auth_controller.go # Auth: login, profile, password change
│ ├── companies_controller.go # Company CRUD
│ ├── employees_controller.go # Employee serials: generate, query, update, revoke, qrcode
│ ├── helper.go # Helper functions (GetCurrentUser, BindJSON, Response)
│ └── serials_controller.go # Company serials: generate, query, update, revoke, qrcode
├── database/ # Database connection and migrations
│ └── database.go # GORM init, AutoMigrate
├── docs/ # Swagger documentation (auto-generated)
├── logger/ # Structured logging
│ └── logger.go # Zap logger wrapper
├── middleware/ # Middleware
│ └── auth.go # JWT auth, Admin permission check
├── models/ # Data models and DTOs
│ └── models.go # User, Company, Serial, EmployeeSerial and DTOs
├── routes/ # Route configuration
│ └── routes.go # API route registration
├── services/ # Business logic layer
│ ├── auth_service.go # Auth: validate user, generate token, password management
│ ├── companies_service.go # Company CRUD
│ ├── employees_service.go # Employee serials: generate, query, update, revoke, qrcode
│ ├── serials_service.go # Company serials: generate, query, update, revoke, qrcode
│ └── services_test.go # Unit tests
├── tests/ # Integration tests
│ └── main_test.go # End-to-end tests
├── data/ # SQLite data directory
├── main.go # Application entry point
├── go.mod # Go dependencies
├── Makefile # Build tasks
└── .env.example # Environment variable example
```
### Layer Responsibilities
- **controllers/**: Receive HTTP requests, validate params, call services, return responses
- **services/**: Business logic, data processing, database interaction, return results or errors
- **models/**: Data structure definitions, GORM models, API request/response DTOs
- **middleware/**: Authentication and authorization
- **routes/**: Route registration, connect controllers to router
### Import Organization
Standard imports followed by third-party imports, then project imports (sorted alphabetically):
@@ -61,8 +100,8 @@ import (
```
### Naming Conventions
- **Controllers**: `AuthController`, `SerialsController`, `CompaniesController`
- **Services**: `AuthService`, `SerialsService`, `CompaniesService`
- **Controllers**: `AuthController`, `SerialsController`, `CompaniesController`, `EmployeeSerialsController`
- **Services**: `AuthService`, `SerialsService`, `CompaniesService`, `EmployeeSerialsService`
- **Models**: `User`, `Company`, `Serial` (use PascalCase for exported structs)
- **DTOs**: `LoginDTO`, `ChangePasswordDTO`, `UpdateProfileDTO` (DTO suffix)
- **Functions**: `ValidateUser`, `Generate`, `Query` (PascalCase for exported)