64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
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 CompanyyDatasource struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewCompanyDatasource(db *gorm.DB) persistence.CompanyDatasource {
|
|
return &CompanyyDatasource{db: db}
|
|
}
|
|
|
|
func (r *CompanyyDatasource) Get(ctx context.Context, id ulid.ULID) (*entities.Company, error) {
|
|
var companyDBO dbo.CompanyDBO
|
|
if err := r.db.WithContext(ctx).First(&companyDBO, "id = ?", id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
company := &entities.Company{
|
|
EntityBase: entities.EntityBase{
|
|
ID: companyDBO.ID,
|
|
CreatedAt: companyDBO.CreatedAt,
|
|
UpdatedAt: companyDBO.UpdatedAt,
|
|
},
|
|
Name: companyDBO.Name,
|
|
}
|
|
|
|
return company, nil
|
|
}
|
|
|
|
func (r *CompanyyDatasource) Create(ctx context.Context, company *entities.Company) error {
|
|
companyDBO := dbo.CompanyDBO{
|
|
ID: company.ID,
|
|
CreatedAt: company.CreatedAt,
|
|
UpdatedAt: company.UpdatedAt,
|
|
Name: company.Name,
|
|
}
|
|
|
|
return r.db.WithContext(ctx).Create(&companyDBO).Error
|
|
}
|
|
|
|
func (r *CompanyyDatasource) Update(ctx context.Context, company *entities.Company) error {
|
|
companyDBO := dbo.CompanyDBO{
|
|
ID: company.ID,
|
|
CreatedAt: company.CreatedAt,
|
|
UpdatedAt: company.UpdatedAt,
|
|
Name: company.Name,
|
|
}
|
|
|
|
return r.db.WithContext(ctx).Save(&companyDBO).Error
|
|
}
|
|
|
|
func (r *CompanyyDatasource) Delete(ctx context.Context, id ulid.ULID) error {
|
|
return r.db.WithContext(ctx).Delete(&dbo.CompanyDBO{}, "id = ?", id).Error
|
|
}
|