Skip to content

Commit

Permalink
refactor: introduce repo layer
Browse files Browse the repository at this point in the history
  • Loading branch information
dongwlin committed Jan 18, 2025
1 parent e6f7532 commit 10371e8
Show file tree
Hide file tree
Showing 11 changed files with 329 additions and 283 deletions.
18 changes: 1 addition & 17 deletions internal/handler/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,19 @@ type VersionHandler struct {
logger *zap.Logger
resourceLogic *logic.ResourceLogic
versionLogic *logic.VersionLogic
storageLogic *logic.StorageLogic
}

func NewVersionHandler(
conf *config.Config,
logger *zap.Logger,
resourceLogic *logic.ResourceLogic,
versionLogic *logic.VersionLogic,
storageLogic *logic.StorageLogic,
) *VersionHandler {
return &VersionHandler{
conf: conf,
logger: logger,
resourceLogic: resourceLogic,
versionLogic: versionLogic,
storageLogic: storageLogic,
}
}

Expand Down Expand Up @@ -239,7 +236,7 @@ func (h *VersionHandler) Create(c *fiber.Ctx) error {
Name: name,
UploadArchivePath: tempPath,
}
version, saveDir, err := h.versionLogic.Create(ctx, createVersionParam)
version, err := h.versionLogic.Create(ctx, createVersionParam)
if err != nil {
h.logger.Error("Failed to create version",
zap.Error(err),
Expand All @@ -248,19 +245,6 @@ func (h *VersionHandler) Create(c *fiber.Ctx) error {
return c.Status(fiber.StatusInternalServerError).JSON(resp)
}

createStorageParam := CreateStorageParam{
VersionID: version.ID,
Directory: saveDir,
}
_, err = h.storageLogic.Create(ctx, createStorageParam)
if err != nil {
h.logger.Error("Failed to create storage",
zap.Error(err),
)
resp := response.UnexpectedError()
return c.Status(fiber.StatusInternalServerError).JSON(resp)
}

data := CreateVersionResponseData{
ID: version.ID,
Name: version.Name,
Expand Down
58 changes: 8 additions & 50 deletions internal/logic/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,29 @@ import (
"context"

. "github.com/MirrorChyan/resource-backend/internal/model"
"github.com/MirrorChyan/resource-backend/internal/repo"

"github.com/MirrorChyan/resource-backend/internal/ent"
"github.com/MirrorChyan/resource-backend/internal/ent/resource"

"go.uber.org/zap"
)

type ResourceLogic struct {
logger *zap.Logger
db *ent.Client
logger *zap.Logger
resourceRepo *repo.Resource
}

func NewResourceLogic(logger *zap.Logger, db *ent.Client) *ResourceLogic {
func NewResourceLogic(logger *zap.Logger, resourceRepo *repo.Resource) *ResourceLogic {
return &ResourceLogic{
logger: logger,
db: db,
logger: logger,
resourceRepo: resourceRepo,
}
}

func (l *ResourceLogic) Create(ctx context.Context, param CreateResourceParam) (*ent.Resource, error) {
return l.db.Resource.Create().
SetID(param.ID).
SetName(param.Name).
SetDescription(param.Description).
Save(ctx)
return l.resourceRepo.CreateResource(ctx, param.ID, param.Name, param.Description)
}

func (l *ResourceLogic) Exists(ctx context.Context, id string) (bool, error) {
return l.db.Resource.Query().
Where(resource.ID(id)).
Exist(ctx)
}

func (l *ResourceLogic) GetByID(ctx context.Context, id string) (*ent.Resource, error) {
return l.db.Resource.Get(ctx, id)
}

func (l *ResourceLogic) List(ctx context.Context, param ListVersionParam) (int, []*ent.Resource, error) {
query := l.db.Resource.Query()

count, err := query.Count(ctx)
if err != nil {
return 0, nil, err
}

resources, err := query.
Offset(param.Offset).
Limit(param.Limit).
All(ctx)
if err != nil {
return 0, nil, err
}

return count, resources, nil
}

func (l *ResourceLogic) Update(ctx context.Context, param UpdateResourceParam) (*ent.Resource, error) {
return l.db.Resource.UpdateOneID(param.ID).
SetName(param.Name).
SetDescription(param.Description).
Save(ctx)
}

func (l *ResourceLogic) Delete(ctx context.Context, id string) error {
return l.db.Resource.
DeleteOneID(id).
Exec(ctx)
return l.resourceRepo.CheckResourceExistsByID(ctx, id)
}
35 changes: 0 additions & 35 deletions internal/logic/storage.go

This file was deleted.

Loading

0 comments on commit 10371e8

Please sign in to comment.