71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
package internal_reference
|
|
|
|
import (
|
|
"errors"
|
|
ir "simrs-vx/internal/domain/main-entities/internal-reference"
|
|
plh "simrs-vx/pkg/lib-helper"
|
|
pl "simrs-vx/pkg/logger"
|
|
|
|
dg "github.com/karincake/apem/db-gorm-pg"
|
|
gh "github.com/karincake/getuk"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func CreateData(input ir.CreateDto, event *pl.Event, dbx ...*gorm.DB) (*ir.InternalReference, error) {
|
|
pl.SetLogInfo(event, nil, "started", "DBCreate")
|
|
|
|
data := ir.InternalReference{}
|
|
setData(&input, &data)
|
|
|
|
var tx *gorm.DB
|
|
if len(dbx) > 0 {
|
|
tx = dbx[0]
|
|
} else {
|
|
tx = dg.I
|
|
}
|
|
|
|
if err := tx.Create(&data).Error; err != nil {
|
|
return nil, plh.HandleCreateError(input, event, err)
|
|
}
|
|
|
|
pl.SetLogInfo(event, nil, "complete")
|
|
return &data, nil
|
|
}
|
|
|
|
func ReadListData(input ir.ReadListDto, event *pl.Event, dbx ...*gorm.DB) ([]ir.InternalReference, *ir.MetaDto, error) {
|
|
pl.SetLogInfo(event, input, "started", "DBReadList")
|
|
data := []ir.InternalReference{}
|
|
pagination := gh.Pagination{}
|
|
count := int64(0)
|
|
meta := ir.MetaDto{}
|
|
|
|
var tx *gorm.DB
|
|
if len(dbx) > 0 {
|
|
tx = dbx[0]
|
|
} else {
|
|
tx = dg.I
|
|
}
|
|
|
|
tx = tx.
|
|
Model(&ir.InternalReference{}).
|
|
Scopes(gh.Preload(input.Includes)).
|
|
Scopes(gh.Filter(input.FilterDto)).
|
|
Count(&count).
|
|
Scopes(gh.Paginate(input, &pagination)).
|
|
Order("\"CreatedAt\" DESC")
|
|
|
|
if err := tx.Find(&data).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, &meta, nil
|
|
}
|
|
return nil, nil, plh.HandleListError(input, event, err)
|
|
}
|
|
|
|
meta.Count = int(count)
|
|
meta.PageNumber = pagination.PageNumber
|
|
meta.PageSize = pagination.PageSize
|
|
|
|
pl.SetLogInfo(event, nil, "complete")
|
|
return data, &meta, nil
|
|
}
|