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
|
|
|
|
|
|
|
"git.unistack.org/unistack-org/pkgdash/internal/models"
|
|
|
|
pb "git.unistack.org/unistack-org/pkgdash/proto"
|
2023-08-18 23:59:15 +03:00
|
|
|
"github.com/jmoiron/sqlx"
|
2023-08-16 13:17:42 +03:00
|
|
|
)
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
func RegisterStorage(name string, fn func(*sqlx.DB) interface{}) {
|
|
|
|
storages[name] = fn
|
2023-08-16 13:17:42 +03:00
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
var storages = map[string]func(*sqlx.DB) interface{}{}
|
2023-08-16 13:17:42 +03:00
|
|
|
|
|
|
|
type Storage interface {
|
2023-08-19 16:55:52 +03:00
|
|
|
PackagesModulesCreate(ctx context.Context, pkg *models.Package, modules []*models.Module) error
|
|
|
|
PackagesUpdateLastCheck(ctx context.Context, packages []*models.Package) error
|
|
|
|
PackagesModules(ctx context.Context, req *pb.PackagesModulesReq) ([]*models.Module, error)
|
|
|
|
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)
|
|
|
|
PackagesCreate(ctx context.Context, req *pb.PackagesCreateReq) (*models.Package, error)
|
2023-08-16 13:17:42 +03:00
|
|
|
PackagesList(ctx context.Context, req *pb.PackagesListReq) ([]*models.Package, error)
|
2023-08-18 23:59:15 +03:00
|
|
|
PackagesLookup(ctx context.Context, req *pb.PackagesLookupReq) (*models.Package, error)
|
|
|
|
PackagesUpdate(ctx context.Context, req *pb.PackagesUpdateReq) (*models.Package, error)
|
2023-08-16 13:17:42 +03:00
|
|
|
PackagesDelete(ctx context.Context, req *pb.PackagesDeleteReq) error
|
|
|
|
CommentsCreate(ctx context.Context, req *pb.CommentsCreateReq) (*models.Comment, error)
|
|
|
|
CommentsDelete(ctx context.Context, req *pb.CommentsDeleteReq) error
|
|
|
|
CommentsList(ctx context.Context, req *pb.CommentsListReq) ([]*models.Comment, error)
|
|
|
|
ModulesList(ctx context.Context, req *pb.ModulesListReq) ([]*models.Module, error)
|
2023-08-19 16:55:52 +03:00
|
|
|
ModulesCreate(ctx context.Context, modules []*models.Module) error
|
2023-08-16 13:17:42 +03:00
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
func NewStorage(name string, db *sqlx.DB) (Storage, error) {
|
2023-08-16 13:17:42 +03:00
|
|
|
function, ok := storages[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("incorrect name store")
|
|
|
|
}
|
2023-08-18 23:59:15 +03:00
|
|
|
store := function(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
|
|
|
|
}
|