refactor functional styple in data sources

This commit is contained in:
Jean Jacques Avril 2025-01-04 11:10:33 +00:00
parent 05fdefc3e2
commit bded64757a
No known key found for this signature in database
6 changed files with 259 additions and 376 deletions

View File

@ -6,31 +6,31 @@ import (
"crypto/sha256" "crypto/sha256"
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt"
E "github.com/IBM/fp-go/either" E "github.com/IBM/fp-go/either"
O "github.com/IBM/fp-go/option"
) )
// Dereference safely dereferences a pointer within an Option. func TryDereference[T any](ptr *T) E.Either[error, T] {
// If the Option is None, it returns None. Otherwise, it dereferences the pointer and wraps the value. if ptr == nil {
func Dereference[T any](opt O.Option[*T]) O.Option[T] { return E.Left[T](error(app_error.NewInternalError(fmt.Errorf("Could not dereference"))))
return O.Map(func(ptr *T) T {
return *ptr
})(opt)
}
// FromOptionWithError wraps E.FromOption and simplifies error creation.
func FromOptionWithError[A any](err error) func(O.Option[A]) E.Either[error, A] {
return func(opt O.Option[A]) E.Either[error, A] {
return E.FromOption[A](func() error { return err })(opt)
} }
return E.Right[error](*ptr)
} }
func handleDBError(err error, notFoundMessage string) error { func WrapAppError(err error) error {
return app_error.NewInternalError(err)
}
func WrapDBError(notFoundMessage string) func(error) error {
return func(err error) error {
if errors.Is(err, db.ErrNotFound) { if errors.Is(err, db.ErrNotFound) {
return app_error.NewNotFoundError(notFoundMessage) return app_error.NewNotFoundError(notFoundMessage)
} }
return app_error.NewInternalError(err) return app_error.NewInternalError(err)
}
} }
func NullableField[T any](getter func() (T, bool)) *T { func NullableField[T any](getter func() (T, bool)) *T {

View File

@ -1,7 +1,6 @@
package data package data
import ( import (
"actatempus_backend/internal/domain/app_error"
"actatempus_backend/internal/domain/entities" "actatempus_backend/internal/domain/entities"
"actatempus_backend/internal/infrastructure/data/db" "actatempus_backend/internal/infrastructure/data/db"
"context" "context"
@ -10,7 +9,6 @@ import (
A "github.com/IBM/fp-go/array" A "github.com/IBM/fp-go/array"
E "github.com/IBM/fp-go/either" E "github.com/IBM/fp-go/either"
F "github.com/IBM/fp-go/function" F "github.com/IBM/fp-go/function"
O "github.com/IBM/fp-go/option"
) )
type PrismaProjectDataSource struct { type PrismaProjectDataSource struct {
@ -23,50 +21,42 @@ func NewPrismaProjectDataSource(client *db.PrismaClient) *PrismaProjectDataSourc
// Create a new project // Create a new project
func (ds *PrismaProjectDataSource) Create(ctx context.Context, project entities.ProjectCreate) E.Either[error, entities.Project] { func (ds *PrismaProjectDataSource) Create(ctx context.Context, project entities.ProjectCreate) E.Either[error, entities.Project] {
createdProject, err := ds.client.ProjectDbo.CreateOne( return F.Flow4(
E.Eitherize1(ds.client.ProjectDbo.CreateOne(
db.ProjectDbo.Name.Set(project.Name), db.ProjectDbo.Name.Set(project.Name),
db.ProjectDbo.User.Link( db.ProjectDbo.User.Link(
db.UserDbo.ID.Equals(project.UserID), db.UserDbo.ID.Equals(project.UserID),
), ),
db.ProjectDbo.Description.SetIfPresent(project.Description), db.ProjectDbo.Description.SetIfPresent(project.Description),
db.ProjectDbo.ClientID.SetIfPresent(project.ClientID), db.ProjectDbo.ClientID.SetIfPresent(project.ClientID),
).Exec(ctx) ).Exec),
E.MapLeft[*db.ProjectDboModel](WrapDBError("Could not create project")),
if err != nil { E.Chain(
return E.Left[entities.Project, error](app_error.NewInternalError(err)) TryDereference[db.ProjectDboModel],
} ),
return F.Pipe3(
O.FromNillable(createdProject),
Dereference,
FromOptionWithError[db.ProjectDboModel](app_error.NewInternalError(fmt.Errorf("Could not create project"))),
E.Map[error](mapPrismaProjectToDomain), E.Map[error](mapPrismaProjectToDomain),
) )(ctx)
} }
// FindByID retrieves a project by ID // FindByID retrieves a project by ID
func (ds *PrismaProjectDataSource) FindByID(ctx context.Context, id string) E.Either[error, entities.Project] { func (ds *PrismaProjectDataSource) FindByID(ctx context.Context, id string) E.Either[error, entities.Project] {
project, err := ds.client.ProjectDbo.FindUnique(
return F.Flow4(
E.Eitherize1(ds.client.ProjectDbo.FindUnique(
db.ProjectDbo.ID.Equals(id), db.ProjectDbo.ID.Equals(id),
).Exec(ctx) ).Exec),
E.MapLeft[*db.ProjectDboModel](WrapDBError(fmt.Sprintf("Could not retrieve project with ID %s", id))),
if err != nil { E.Chain(
return E.Left[entities.Project](handleDBError(err, fmt.Sprintf("Project with ID %s not found", id))) TryDereference[db.ProjectDboModel],
} ),
return F.Pipe3(
O.FromNillable(project),
Dereference,
FromOptionWithError[db.ProjectDboModel](app_error.NewNotFoundError(fmt.Sprintf("Project with ID %s not found", id))),
E.Map[error](mapPrismaProjectToDomain), E.Map[error](mapPrismaProjectToDomain),
) )(ctx)
} }
// Update updates a project // Update updates a project
func (ds *PrismaProjectDataSource) Update(ctx context.Context, project entities.ProjectUpdate) E.Either[error, entities.Project] { func (ds *PrismaProjectDataSource) Update(ctx context.Context, project entities.ProjectUpdate) E.Either[error, entities.Project] {
return F.Flow4(
updatedProject, err := ds.client.ProjectDbo.FindUnique( E.Eitherize1(ds.client.ProjectDbo.FindUnique(
db.ProjectDbo.ID.Equals(project.ID), db.ProjectDbo.ID.Equals(project.ID),
).Update( ).Update(
db.ProjectDbo.Name.SetIfPresent(project.Name), db.ProjectDbo.Name.SetIfPresent(project.Name),
@ -75,64 +65,46 @@ func (ds *PrismaProjectDataSource) Update(ctx context.Context, project entities.
db.ProjectDbo.User.Link( db.ProjectDbo.User.Link(
db.UserDbo.ID.EqualsIfPresent(project.UserID), db.UserDbo.ID.EqualsIfPresent(project.UserID),
), ),
).Exec(ctx) ).Exec),
E.MapLeft[*db.ProjectDboModel](WrapDBError(fmt.Sprintf("Could not update project with ID %s", project.ID))),
if err != nil { E.Chain(
return E.Left[entities.Project](handleDBError(err, fmt.Sprintf("Could not update project with ID %s", project.ID))) TryDereference[db.ProjectDboModel],
} ),
return F.Pipe3(
O.FromNillable(updatedProject),
Dereference,
FromOptionWithError[db.ProjectDboModel](app_error.NewNotFoundError(fmt.Sprintf("Project with ID %s not found", project.ID))),
E.Map[error](mapPrismaProjectToDomain), E.Map[error](mapPrismaProjectToDomain),
) )(ctx)
} }
// Delete removes a project // Delete removes a project
func (ds *PrismaProjectDataSource) Delete(ctx context.Context, id string) E.Either[error, entities.Project] { func (ds *PrismaProjectDataSource) Delete(ctx context.Context, id string) E.Either[error, entities.Project] {
deleted, err := ds.client.ProjectDbo.FindUnique(
return F.Flow4(
E.Eitherize1(ds.client.ProjectDbo.FindUnique(
db.ProjectDbo.ID.Equals(id), db.ProjectDbo.ID.Equals(id),
).Delete().Exec(ctx) ).Delete().Exec),
E.MapLeft[*db.ProjectDboModel](WrapDBError(fmt.Sprintf("Could not delete project with ID %s", id))),
if err != nil { E.Chain(
return E.Left[entities.Project](handleDBError(err, fmt.Sprintf("Could not delete project with ID %s", id))) TryDereference[db.ProjectDboModel],
} ),
return F.Pipe3(
O.FromNillable(deleted),
Dereference,
FromOptionWithError[db.ProjectDboModel](app_error.NewNotFoundError(fmt.Sprintf("Project with ID %s not found", id))),
E.Map[error](mapPrismaProjectToDomain), E.Map[error](mapPrismaProjectToDomain),
) )(ctx)
} }
// FindAll retrieves all projects // FindAll retrieves all projects
func (ds *PrismaProjectDataSource) FindAll(ctx context.Context) E.Either[error, []entities.Project] { func (ds *PrismaProjectDataSource) FindAll(ctx context.Context) E.Either[error, []entities.Project] {
projects, err := ds.client.ProjectDbo.FindMany().Exec(ctx) return F.Flow3(
if err != nil { E.Eitherize1(ds.client.ProjectDbo.FindMany().Exec),
return E.Left[[]entities.Project](handleDBError(err, "Could not retrieve projects")) E.MapLeft[[]db.ProjectDboModel](WrapAppError),
} E.Map[error](A.Map(mapPrismaProjectToDomain)),
)(ctx)
return F.Pipe2(
projects,
A.Map(mapPrismaProjectToDomain),
E.Right[error],
)
} }
// FindByUserID retrieves all projects for a user // FindByUserID retrieves all projects for a user
func (ds *PrismaProjectDataSource) FindByUserID(ctx context.Context, userID string) E.Either[error, []entities.Project] { func (ds *PrismaProjectDataSource) FindByUserID(ctx context.Context, userID string) E.Either[error, []entities.Project] {
projects, err := ds.client.ProjectDbo.FindMany( return F.Flow3(
E.Eitherize1(ds.client.ProjectDbo.FindMany(
db.ProjectDbo.UserID.Equals(userID), db.ProjectDbo.UserID.Equals(userID),
).Exec(ctx) ).Exec),
if err != nil { E.MapLeft[[]db.ProjectDboModel](WrapDBError(fmt.Sprintf("Could not retrieve projects for user with ID %s", userID))),
return E.Left[[]entities.Project](handleDBError(err, fmt.Sprintf("Could not retrieve projects for user with ID %s", userID))) E.Map[error](A.Map(mapPrismaProjectToDomain)),
} )(ctx)
return F.Pipe2(
projects,
A.Map(mapPrismaProjectToDomain),
E.Right[error],
)
} }

View File

@ -1,7 +1,6 @@
package data package data
import ( import (
"actatempus_backend/internal/domain/app_error"
"actatempus_backend/internal/domain/entities" "actatempus_backend/internal/domain/entities"
"actatempus_backend/internal/infrastructure/data/db" "actatempus_backend/internal/infrastructure/data/db"
"context" "context"
@ -10,7 +9,6 @@ import (
A "github.com/IBM/fp-go/array" A "github.com/IBM/fp-go/array"
E "github.com/IBM/fp-go/either" E "github.com/IBM/fp-go/either"
F "github.com/IBM/fp-go/function" F "github.com/IBM/fp-go/function"
O "github.com/IBM/fp-go/option"
) )
type PrismaProjectTaskDataSource struct { type PrismaProjectTaskDataSource struct {
@ -23,47 +21,40 @@ func NewPrismaProjectTaskDataSource(client *db.PrismaClient) *PrismaProjectTaskD
// Create a new ProjectTask // Create a new ProjectTask
func (ds *PrismaProjectTaskDataSource) Create(ctx context.Context, task entities.ProjectTaskCreate) E.Either[error, entities.ProjectTask] { func (ds *PrismaProjectTaskDataSource) Create(ctx context.Context, task entities.ProjectTaskCreate) E.Either[error, entities.ProjectTask] {
createdTask, err := ds.client.ProjectTaskDbo.CreateOne( return F.Flow4(
E.Eitherize1(ds.client.ProjectTaskDbo.CreateOne(
db.ProjectTaskDbo.Name.Set(task.Name), db.ProjectTaskDbo.Name.Set(task.Name),
db.ProjectTaskDbo.Project.Link( db.ProjectTaskDbo.Project.Link(
db.ProjectDbo.ID.Equals(task.ProjectID), db.ProjectDbo.ID.Equals(task.ProjectID),
), ),
db.ProjectTaskDbo.Description.SetIfPresent(task.Description), db.ProjectTaskDbo.Description.SetIfPresent(task.Description),
).Exec(ctx) ).Exec),
E.MapLeft[*db.ProjectTaskDboModel](WrapDBError("Could not create project task")),
if err != nil { E.Chain(
return E.Left[entities.ProjectTask, error](handleDBError(err, fmt.Sprintf("Could not create project task"))) TryDereference[db.ProjectTaskDboModel],
} ),
return F.Pipe3(
O.FromNillable(createdTask),
Dereference,
FromOptionWithError[db.ProjectTaskDboModel](app_error.NewInternalError(fmt.Errorf("Could not create project task"))),
E.Map[error](mapPrismaProjectTaskToDomain), E.Map[error](mapPrismaProjectTaskToDomain),
) )(ctx)
} }
// Find ProjectTask by ID // Find ProjectTask by ID
func (ds *PrismaProjectTaskDataSource) FindByID(ctx context.Context, id string) E.Either[error, entities.ProjectTask] { func (ds *PrismaProjectTaskDataSource) FindByID(ctx context.Context, id string) E.Either[error, entities.ProjectTask] {
task, err := ds.client.ProjectTaskDbo.FindUnique( return F.Flow4(
E.Eitherize1(ds.client.ProjectTaskDbo.FindUnique(
db.ProjectTaskDbo.ID.Equals(id), db.ProjectTaskDbo.ID.Equals(id),
).Exec(ctx) ).Exec),
E.MapLeft[*db.ProjectTaskDboModel](WrapDBError(fmt.Sprintf("Could not retrieve project task with ID %s", id))),
if err != nil { E.Chain(
return E.Left[entities.ProjectTask](handleDBError(err, fmt.Sprintf("ProjectTask with ID %s not found", id))) TryDereference[db.ProjectTaskDboModel],
} ),
return F.Pipe3(
O.FromNillable(task),
Dereference,
FromOptionWithError[db.ProjectTaskDboModel](app_error.NewNotFoundError(fmt.Sprintf("ProjectTask with ID %s not found", id))),
E.Map[error](mapPrismaProjectTaskToDomain), E.Map[error](mapPrismaProjectTaskToDomain),
) )(ctx)
} }
// Update an existing ProjectTask // Update an existing ProjectTask
func (ds *PrismaProjectTaskDataSource) Update(ctx context.Context, task entities.ProjectTaskUpdate) E.Either[error, entities.ProjectTask] { func (ds *PrismaProjectTaskDataSource) Update(ctx context.Context, task entities.ProjectTaskUpdate) E.Either[error, entities.ProjectTask] {
updatedTask, err := ds.client.ProjectTaskDbo.FindUnique( return F.Flow4(
E.Eitherize1(ds.client.ProjectTaskDbo.FindUnique(
db.ProjectTaskDbo.ID.Equals(task.ID), db.ProjectTaskDbo.ID.Equals(task.ID),
).Update( ).Update(
db.ProjectTaskDbo.Name.SetIfPresent(task.Name), db.ProjectTaskDbo.Name.SetIfPresent(task.Name),
@ -71,64 +62,45 @@ func (ds *PrismaProjectTaskDataSource) Update(ctx context.Context, task entities
db.ProjectTaskDbo.Project.Link( db.ProjectTaskDbo.Project.Link(
db.ProjectDbo.ID.EqualsIfPresent(task.ProjectID), db.ProjectDbo.ID.EqualsIfPresent(task.ProjectID),
), ),
).Exec(ctx) ).Exec),
E.MapLeft[*db.ProjectTaskDboModel](WrapDBError(fmt.Sprintf("Could not update project task with ID %s", task.ID))),
if err != nil { E.Chain(
return E.Left[entities.ProjectTask](handleDBError(err, fmt.Sprintf("Could not update project task with ID %s", task.ID))) TryDereference[db.ProjectTaskDboModel],
} ),
return F.Pipe3(
O.FromNillable(updatedTask),
Dereference,
FromOptionWithError[db.ProjectTaskDboModel](app_error.NewNotFoundError(fmt.Sprintf("ProjectTask with ID %s not found", task.ID))),
E.Map[error](mapPrismaProjectTaskToDomain), E.Map[error](mapPrismaProjectTaskToDomain),
) )(ctx)
} }
// Delete a ProjectTask // Delete a ProjectTask
func (ds *PrismaProjectTaskDataSource) Delete(ctx context.Context, id string) E.Either[error, entities.ProjectTask] { func (ds *PrismaProjectTaskDataSource) Delete(ctx context.Context, id string) E.Either[error, entities.ProjectTask] {
deletedTask, err := ds.client.ProjectTaskDbo.FindUnique( return F.Flow4(
E.Eitherize1(ds.client.ProjectTaskDbo.FindUnique(
db.ProjectTaskDbo.ID.Equals(id), db.ProjectTaskDbo.ID.Equals(id),
).Delete().Exec(ctx) ).Delete().Exec),
E.MapLeft[*db.ProjectTaskDboModel](WrapDBError(fmt.Sprintf("Could not delete project task with ID %s", id))),
if err != nil { E.Chain(
return E.Left[entities.ProjectTask](handleDBError(err, fmt.Sprintf("Could not delete project task with ID %s", id))) TryDereference[db.ProjectTaskDboModel],
} ),
return F.Pipe3(
O.FromNillable(deletedTask),
Dereference,
FromOptionWithError[db.ProjectTaskDboModel](app_error.NewNotFoundError(fmt.Sprintf("ProjectTask with ID %s not found", id))),
E.Map[error](mapPrismaProjectTaskToDomain), E.Map[error](mapPrismaProjectTaskToDomain),
) )(ctx)
} }
// FindAll retrieves all ProjectTasks // FindAll retrieves all ProjectTasks
func (ds *PrismaProjectTaskDataSource) FindAll(ctx context.Context) E.Either[error, []entities.ProjectTask] { func (ds *PrismaProjectTaskDataSource) FindAll(ctx context.Context) E.Either[error, []entities.ProjectTask] {
tasks, err := ds.client.ProjectTaskDbo.FindMany().Exec(ctx) return F.Flow3(
if err != nil { E.Eitherize1(ds.client.ProjectTaskDbo.FindMany().Exec),
return E.Left[[]entities.ProjectTask](handleDBError(err, "Could not retrieve project tasks")) E.MapLeft[[]db.ProjectTaskDboModel](WrapDBError("Could not retrieve project tasks")),
} E.Map[error](A.Map(mapPrismaProjectTaskToDomain)),
)(ctx)
return F.Pipe2(
tasks,
A.Map(mapPrismaProjectTaskToDomain),
E.Right[error],
)
} }
// FindByProjectID retrieves all ProjectTasks for a given Project // FindByProjectID retrieves all ProjectTasks for a given Project
func (ds *PrismaProjectTaskDataSource) FindByProjectID(ctx context.Context, projectID string) E.Either[error, []entities.ProjectTask] { func (ds *PrismaProjectTaskDataSource) FindByProjectID(ctx context.Context, projectID string) E.Either[error, []entities.ProjectTask] {
tasks, err := ds.client.ProjectTaskDbo.FindMany( return F.Flow3(
E.Eitherize1(ds.client.ProjectTaskDbo.FindMany(
db.ProjectTaskDbo.ProjectID.Equals(projectID), db.ProjectTaskDbo.ProjectID.Equals(projectID),
).Exec(ctx) ).Exec),
if err != nil { E.MapLeft[[]db.ProjectTaskDboModel](WrapDBError(fmt.Sprintf("Could not retrieve project tasks for project with ID %s", projectID))),
return E.Left[[]entities.ProjectTask](handleDBError(err, fmt.Sprintf("Could not retrieve project tasks for project with ID %s", projectID))) E.Map[error](A.Map(mapPrismaProjectTaskToDomain)),
} )(ctx)
return F.Pipe2(
tasks,
A.Map(mapPrismaProjectTaskToDomain),
E.Right[error],
)
} }

View File

@ -1,7 +1,6 @@
package data package data
import ( import (
"actatempus_backend/internal/domain/app_error"
"actatempus_backend/internal/domain/entities" "actatempus_backend/internal/domain/entities"
"actatempus_backend/internal/infrastructure/data/db" "actatempus_backend/internal/infrastructure/data/db"
"context" "context"
@ -10,7 +9,6 @@ import (
A "github.com/IBM/fp-go/array" A "github.com/IBM/fp-go/array"
E "github.com/IBM/fp-go/either" E "github.com/IBM/fp-go/either"
F "github.com/IBM/fp-go/function" F "github.com/IBM/fp-go/function"
O "github.com/IBM/fp-go/option"
) )
type PrismaTimeEntryDataSource struct { type PrismaTimeEntryDataSource struct {
@ -23,7 +21,8 @@ func NewPrismaTimeEntryDataSource(client *db.PrismaClient) *PrismaTimeEntryDataS
// Create a new TimeEntry // Create a new TimeEntry
func (ds *PrismaTimeEntryDataSource) Create(ctx context.Context, entry entities.TimeEntryCreate) E.Either[error, entities.TimeEntry] { func (ds *PrismaTimeEntryDataSource) Create(ctx context.Context, entry entities.TimeEntryCreate) E.Either[error, entities.TimeEntry] {
createdEntry, err := ds.client.TimeEntryDbo.CreateOne( return F.Flow4(
E.Eitherize1(ds.client.TimeEntryDbo.CreateOne(
db.TimeEntryDbo.StartTime.Set(entry.StartTime), db.TimeEntryDbo.StartTime.Set(entry.StartTime),
db.TimeEntryDbo.User.Link( db.TimeEntryDbo.User.Link(
db.UserDbo.ID.Equals(entry.UserID), db.UserDbo.ID.Equals(entry.UserID),
@ -32,41 +31,33 @@ func (ds *PrismaTimeEntryDataSource) Create(ctx context.Context, entry entities.
), ),
db.TimeEntryDbo.EndTime.SetIfPresent(entry.EndTime), db.TimeEntryDbo.EndTime.SetIfPresent(entry.EndTime),
db.TimeEntryDbo.Description.SetIfPresent(entry.Description), db.TimeEntryDbo.Description.SetIfPresent(entry.Description),
).Exec(ctx) ).Exec),
E.MapLeft[*db.TimeEntryDboModel](WrapDBError("Could not create time entry")),
if err != nil { E.Chain(
return E.Left[entities.TimeEntry, error](app_error.NewInternalError(err)) TryDereference[db.TimeEntryDboModel],
} ),
return F.Pipe3(
O.FromNillable(createdEntry),
Dereference,
FromOptionWithError[db.TimeEntryDboModel](app_error.NewInternalError(fmt.Errorf("Could not create time entry"))),
E.Map[error](mapPrismaTimeEntryToDomain), E.Map[error](mapPrismaTimeEntryToDomain),
) )(ctx)
} }
// Find TimeEntry by ID // Find TimeEntry by ID
func (ds *PrismaTimeEntryDataSource) FindByID(ctx context.Context, id string) E.Either[error, entities.TimeEntry] { func (ds *PrismaTimeEntryDataSource) FindByID(ctx context.Context, id string) E.Either[error, entities.TimeEntry] {
entry, err := ds.client.TimeEntryDbo.FindUnique( return F.Flow4(
E.Eitherize1(ds.client.TimeEntryDbo.FindUnique(
db.TimeEntryDbo.ID.Equals(id), db.TimeEntryDbo.ID.Equals(id),
).Exec(ctx) ).Exec),
E.MapLeft[*db.TimeEntryDboModel](WrapDBError(fmt.Sprintf("Could not retrieve time entry with ID %s", id))),
if err != nil { E.Chain(
return E.Left[entities.TimeEntry](handleDBError(err, fmt.Sprintf("TimeEntry with ID %s not found", id))) TryDereference[db.TimeEntryDboModel],
} ),
return F.Pipe3(
O.FromNillable(entry),
Dereference,
FromOptionWithError[db.TimeEntryDboModel](app_error.NewNotFoundError(fmt.Sprintf("TimeEntry with ID %s not found", id))),
E.Map[error](mapPrismaTimeEntryToDomain), E.Map[error](mapPrismaTimeEntryToDomain),
) )(ctx)
} }
// Update an existing TimeEntry // Update an existing TimeEntry
func (ds *PrismaTimeEntryDataSource) Update(ctx context.Context, entry entities.TimeEntryUpdate) E.Either[error, entities.TimeEntry] { func (ds *PrismaTimeEntryDataSource) Update(ctx context.Context, entry entities.TimeEntryUpdate) E.Either[error, entities.TimeEntry] {
updatedEntry, err := ds.client.TimeEntryDbo.FindUnique( return F.Flow4(
E.Eitherize1(ds.client.TimeEntryDbo.FindUnique(
db.TimeEntryDbo.ID.Equals(entry.ID), db.TimeEntryDbo.ID.Equals(entry.ID),
).Update( ).Update(
db.TimeEntryDbo.StartTime.SetIfPresent(entry.StartTime), db.TimeEntryDbo.StartTime.SetIfPresent(entry.StartTime),
@ -78,80 +69,56 @@ func (ds *PrismaTimeEntryDataSource) Update(ctx context.Context, entry entities.
db.TimeEntryDbo.Project.Link( db.TimeEntryDbo.Project.Link(
db.ProjectDbo.ID.EqualsIfPresent(entry.ProjectID), db.ProjectDbo.ID.EqualsIfPresent(entry.ProjectID),
), ),
).Exec(ctx) ).Exec),
E.MapLeft[*db.TimeEntryDboModel](WrapDBError(fmt.Sprintf("Could not update time entry with ID %s", entry.ID))),
if err != nil { E.Chain(
return E.Left[entities.TimeEntry, error](handleDBError(err, fmt.Sprintf("Could not update time entry with ID %s", entry.ID))) TryDereference[db.TimeEntryDboModel],
} ),
return F.Pipe3(
O.FromNillable(updatedEntry),
Dereference,
FromOptionWithError[db.TimeEntryDboModel](app_error.NewNotFoundError(fmt.Sprintf("TimeEntry with ID %s not found", entry.ID))),
E.Map[error](mapPrismaTimeEntryToDomain), E.Map[error](mapPrismaTimeEntryToDomain),
) )(ctx)
} }
// Delete a TimeEntry // Delete a TimeEntry
func (ds *PrismaTimeEntryDataSource) Delete(ctx context.Context, id string) E.Either[error, entities.TimeEntry] { func (ds *PrismaTimeEntryDataSource) Delete(ctx context.Context, id string) E.Either[error, entities.TimeEntry] {
deletedEntry, err := ds.client.TimeEntryDbo.FindUnique( return F.Flow4(
E.Eitherize1(ds.client.TimeEntryDbo.FindUnique(
db.TimeEntryDbo.ID.Equals(id), db.TimeEntryDbo.ID.Equals(id),
).Delete().Exec(ctx) ).Delete().Exec),
E.MapLeft[*db.TimeEntryDboModel](WrapDBError(fmt.Sprintf("Could not delete time entry with ID %s", id))),
if err != nil { E.Chain(
return E.Left[entities.TimeEntry](handleDBError(err, fmt.Sprintf("Could not delete time entry with ID %s", id))) TryDereference[db.TimeEntryDboModel],
} ),
return F.Pipe3(
O.FromNillable(deletedEntry),
Dereference,
FromOptionWithError[db.TimeEntryDboModel](app_error.NewNotFoundError(fmt.Sprintf("TimeEntry with ID %s not found", id))),
E.Map[error](mapPrismaTimeEntryToDomain), E.Map[error](mapPrismaTimeEntryToDomain),
) )(ctx)
} }
// FindAll retrieves all TimeEntries // FindAll retrieves all TimeEntries
func (ds *PrismaTimeEntryDataSource) FindAll(ctx context.Context) E.Either[error, []entities.TimeEntry] { func (ds *PrismaTimeEntryDataSource) FindAll(ctx context.Context) E.Either[error, []entities.TimeEntry] {
entries, err := ds.client.TimeEntryDbo.FindMany().Exec(ctx) return F.Flow3(
if err != nil { E.Eitherize1(ds.client.TimeEntryDbo.FindMany().Exec),
return E.Left[[]entities.TimeEntry](handleDBError(err, "Could not retrieve time entries")) E.MapLeft[[]db.TimeEntryDboModel](WrapDBError("Could not retrieve time entries")),
} E.Map[error](A.Map(mapPrismaTimeEntryToDomain)),
)(ctx)
return F.Pipe2(
entries,
A.Map(mapPrismaTimeEntryToDomain),
E.Right[error],
)
} }
// FindByUserID retrieves all TimeEntries by UserID // FindByUserID retrieves all TimeEntries by UserID
func (ds *PrismaTimeEntryDataSource) FindByUserID(ctx context.Context, userID string) E.Either[error, []entities.TimeEntry] { func (ds *PrismaTimeEntryDataSource) FindByUserID(ctx context.Context, userID string) E.Either[error, []entities.TimeEntry] {
entries, err := ds.client.TimeEntryDbo.FindMany( return F.Flow3(
E.Eitherize1(ds.client.TimeEntryDbo.FindMany(
db.TimeEntryDbo.UserID.Equals(userID), db.TimeEntryDbo.UserID.Equals(userID),
).Exec(ctx) ).Exec),
if err != nil { E.MapLeft[[]db.TimeEntryDboModel](WrapDBError(fmt.Sprintf("Could not retrieve time entries for user with ID %s", userID))),
return E.Left[[]entities.TimeEntry](handleDBError(err, fmt.Sprintf("Could not retrieve time entries for user with ID %s", userID))) E.Map[error](A.Map(mapPrismaTimeEntryToDomain)),
} )(ctx)
return F.Pipe2(
entries,
A.Map(mapPrismaTimeEntryToDomain),
E.Right[error],
)
} }
// FindByProjectID retrieves all TimeEntries by ProjectID // FindByProjectID retrieves all TimeEntries by ProjectID
func (ds *PrismaTimeEntryDataSource) FindByProjectID(ctx context.Context, projectID string) E.Either[error, []entities.TimeEntry] { func (ds *PrismaTimeEntryDataSource) FindByProjectID(ctx context.Context, projectID string) E.Either[error, []entities.TimeEntry] {
entries, err := ds.client.TimeEntryDbo.FindMany( return F.Flow3(
E.Eitherize1(ds.client.TimeEntryDbo.FindMany(
db.TimeEntryDbo.ProjectID.Equals(projectID), db.TimeEntryDbo.ProjectID.Equals(projectID),
).Exec(ctx) ).Exec),
if err != nil { E.MapLeft[[]db.TimeEntryDboModel](WrapDBError(fmt.Sprintf("Could not retrieve time entries for project with ID %s", projectID))),
return E.Left[[]entities.TimeEntry](handleDBError(err, fmt.Sprintf("Could not retrieve time entries for project with ID %s", projectID))) E.Map[error](A.Map(mapPrismaTimeEntryToDomain)),
} )(ctx)
return F.Pipe2(
entries,
A.Map(mapPrismaTimeEntryToDomain),
E.Right[error],
)
} }

View File

@ -1,7 +1,6 @@
package data package data
import ( import (
"actatempus_backend/internal/domain/app_error"
"actatempus_backend/internal/domain/entities" "actatempus_backend/internal/domain/entities"
"actatempus_backend/internal/infrastructure/data/db" "actatempus_backend/internal/infrastructure/data/db"
"actatempus_backend/internal/utils" "actatempus_backend/internal/utils"
@ -11,7 +10,6 @@ import (
A "github.com/IBM/fp-go/array" A "github.com/IBM/fp-go/array"
E "github.com/IBM/fp-go/either" E "github.com/IBM/fp-go/either"
F "github.com/IBM/fp-go/function" F "github.com/IBM/fp-go/function"
O "github.com/IBM/fp-go/option"
) )
type PrismaUserDataSource struct { type PrismaUserDataSource struct {
@ -23,107 +21,81 @@ func NewPrismaUserDataSource(client *db.PrismaClient) *PrismaUserDataSource {
} }
func (ds *PrismaUserDataSource) Create(ctx context.Context, user entities.UserCreate) E.Either[error, entities.User] { func (ds *PrismaUserDataSource) Create(ctx context.Context, user entities.UserCreate) E.Either[error, entities.User] {
createdUser, err := ds.client.UserDbo.CreateOne( return F.Flow4(
E.Eitherize1(ds.client.UserDbo.CreateOne(
db.UserDbo.Name.Set(user.Name), db.UserDbo.Name.Set(user.Name),
db.UserDbo.Email.Set(user.Email), db.UserDbo.Email.Set(user.Email),
db.UserDbo.Password.Set(GenerateSecureHash(user.Password)), db.UserDbo.Password.Set(GenerateSecureHash(user.Password)),
).Exec(ctx) ).Exec),
E.MapLeft[*db.UserDboModel](WrapDBError("Could not create user")),
if err != nil { E.Chain(
return E.Left[entities.User, error](app_error.NewInternalError(err)) TryDereference[db.UserDboModel],
} ),
return F.Pipe3(
O.FromNillable(createdUser),
Dereference,
FromOptionWithError[db.UserDboModel](app_error.NewInternalError(fmt.Errorf("Could not create user"))),
E.Map[error](mapPrismaUserToDomain), E.Map[error](mapPrismaUserToDomain),
) )(ctx)
} }
func (ds *PrismaUserDataSource) FindByID(ctx context.Context, id string) E.Either[error, entities.User] { func (ds *PrismaUserDataSource) FindByID(ctx context.Context, id string) E.Either[error, entities.User] {
user, err := ds.client.UserDbo.FindUnique( return F.Flow4(
E.Eitherize1(ds.client.UserDbo.FindUnique(
db.UserDbo.ID.Equals(id), db.UserDbo.ID.Equals(id),
).Exec(ctx) ).Exec),
E.MapLeft[*db.UserDboModel](WrapDBError(fmt.Sprintf("Could not retrieve user with ID %s", id))),
if err != nil { E.Chain(
return E.Left[entities.User, error](handleDBError(err, fmt.Sprintf("Query for user with ID %s failed", id))) TryDereference[db.UserDboModel],
} ),
return F.Pipe3(
O.FromNillable(user),
Dereference,
FromOptionWithError[db.UserDboModel](app_error.NewNotFoundError(fmt.Sprintf("User with ID %s not found", user.ID))),
E.Map[error](mapPrismaUserToDomain), E.Map[error](mapPrismaUserToDomain),
) )(ctx)
} }
func (ds *PrismaUserDataSource) FindByEmail(ctx context.Context, email string) E.Either[error, entities.User] { func (ds *PrismaUserDataSource) FindByEmail(ctx context.Context, email string) E.Either[error, entities.User] {
user, err := ds.client.UserDbo.FindUnique( return F.Flow4(
E.Eitherize1(ds.client.UserDbo.FindUnique(
db.UserDbo.Email.Equals(email), db.UserDbo.Email.Equals(email),
).Exec(ctx) ).Exec),
E.MapLeft[*db.UserDboModel](WrapDBError(fmt.Sprintf("Could not retrieve user with email %s", email))),
if err != nil { E.Chain(
return E.Left[entities.User, error](handleDBError(err, fmt.Sprintf("Query for user with email %s failed", email))) TryDereference[db.UserDboModel],
),
}
return F.Pipe3(
O.FromNillable(user),
Dereference,
FromOptionWithError[db.UserDboModel](app_error.NewNotFoundError(fmt.Sprintf("User with email %s not found", email))),
E.Map[error](mapPrismaUserToDomain), E.Map[error](mapPrismaUserToDomain),
) )(ctx)
} }
func (ds *PrismaUserDataSource) Update(ctx context.Context, user entities.UserUpdate) E.Either[error, entities.User] { func (ds *PrismaUserDataSource) Update(ctx context.Context, user entities.UserUpdate) E.Either[error, entities.User] {
updatedUser, err := ds.client.UserDbo.FindUnique( return F.Flow4(
E.Eitherize1(ds.client.UserDbo.FindUnique(
db.UserDbo.ID.Equals(user.ID), db.UserDbo.ID.Equals(user.ID),
).Update( ).Update(
db.UserDbo.Name.SetIfPresent(user.Name), db.UserDbo.Name.SetIfPresent(user.Name),
db.UserDbo.Email.SetIfPresent(user.Email), db.UserDbo.Email.SetIfPresent(user.Email),
db.UserDbo.Password.SetIfPresent(utils.Let(user.Password, GenerateSecureHash)), db.UserDbo.Password.SetIfPresent(utils.Let(user.Password, GenerateSecureHash)),
).Exec(ctx) ).Exec),
E.MapLeft[*db.UserDboModel](WrapDBError(fmt.Sprintf("Could not update user with ID %s", user.ID))),
if err != nil { E.Chain(
return E.Left[entities.User, error](handleDBError(err, fmt.Sprintf("Could not update user with ID %s", user.ID))) TryDereference[db.UserDboModel],
} ),
return F.Pipe3(
O.FromNillable(updatedUser),
Dereference,
FromOptionWithError[db.UserDboModel](app_error.NewNotFoundError(fmt.Sprintf("User with ID %s not found", user.ID))),
E.Map[error](mapPrismaUserToDomain), E.Map[error](mapPrismaUserToDomain),
) )(ctx)
} }
func (ds *PrismaUserDataSource) Delete(ctx context.Context, id string) E.Either[error, entities.User] { func (ds *PrismaUserDataSource) Delete(ctx context.Context, id string) E.Either[error, entities.User] {
deleted, err := ds.client.UserDbo.FindUnique( return F.Flow4(
E.Eitherize1(ds.client.UserDbo.FindUnique(
db.UserDbo.ID.Equals(id), db.UserDbo.ID.Equals(id),
).Delete().Exec(ctx) ).Delete().Exec),
E.MapLeft[*db.UserDboModel](WrapDBError(fmt.Sprintf("Could not delete user with ID %s", id))),
if err != nil { E.Chain(
return E.Left[entities.User, error](handleDBError(err, fmt.Sprintf("Could not delete user with ID %s", id))) TryDereference[db.UserDboModel],
} ),
return F.Pipe3(
O.FromNillable(deleted),
Dereference,
FromOptionWithError[db.UserDboModel](app_error.NewNotFoundError(fmt.Sprintf("User with ID %s not found", id))),
E.Map[error](mapPrismaUserToDomain), E.Map[error](mapPrismaUserToDomain),
) )(ctx)
} }
func (ds *PrismaUserDataSource) FindAll(ctx context.Context) E.Either[error, []entities.User] { func (ds *PrismaUserDataSource) FindAll(ctx context.Context) E.Either[error, []entities.User] {
users, err := ds.client.UserDbo.FindMany().Exec(ctx) return F.Flow3(
if err != nil { E.Eitherize1(ds.client.UserDbo.FindMany().Exec),
return E.Left[[]entities.User, error](handleDBError(err, "Could not retrieve users")) E.MapLeft[[]db.UserDboModel](WrapDBError("Could not retrieve users")),
} E.Map[error](A.Map(mapPrismaUserToDomain)),
)(ctx)
return F.Pipe2(
users,
A.Map(mapPrismaUserToDomain),
E.Right[error],
)
} }