Re-migrate code

This commit is contained in:
2026-03-02 10:24:11 +08:00
parent 51025195a5
commit f80f2b43ce
6 changed files with 479 additions and 103 deletions

View File

@@ -38,8 +38,6 @@ swag init -g main.go # Alternative command
## Code Style Guidelines
## Code Style Guidelines
### Project Structure
```
backend-go/
@@ -84,6 +82,12 @@ backend-go/
- **middleware/**: Authentication and authorization
- **routes/**: Route registration, connect controllers to router
### API Surface (Current)
- **Auth**: `POST /api/auth/login`, `POST /api/auth/logout`, `GET /api/auth/profile`, `PUT /api/auth/profile`, `POST /api/auth/change-password`
- **Serials**: `POST /api/serials/generate`, `POST /api/serials/generate-with-prefix`, `POST /api/serials/:serialNumber/qrcode`, `GET /api/serials/:serialNumber/query`, `GET /api/serials`, `PATCH /api/serials/:serialNumber`, `PUT /api/serials/:serialNumber`, `POST /api/serials/:serialNumber/revoke`
- **Companies**: `GET /api/companies/stats/overview`, `GET /api/companies`, `GET /api/companies/:companyName`, `POST /api/companies`, `PATCH /api/companies/:companyName`, `PUT /api/companies/:companyName`, `POST /api/companies/:companyName/revoke`, `DELETE /api/companies/:companyName/serials/:serialNumber`, `DELETE /api/companies/:companyName`
- **Employee Serials**: `POST /api/employee-serials/generate`, `POST /api/employee-serials/:serialNumber/qrcode`, `GET /api/employee-serials/:serialNumber/query`, `GET /api/employee-serials`, `PATCH /api/employee-serials/:serialNumber`, `PUT /api/employee-serials/:serialNumber`, `POST /api/employee-serials/:serialNumber/revoke`
### Import Organization
Standard imports followed by third-party imports, then project imports (sorted alphabetically):
```go
@@ -175,6 +179,7 @@ logger.Fatal("致命错误", logger.Err(err))
- Always check for errors: `if err != nil { ... }`
- Use Unscoped for permanent deletion: `database.DB.Unscoped().Delete(...)`
- Test cleanup: `database.DB.Unscoped().Where("1 = 1").Delete(&models.User{})`
- During `AutoMigrate()`, default admin is seeded only when user table is empty
### Testing
- Use `testify/assert` for assertions
@@ -203,7 +208,7 @@ After modifying Swagger annotations, run `make swagger`.
### Configuration
- Load with `config.LoadConfig()`
- Access with `config.GetAppConfig()`
- Environment variables: `APP_SERVER_PORT`, `APP_DATABASE_DRIVER`, etc.
- Environment variables must use `APP_` prefix (e.g. `APP_SERVER_PORT`, `APP_DATABASE_DRIVER`, `APP_DATABASE_SQLITE_PATH`, `APP_JWT_SECRET`)
- .env file for local development (not committed)
### Middleware