package ds import ( "context" "github.com/oklog/ulid/v2" "github.com/timetracker/backend/internal/domain/entities" "github.com/timetracker/backend/internal/domain/persistence" "github.com/timetracker/backend/internal/infrastructure/persistence/db/dbo" "gorm.io/gorm" ) type CustomerDatasource struct { db *gorm.DB } func NewCustomerDatasource(db *gorm.DB) persistence.CustomerDatasource { return &CustomerDatasource{db: db} } func (r *CustomerDatasource) Get(ctx context.Context, id ulid.ULID) (*entities.Customer, error) { var customerDBO dbo.CustomerDBO if err := r.db.WithContext(ctx).First(&customerDBO, "id = ?", id).Error; err != nil { return nil, err } customer := &entities.Customer{ EntityBase: entities.EntityBase{ ID: customerDBO.ID, CreatedAt: customerDBO.CreatedAt, UpdatedAt: customerDBO.UpdatedAt, }, Name: customerDBO.Name, CompanyID: customerDBO.CompanyID, } return customer, nil } func (r *CustomerDatasource) Create(ctx context.Context, customer *entities.Customer) error { customerDBO := dbo.CustomerDBO{ ID: customer.ID, CreatedAt: customer.CreatedAt, UpdatedAt: customer.UpdatedAt, Name: customer.Name, CompanyID: customer.CompanyID, } return r.db.WithContext(ctx).Create(&customerDBO).Error } func (r *CustomerDatasource) Update(ctx context.Context, customer *entities.Customer) error { customerDBO := dbo.CustomerDBO{ ID: customer.ID, CreatedAt: customer.CreatedAt, UpdatedAt: customer.UpdatedAt, Name: customer.Name, CompanyID: customer.CompanyID, } return r.db.WithContext(ctx).Save(&customerDBO).Error } func (r *CustomerDatasource) Delete(ctx context.Context, id ulid.ULID) error { return r.db.WithContext(ctx).Delete(&dbo.CustomerDBO{}, "id = ?", id).Error }