2023-08-16 13:17:42 +03:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2023-08-18 23:59:15 +03:00
|
|
|
"time"
|
2023-08-16 13:17:42 +03:00
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
"github.com/jmoiron/sqlx"
|
2024-12-07 02:35:30 +03:00
|
|
|
"go.unistack.org/micro/v3/logger"
|
|
|
|
"go.unistack.org/pkgdash/internal/models"
|
|
|
|
pb "go.unistack.org/pkgdash/proto"
|
2023-08-16 13:17:42 +03:00
|
|
|
)
|
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
func RegisterStorage(name string, fn func(logger.Logger, *sqlx.DB) interface{}) {
|
2023-08-18 23:59:15 +03:00
|
|
|
storages[name] = fn
|
2023-08-16 13:17:42 +03:00
|
|
|
}
|
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
var storages = map[string]func(logger.Logger, *sqlx.DB) interface{}{}
|
2023-08-16 13:17:42 +03:00
|
|
|
|
|
|
|
type Storage interface {
|
2023-08-20 14:19:57 +03:00
|
|
|
PackageModulesCreate(ctx context.Context, pkg *models.Package, modules []*models.Module) error
|
2023-08-19 16:55:52 +03:00
|
|
|
PackagesUpdateLastCheck(ctx context.Context, packages []*models.Package) error
|
2023-08-20 14:19:57 +03:00
|
|
|
PackageModules(ctx context.Context, req *pb.PackageModulesReq) ([]*models.Module, error)
|
2023-08-19 16:55:52 +03:00
|
|
|
ModulesProcess(ctx context.Context, td time.Duration) ([]*models.Module, error)
|
2023-08-18 23:59:15 +03:00
|
|
|
PackagesProcess(ctx context.Context, td time.Duration) ([]*models.Package, error)
|
2023-08-20 14:19:57 +03:00
|
|
|
PackageCreate(ctx context.Context, req *pb.PackageCreateReq) (*models.Package, error)
|
2023-09-23 21:16:26 +03:00
|
|
|
HandlerList(ctx context.Context, req *pb.HandlerListReq) ([]*models.Handler, error)
|
2023-08-20 14:19:57 +03:00
|
|
|
PackageList(ctx context.Context, req *pb.PackageListReq) ([]*models.Package, error)
|
|
|
|
PackageLookup(ctx context.Context, req *pb.PackageLookupReq) (*models.Package, error)
|
|
|
|
PackageUpdate(ctx context.Context, req *pb.PackageUpdateReq) (*models.Package, error)
|
|
|
|
PackageDelete(ctx context.Context, req *pb.PackageDeleteReq) error
|
|
|
|
CommentCreate(ctx context.Context, req *pb.CommentCreateReq) (*models.Comment, error)
|
|
|
|
CommentDelete(ctx context.Context, req *pb.CommentDeleteReq) error
|
|
|
|
CommentList(ctx context.Context, req *pb.CommentListReq) ([]*models.Comment, error)
|
|
|
|
ModuleList(ctx context.Context, req *pb.ModuleListReq) ([]*models.Module, error)
|
|
|
|
ModuleCreate(ctx context.Context, modules []*models.Module) error
|
2023-08-16 13:17:42 +03:00
|
|
|
}
|
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
func NewStorage(name string, log logger.Logger, db *sqlx.DB) (Storage, error) {
|
|
|
|
fn, ok := storages[name]
|
2023-08-16 13:17:42 +03:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("incorrect name store")
|
|
|
|
}
|
2024-12-07 02:35:30 +03:00
|
|
|
store := fn(log, db)
|
2023-08-16 13:17:42 +03:00
|
|
|
database, ok := store.(Storage)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("dont implements interface Storage")
|
|
|
|
}
|
|
|
|
return database, nil
|
|
|
|
}
|