pkgdash/internal/storage/storage.go
Vasiliy Tolstov 0e18a63f10 use worker
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-08-18 23:59:15 +03:00

45 lines
1.6 KiB
Go

package storage
import (
"context"
"errors"
"time"
"git.unistack.org/unistack-org/pkgdash/internal/models"
pb "git.unistack.org/unistack-org/pkgdash/proto"
"github.com/jmoiron/sqlx"
)
func RegisterStorage(name string, fn func(*sqlx.DB) interface{}) {
storages[name] = fn
}
var storages = map[string]func(*sqlx.DB) interface{}{}
type Storage interface {
PackagesProcess(ctx context.Context, td time.Duration) ([]*models.Package, error)
PackagesCreate(ctx context.Context, req *pb.PackagesCreateReq) (*models.Package, error)
PackagesList(ctx context.Context, req *pb.PackagesListReq) ([]*models.Package, error)
PackagesLookup(ctx context.Context, req *pb.PackagesLookupReq) (*models.Package, error)
PackagesUpdate(ctx context.Context, req *pb.PackagesUpdateReq) (*models.Package, error)
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)
PackagesModulesCreate(ctx context.Context, pkg *models.Package, modules []*models.Module) error
}
func NewStorage(name string, db *sqlx.DB) (Storage, error) {
function, ok := storages[name]
if !ok {
return nil, errors.New("incorrect name store")
}
store := function(db)
database, ok := store.(Storage)
if !ok {
return nil, errors.New("dont implements interface Storage")
}
return database, nil
}