made usage of first class citizen functions in go

This commit is contained in:
2025-01-03 15:23:33 +00:00
parent e021ab7370
commit f151fa7eae
5 changed files with 55 additions and 45 deletions
@@ -0,0 +1,14 @@
package repository
import (
"context"
E "github.com/IBM/fp-go/either"
)
// curried delegiert eine Methode mit Kontext und gibt eine Funktion zurück.
func curried[T any, R any](ctx context.Context, fn func(context.Context, T) E.Either[error, R]) func(T) E.Either[error, R] {
return func(input T) E.Either[error, R] {
return fn(ctx, input)
}
}
@@ -1,9 +1,8 @@
package repository
import (
"actatempus_backend/internal/domain/entities"
"actatempus_backend/internal/domain/repository"
"actatempus_backend/internal/domain/data"
"actatempus_backend/internal/domain/entities"
"context"
E "github.com/IBM/fp-go/either"
@@ -14,37 +13,32 @@ type UserRepositoryImpl struct {
dataSource data.UserDataSource
}
// NewUserRepository creates a new instance of UserRepositoryImpl.
func NewUserRepository(dataSource data.UserDataSource) repository.UserRepository {
return &UserRepositoryImpl{dataSource: dataSource}
// Create delegiert die Erstellung eines Benutzers an die Datenquelle.
func (r *UserRepositoryImpl) Create(ctx context.Context) func(user entities.UserCreate) E.Either[error, entities.User] {
return curried(ctx, r.dataSource.Create)
}
// Create delegates the creation of a user to the data source.
func (r *UserRepositoryImpl) Create(ctx context.Context, user entities.UserCreate) E.Either[error, entities.User] {
return r.dataSource.Create(ctx, user)
// FindByID delegiert das Abrufen eines Benutzers nach ID an die Datenquelle.
func (r *UserRepositoryImpl) FindByID(ctx context.Context) func(id string) E.Either[error, entities.User] {
return curried(ctx, r.dataSource.FindByID)
}
// FindByID delegates fetching a user by ID to the data source.
func (r *UserRepositoryImpl) FindByID(ctx context.Context, id string) E.Either[error, entities.User] {
return r.dataSource.FindByID(ctx, id)
// FindByEmail delegiert das Abrufen eines Benutzers nach E-Mail an die Datenquelle.
func (r *UserRepositoryImpl) FindByEmail(ctx context.Context) func(email string) E.Either[error, entities.User] {
return curried(ctx, r.dataSource.FindByEmail)
}
// FindByEmail delegates fetching a user by email to the data source.
func (r *UserRepositoryImpl) FindByEmail(ctx context.Context, email string) E.Either[error, entities.User] {
return r.dataSource.FindByEmail(ctx, email)
// Update delegiert das Aktualisieren eines Benutzers an die Datenquelle.
func (r *UserRepositoryImpl) Update(ctx context.Context) func(user entities.UserUpdate) E.Either[error, entities.User] {
return curried(ctx, r.dataSource.Update)
}
// Update delegates updating a user to the data source.
func (r *UserRepositoryImpl) Update(ctx context.Context, user entities.UserUpdate) E.Either[error, entities.User] {
return r.dataSource.Update(ctx, user)
// Delete delegiert das Löschen eines Benutzers an die Datenquelle.
func (r *UserRepositoryImpl) Delete(ctx context.Context) func(id string) E.Either[error, entities.User] {
return curried(ctx, r.dataSource.Delete)
}
// Delete delegates deleting a user to the data source.
func (r *UserRepositoryImpl) Delete(ctx context.Context, id string) E.Either[error, entities.User] {
return r.dataSource.Delete(ctx, id)
}
// FindAll delegates fetching all users to the data source.
// FindAll delegiert das Abrufen aller Benutzer an die Datenquelle.
func (r *UserRepositoryImpl) FindAll(ctx context.Context) E.Either[error, []entities.User] {
return r.dataSource.FindAll(ctx)
}