feat: add tenant and user management with migrations, handlers, and tests

- Implemented tenant and user management features including:
  - Tenant creation and management with associated migrations.
  - User creation and management with associated migrations.
  - Tenant membership management with associated migrations.
  - Platform user roles management with associated migrations.
  - Quota management with associated migrations.
  - Article and template management with associated migrations.
- Added HTTP handlers for templates and workspaces.
- Created tests for protected and public routes.
- Introduced a script to check tenant scope in SQL queries.
- Documented task plan for backend completion and frontend foundation.
This commit is contained in:
2026-04-01 00:58:42 +08:00
commit de30497f59
210 changed files with 23733 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
package response
import "net/http"
func ErrBadRequest(code int, message, detail string) *AppError {
return &AppError{HTTPStatus: http.StatusBadRequest, Code: code, Message: message, Detail: detail}
}
func ErrUnauthorized(code int, message, detail string) *AppError {
return &AppError{HTTPStatus: http.StatusUnauthorized, Code: code, Message: message, Detail: detail}
}
func ErrForbidden(code int, message, detail string) *AppError {
return &AppError{HTTPStatus: http.StatusForbidden, Code: code, Message: message, Detail: detail}
}
func ErrNotFound(code int, message, detail string) *AppError {
return &AppError{HTTPStatus: http.StatusNotFound, Code: code, Message: message, Detail: detail}
}
func ErrConflict(code int, message, detail string) *AppError {
return &AppError{HTTPStatus: http.StatusConflict, Code: code, Message: message, Detail: detail}
}
func ErrTooManyRequests(code int, message, detail string) *AppError {
return &AppError{HTTPStatus: http.StatusTooManyRequests, Code: code, Message: message, Detail: detail}
}
func ErrInternal(code int, message, detail string) *AppError {
return &AppError{HTTPStatus: http.StatusInternalServerError, Code: code, Message: message, Detail: detail}
}
func ErrServiceUnavailable(code int, message, detail string) *AppError {
return &AppError{HTTPStatus: http.StatusServiceUnavailable, Code: code, Message: message, Detail: detail}
}
@@ -0,0 +1,70 @@
package response
import (
"net/http"
"github.com/gin-gonic/gin"
)
type AppError struct {
HTTPStatus int `json:"-"`
Code int `json:"code"`
Message string `json:"message"`
Detail string `json:"detail,omitempty"`
Cause error `json:"-"`
}
func (e *AppError) Error() string {
if e.Cause != nil {
return e.Cause.Error()
}
return e.Message
}
func Success(c *gin.Context, data interface{}) {
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "ok",
"data": data,
"request_id": getRequestID(c),
})
}
func SuccessWithStatus(c *gin.Context, status int, data interface{}) {
c.JSON(status, gin.H{
"code": 0,
"message": "ok",
"data": data,
"request_id": getRequestID(c),
})
}
func Error(c *gin.Context, err error) {
appErr := Normalize(err)
c.JSON(appErr.HTTPStatus, gin.H{
"code": appErr.Code,
"message": appErr.Message,
"detail": appErr.Detail,
"request_id": getRequestID(c),
})
}
func Normalize(err error) *AppError {
if appErr, ok := err.(*AppError); ok {
return appErr
}
return &AppError{
HTTPStatus: http.StatusInternalServerError,
Code: 50000,
Message: "internal_error",
Detail: err.Error(),
Cause: err,
}
}
func getRequestID(c *gin.Context) string {
if rid, ok := c.Get("request_id"); ok {
return rid.(string)
}
return ""
}