package model import ( "context" "errors" "fmt" "github.com/zeromicro/go-zero/core/stores/sqlc" "github.com/zeromicro/go-zero/core/stores/sqlx" ) var _ PermissionModel = (*customPermissionModel)(nil) type ( // PermissionModel is an interface to be customized, add more methods here, // and implement the added methods in customPermissionModel. PermissionModel interface { permissionModel FindAllOpenPermission(ctx context.Context) ([]*Permission, error) } customPermissionModel struct { *defaultPermissionModel } ) // NewPermissionModel 者裡不用快取版本,因為快取我想要自己控制,也就是 local cache 不想上升至 redis 的層級 // 因為我 permission 設計是由 sql 新增,重啟服務就可以重啟,或者是未來可以放一個mq 來同步 func NewPermissionModel(conn sqlx.SqlConn) PermissionModel { return &customPermissionModel{ defaultPermissionModel: newPermissionModel(conn), } } func (m *customPermissionModel) FindAllOpenPermission(ctx context.Context) ([]*Permission, error) { query := fmt.Sprintf("select %s from %s where `status` = ? order by `create_time` asc", permissionRows, m.table) var resp []*Permission err := m.conn.QueryRowsCtx(ctx, &resp, query, 1) switch { case err == nil: return resp, nil case errors.Is(err, sqlc.ErrNotFound): return nil, ErrNotFound default: return nil, err } }