implemented data sources with prisma in go
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package app_error
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// AppError is the standard error type used throughout the application.
|
||||
type AppError struct {
|
||||
Code ErrorCode `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Status int `json:"status"` // HTTP status code
|
||||
Err error `json:"-"` // Original error, if any
|
||||
}
|
||||
|
||||
// Error satisfies the error interface.
|
||||
func (e *AppError) Error() string {
|
||||
if e.Err != nil {
|
||||
return fmt.Sprintf("Code: %s, Message: %s, Original Error: %s", e.Code, e.Message, e.Err.Error())
|
||||
}
|
||||
return fmt.Sprintf("Code: %s, Message: %s", e.Code, e.Message)
|
||||
}
|
||||
|
||||
// Wrap wraps an existing error into an AppError.
|
||||
func Wrap(err error, code ErrorCode, message string, status int) *AppError {
|
||||
return &AppError{
|
||||
Code: code,
|
||||
Message: message,
|
||||
Status: status,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
|
||||
// New creates a new AppError.
|
||||
func New(code ErrorCode, message string, status int) *AppError {
|
||||
return &AppError{
|
||||
Code: code,
|
||||
Message: message,
|
||||
Status: status,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package app_error
|
||||
|
||||
// ErrorCode is a custom type for application error codes.
|
||||
type ErrorCode string
|
||||
|
||||
const (
|
||||
// General errors
|
||||
InternalError ErrorCode = "internal_error"
|
||||
ValidationError ErrorCode = "validation_error"
|
||||
NotFoundError ErrorCode = "not_found"
|
||||
UnauthorizedError ErrorCode = "unauthorized"
|
||||
ForbiddenError ErrorCode = "forbidden"
|
||||
ConflictError ErrorCode = "conflict"
|
||||
DatabaseError ErrorCode = "database_error"
|
||||
ExternalServiceError ErrorCode = "external_service_error"
|
||||
|
||||
// Specific errors
|
||||
UserNotFoundError ErrorCode = "user_not_found"
|
||||
ProjectNotFoundError ErrorCode = "project_not_found"
|
||||
)
|
||||
@@ -0,0 +1,25 @@
|
||||
package app_error
|
||||
|
||||
import "net/http"
|
||||
|
||||
// Helpers for creating common errors
|
||||
|
||||
// NewValidationError creates a validation error.
|
||||
func NewValidationError(message string) *AppError {
|
||||
return New(ValidationError, message, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
// NewNotFoundError creates a not found error.
|
||||
func NewNotFoundError(message string) *AppError {
|
||||
return New(NotFoundError, message, http.StatusNotFound)
|
||||
}
|
||||
|
||||
// NewUnauthorizedError creates an unauthorized error.
|
||||
func NewUnauthorizedError(message string) *AppError {
|
||||
return New(UnauthorizedError, message, http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
// NewInternalError creates an internal server error.
|
||||
func NewInternalError(err error) *AppError {
|
||||
return Wrap(err, InternalError, "An internal server error occurred", http.StatusInternalServerError)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"actatempus_backend/internal/domain/entities"
|
||||
"context"
|
||||
|
||||
E "github.com/IBM/fp-go/either"
|
||||
)
|
||||
|
||||
// ProjectDataSource defines the operations for interacting with project data.
|
||||
type ProjectDataSource interface {
|
||||
Create(ctx context.Context, project entities.ProjectCreate) E.Either[error,entities.Project]
|
||||
FindByID(ctx context.Context, id string) E.Either[error,entities.Project]
|
||||
FindByUserID(ctx context.Context, userID string) E.Either[error,[]entities.Project]
|
||||
Update(ctx context.Context, project entities.ProjectUpdate) E.Either[error,entities.Project]
|
||||
Delete(ctx context.Context, id string) E.Either[error,entities.Project]
|
||||
FindAll(ctx context.Context) E.Either[error,[]entities.Project]
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"actatempus_backend/internal/domain/entities"
|
||||
"context"
|
||||
|
||||
E "github.com/IBM/fp-go/either"
|
||||
)
|
||||
|
||||
// ProjectTaskDataSource defines the operations for interacting with project task data.
|
||||
type ProjectTaskDataSource interface {
|
||||
Create(ctx context.Context, task entities.ProjectTaskCreate) E.Either[error,entities.ProjectTask]
|
||||
FindByID(ctx context.Context, id string) E.Either[error,entities.ProjectTask]
|
||||
FindByProjectID(ctx context.Context, projectID string) E.Either[error,[]entities.ProjectTask]
|
||||
Update(ctx context.Context, task entities.ProjectTaskUpdate) E.Either[error,entities.ProjectTask]
|
||||
Delete(ctx context.Context, id string) E.Either[error,entities.ProjectTask]
|
||||
FindAll(ctx context.Context) E.Either[error,[]entities.ProjectTask]
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"actatempus_backend/internal/domain/entities"
|
||||
"context"
|
||||
|
||||
E "github.com/IBM/fp-go/either"
|
||||
)
|
||||
|
||||
// TimeEntryDataSource defines the operations for interacting with time entry data.
|
||||
type TimeEntryDataSource interface {
|
||||
Create(ctx context.Context, entry entities.TimeEntryCreate) E.Either[error,entities.TimeEntry]
|
||||
FindByID(ctx context.Context, id string) E.Either[error,entities.TimeEntry]
|
||||
FindByUserID(ctx context.Context, userID string) E.Either[error,[]entities.TimeEntry]
|
||||
FindByProjectID(ctx context.Context, projectID string) E.Either[error,[]entities.TimeEntry]
|
||||
Update(ctx context.Context, entry entities.TimeEntryUpdate) E.Either[error,entities.TimeEntry]
|
||||
Delete(ctx context.Context, id string) E.Either[error,entities.TimeEntry]
|
||||
FindAll(ctx context.Context) E.Either[error,[]entities.TimeEntry]
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"actatempus_backend/internal/domain/entities"
|
||||
"context"
|
||||
E "github.com/IBM/fp-go/either"
|
||||
)
|
||||
|
||||
// UserDataSource defines the operations for interacting with user data.
|
||||
type UserDataSource interface {
|
||||
Create(ctx context.Context, user entities.UserCreate) E.Either[error,entities.User]
|
||||
FindByID(ctx context.Context, id string) E.Either[error,entities.User]
|
||||
FindByEmail(ctx context.Context, email string) E.Either[error,entities.User]
|
||||
Update(ctx context.Context, user entities.UserUpdate) E.Either[error,entities.User]
|
||||
Delete(ctx context.Context, id string) E.Either[error,entities.User]
|
||||
FindAll(ctx context.Context) E.Either[error,[]entities.User]
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package entities
|
||||
|
||||
import "time"
|
||||
|
||||
// Project Domain
|
||||
type Project struct {
|
||||
ID string
|
||||
Name string
|
||||
Description *string
|
||||
ClientID *string
|
||||
UserID string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// ProjectCreate
|
||||
type ProjectCreate struct {
|
||||
Name string
|
||||
Description *string
|
||||
ClientID *string
|
||||
UserID string
|
||||
}
|
||||
|
||||
// ProjectUpdate
|
||||
type ProjectUpdate struct {
|
||||
ID string
|
||||
Name *string
|
||||
Description *string
|
||||
ClientID *string
|
||||
UserID *string
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package entities
|
||||
|
||||
import "time"
|
||||
|
||||
// ProjectTask Domain
|
||||
type ProjectTask struct {
|
||||
ID string
|
||||
Name string
|
||||
Description *string
|
||||
ProjectID string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// ProjectTaskCreate
|
||||
type ProjectTaskCreate struct {
|
||||
Name string
|
||||
Description *string
|
||||
ProjectID string
|
||||
}
|
||||
|
||||
// ProjectTaskUpdate
|
||||
type ProjectTaskUpdate struct {
|
||||
ID string
|
||||
Name *string
|
||||
Description *string
|
||||
ProjectID *string
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package entities
|
||||
|
||||
import "time"
|
||||
|
||||
// TimeEntry Domain
|
||||
type TimeEntry struct {
|
||||
ID string
|
||||
StartTime time.Time
|
||||
EndTime *time.Time
|
||||
Description *string
|
||||
UserID string
|
||||
ProjectID string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// TimeEntryCreate
|
||||
type TimeEntryCreate struct {
|
||||
StartTime time.Time
|
||||
EndTime *time.Time
|
||||
Description *string
|
||||
UserID string
|
||||
ProjectID string
|
||||
}
|
||||
|
||||
// TimeEntryUpdate
|
||||
type TimeEntryUpdate struct {
|
||||
ID string
|
||||
StartTime *time.Time
|
||||
EndTime *time.Time
|
||||
Description *string
|
||||
UserID *string
|
||||
ProjectID *string
|
||||
}
|
||||
@@ -1,8 +1,30 @@
|
||||
package entities
|
||||
|
||||
import "time"
|
||||
|
||||
// In user.go
|
||||
|
||||
// User Domain
|
||||
type User struct {
|
||||
ID string
|
||||
ID string
|
||||
Name string
|
||||
Email string
|
||||
Password string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// UserCreate DTO
|
||||
type UserCreate struct {
|
||||
Name string
|
||||
Email string
|
||||
Password string
|
||||
}
|
||||
|
||||
// UserUpdate DTO
|
||||
type UserUpdate struct {
|
||||
ID string
|
||||
Name *string
|
||||
Email *string
|
||||
Password *string
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package repositories
|
||||
|
||||
import "actatempus_backend/internal/domain/entities"
|
||||
|
||||
type UserRepository interface {
|
||||
Create(user *entities.User) error
|
||||
FindByEmail(email string) (*entities.User, error)
|
||||
FindByID(id string) (*entities.User, error)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"actatempus_backend/internal/domain/entities"
|
||||
"context"
|
||||
)
|
||||
|
||||
// ProjectRepository defines the operations for interacting with project data.
|
||||
type ProjectRepository interface {
|
||||
Create(ctx context.Context, project entities.Project) (entities.Project, error)
|
||||
FindByID(ctx context.Context, id string) (entities.Project, error)
|
||||
FindByUserID(ctx context.Context, userID string) ([]entities.Project, error)
|
||||
Update(ctx context.Context, project entities.Project) (entities.Project, error)
|
||||
Delete(ctx context.Context, id string) error
|
||||
FindAll(ctx context.Context) ([]entities.Project, error)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"actatempus_backend/internal/domain/entities"
|
||||
"context"
|
||||
)
|
||||
|
||||
// ProjectTaskRepository defines the operations for interacting with project task data.
|
||||
type ProjectTaskRepository interface {
|
||||
Create(ctx context.Context, task entities.ProjectTask) (entities.ProjectTask, error)
|
||||
FindByID(ctx context.Context, id string) (entities.ProjectTask, error)
|
||||
FindByProjectID(ctx context.Context, projectID string) ([]entities.ProjectTask, error)
|
||||
Update(ctx context.Context, task entities.ProjectTask) (entities.ProjectTask, error)
|
||||
Delete(ctx context.Context, id string) error
|
||||
FindAll(ctx context.Context) ([]entities.ProjectTask, error)
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"actatempus_backend/internal/domain/entities"
|
||||
"context"
|
||||
)
|
||||
|
||||
// TimeEntryRepository defines the operations for interacting with time entry data.
|
||||
type TimeEntryRepository interface {
|
||||
Create(ctx context.Context, entry entities.TimeEntry) (entities.TimeEntry, error)
|
||||
FindByID(ctx context.Context, id string) (entities.TimeEntry, error)
|
||||
FindByUserID(ctx context.Context, userID string) ([]entities.TimeEntry, error)
|
||||
FindByProjectID(ctx context.Context, projectID string) ([]entities.TimeEntry, error)
|
||||
Update(ctx context.Context, entry entities.TimeEntry) (entities.TimeEntry, error)
|
||||
Delete(ctx context.Context, id string) error
|
||||
FindAll(ctx context.Context) ([]entities.TimeEntry, error)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"actatempus_backend/internal/domain/entities"
|
||||
"context"
|
||||
)
|
||||
|
||||
// UserRepository defines the operations for interacting with user data.
|
||||
type UserRepository interface {
|
||||
Create(ctx context.Context, user entities.UserCreate) (entities.User, error)
|
||||
FindByID(ctx context.Context, id string) (entities.User, error)
|
||||
FindByEmail(ctx context.Context, email string) (entities.User, error)
|
||||
Update(ctx context.Context, user entities.UserUpdate) (entities.User, error)
|
||||
Delete(ctx context.Context, id string) error
|
||||
FindAll(ctx context.Context) ([]entities.User, error)
|
||||
}
|
||||
Reference in New Issue
Block a user