Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions repository/crud_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (
)

type CrudRepository[T any] interface {
Create(et T) (*T, error)
Create(et *T) (*T, error)
FindByID(id uuid.UUID) (*T, error)
Update(et T) (*T, error)
Update(et *T) (*T, error)
Delete(id uuid.UUID) error
ListAll() ([]T, error)
First(et T) (*T, error)
FindWithEntity(et T) ([]T, error)
First(et *T) (*T, error)
FindWithEntity(et *T) ([]T, error)
FindWithConditions(conditions map[string]any) ([]T, error)
FindWithOrConditions(conditions map[string]any) ([]T, error)
CountWithConditions(conditions map[string]any) (int64, error)
CountWithEntity(et T) (int64, error)
CountWithEntity(et *T) (int64, error)
}

func NewCrudRepository[T any](db *gorm.DB) CrudRepository[T] {
Expand All @@ -30,13 +30,13 @@ type crudRepository[T any] struct {
db *gorm.DB
}

func (r *crudRepository[T]) Create(et T) (*T, error) {
func (r *crudRepository[T]) Create(et *T) (*T, error) {
v := validator.New()
if err := v.Struct(et); err != nil {
return nil, err
}
err := r.db.Create(&et).Error
return &et, err
err := r.db.Create(et).Error
return et, err
}

func (r *crudRepository[T]) FindByID(id uuid.UUID) (*T, error) {
Expand All @@ -45,13 +45,13 @@ func (r *crudRepository[T]) FindByID(id uuid.UUID) (*T, error) {
return &et, err
}

func (r *crudRepository[T]) Update(et T) (*T, error) {
func (r *crudRepository[T]) Update(et *T) (*T, error) {
v := validator.New()
if err := v.Struct(et); err != nil {
return nil, err
}
err := r.db.Save(&et).Error
return &et, err
err := r.db.Save(et).Error
return et, err
}

func (r *crudRepository[T]) Delete(id uuid.UUID) error {
Expand All @@ -65,13 +65,13 @@ func (r *crudRepository[T]) ListAll() ([]T, error) {
return entities, err
}

func (r *crudRepository[T]) First(et T) (*T, error) {
func (r *crudRepository[T]) First(et *T) (*T, error) {
var existed T
err := r.db.Where(et).First(&existed).Error
return &existed, err
}

func (r *crudRepository[T]) FindWithEntity(et T) ([]T, error) {
func (r *crudRepository[T]) FindWithEntity(et *T) ([]T, error) {
var entities []T
err := r.db.Where(et).Find(&entities).Error
return entities, err
Expand All @@ -82,6 +82,7 @@ func (r *crudRepository[T]) FindWithConditions(conditions map[string]any) ([]T,
err := r.db.Where(conditions).Find(&entities).Error
return entities, err
}

func (r *crudRepository[T]) FindWithOrConditions(conditions map[string]any) ([]T, error) {
var entities []T
query := r.db.Model(new(T))
Expand All @@ -98,7 +99,7 @@ func (r *crudRepository[T]) CountWithConditions(conditions map[string]any) (int6
return count, err
}

func (r *crudRepository[T]) CountWithEntity(et T) (int64, error) {
func (r *crudRepository[T]) CountWithEntity(et *T) (int64, error) {
var count int64
err := r.db.Model(new(T)).Where(et).Count(&count).Error
return count, err
Expand Down