pg_company_repository.go 3.1 KB
package repository

import (
	"time"

	"github.com/go-pg/pg/v10"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
)

type CompanyRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

var _ domain.CompanyRepository = (*CompanyRepository)(nil)

func NewCompanyRepository(tx *pgTransaction.TransactionContext) *CompanyRepository {
	return &CompanyRepository{
		transactionContext: tx,
	}
}

func (repo *CompanyRepository) Insert(u *domain.Company) (*domain.Company, error) {
	companyModel := models.Company{
		Id:            u.Id,
		Logo:          u.Logo,
		Name:          u.Name,
		Status:        u.Status,
		ChargeUserIds: u.ChargeUserIds,
		UpdatedAt:     u.UpdatedAt,
		CreatedAt:     u.CreatedAt,
		DeletedAt:     u.DeletedAt,
	}
	tx := repo.transactionContext.PgTx
	_, err := tx.Model(&companyModel).Insert()
	if err != nil {
		return nil, err
	}
	u.Id = companyModel.Id
	return u, nil
}

func (repo *CompanyRepository) Update(u *domain.Company) (*domain.Company, error) {
	companyModel := models.Company{
		Id:            u.Id,
		Logo:          u.Logo,
		Name:          u.Name,
		Status:        u.Status,
		ChargeUserIds: u.ChargeUserIds,
		UpdatedAt:     u.UpdatedAt,
		CreatedAt:     u.CreatedAt,
		DeletedAt:     u.DeletedAt,
	}
	tx := repo.transactionContext.PgTx
	_, err := tx.Model(&companyModel).WherePK().Update()
	if err != nil {
		return nil, err
	}
	return u, nil
}

func (repo *CompanyRepository) Remove(u *domain.Company) (*domain.Company, error) {
	nowTime := time.Now()
	u.DeletedAt = &nowTime
	_, err := repo.Update(u)
	return u, err
}

func (repo *CompanyRepository) FindOne(queryOptions map[string]interface{}) (*domain.Company, error) {
	tx := repo.transactionContext.PgTx
	companyModel := models.Company{}
	query := tx.Model(&companyModel)
	if v, ok := queryOptions["id"]; ok {
		query.Where("id=?", v)
	}
	err := query.First()
	if err == pg.ErrNoRows {
		return nil, ErrNoRows
	}
	if err != nil {
		return nil, err
	}
	result := repo.TransformToCompanyDomain(&companyModel)
	return result, nil
}

func (repo *CompanyRepository) Find(queryOptions map[string]interface{}) (int, []*domain.Company, error) {
	tx := repo.transactionContext.PgTx
	companyModel := []models.Company{}
	query := tx.Model(&companyModel).
		Limit(20)
	if v, ok := queryOptions["id"]; ok {
		query.Where("id=?", v)
	}
	if v, ok := queryOptions["limit"]; ok {
		query.Limit(v.(int))
	}
	if v, ok := queryOptions["offset"]; ok {
		query.Offset(v.(int))
	}
	cnt, err := query.SelectAndCount()
	if err != nil {
		return 0, nil, err
	}
	var resultList []*domain.Company
	for i := range companyModel {
		result := repo.TransformToCompanyDomain(&companyModel[i])
		resultList = append(resultList, result)
	}
	return cnt, resultList, nil
}

func (repo *CompanyRepository) TransformToCompanyDomain(m *models.Company) *domain.Company {
	return &domain.Company{
		Id:        m.Id,
		Logo:      m.Logo,
		Name:      m.Name,
		Status:    m.Status,
		UpdatedAt: m.UpdatedAt,
		CreatedAt: m.CreatedAt,
		DeletedAt: m.DeletedAt,
	}
}