Skip to content

Commit

Permalink
refactor: usecase structure
Browse files Browse the repository at this point in the history
  • Loading branch information
halimath committed May 11, 2024
1 parent df8e410 commit b5c0324
Show file tree
Hide file tree
Showing 26 changed files with 1,066 additions and 882 deletions.
16 changes: 16 additions & 0 deletions backend/internal/domain/domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Package domain contains common values used accross the whole domain implementation.
package domain

import "errors"

var (
// ErrNotFound is a sentinel error value returned when an entity is not found.
ErrNotFound = errors.New("not found")

// ErrForbidden is a sentinel error value returned when an operation is forbidden.
ErrForbidden = errors.New("forbidden")

// ErrInvalidCharacter is a sentinel error value returned when an operation targets a character and that
// character does not exist or is otherwise invalid.
ErrInvalidCharacter = errors.New("invlid character")
)
35 changes: 35 additions & 0 deletions backend/internal/domain/ports/repository/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Package repository defines the port interface for a session repository to
// abstract persistence access for a session.
package repository

import (
"context"
"errors"

"github.com/halimath/fate-core-remote-table/backend/internal/domain/session"
)

// NoSave is a sentinel value returned to signal, that a repository must not be
// saved in the datastore.
var NoSave = errors.New("no save")

// UnitOfWork defines a function type that defines the interface for callback
// functions that perform some atomic unit of work on a [session.Session].
// ctx is used to define deadlines and additional context values. exists signals
// whether the s exists in the datastore. s contains the current values for the
// session.
//
// The function returns a session.Session to update the datastore with if the
// returned error is nil. If the error is the sentinal value NoSave, then no
// insert/update is exectued and the returned session is ignored. Any other
// non-nil error value causes no update and is propagated to any caller.
type UnitOfWork func(ctx context.Context, exists bool, s session.Session) (session.Session, error)

// Port defines the port interface for repository implementations
// managing a session.
type Port interface {
// Perform executes uow. It first tries to load the session.Session identified
// by id and passes that information to uow. It returns an error returned
// from uow or from the underlying datastore.
Perform(ctx context.Context, id string, uow UnitOfWork) error
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package repositorymock

import (
"context"
"errors"

"github.com/halimath/fate-core-remote-table/backend/internal/domain/ports/repository"
"github.com/halimath/fate-core-remote-table/backend/internal/domain/session"
)

type Mock struct {
S session.Session
}

func (r *Mock) Perform(ctx context.Context, id string, uow repository.UnitOfWork) error {
s, err := uow(ctx, r.S.ID == id, r.S)
if errors.Is(err, repository.NoSave) {
return nil
}

if err != nil {
return err
}

r.S = s

return nil
}
294 changes: 0 additions & 294 deletions backend/internal/domain/usecase/usecase.go

This file was deleted.

Loading

0 comments on commit b5c0324

Please sign in to comment.