67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package institutions
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/repository"
|
|
)
|
|
|
|
type Service struct {
|
|
repo repository.BankRepository
|
|
}
|
|
|
|
func New(repo repository.BankRepository) *Service {
|
|
return &Service{repo: repo}
|
|
}
|
|
|
|
func (s *Service) Create(ctx context.Context, bank *domain.Bank) error {
|
|
return s.repo.CreateBank(ctx, bank)
|
|
}
|
|
|
|
func (s *Service) Update(ctx context.Context, bank *domain.Bank) error {
|
|
return s.repo.UpdateBank(ctx, bank)
|
|
}
|
|
|
|
func (s *Service) GetByID(ctx context.Context, id int64) (*domain.Bank, error) {
|
|
return s.repo.GetBankByID(ctx, int(id))
|
|
}
|
|
|
|
func (s *Service) Delete(ctx context.Context, id int64) error {
|
|
return s.repo.DeleteBank(ctx, int(id))
|
|
}
|
|
|
|
func (s *Service) List(
|
|
ctx context.Context,
|
|
countryID *int,
|
|
isActive *bool,
|
|
searchTerm *string,
|
|
page int,
|
|
pageSize int,
|
|
) ([]*domain.Bank, *domain.Pagination, error) {
|
|
banks, total, err := s.repo.GetAllBanks(ctx, countryID, isActive, searchTerm, page, pageSize)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
result := make([]*domain.Bank, len(banks))
|
|
for i := range banks {
|
|
result[i] = &banks[i]
|
|
}
|
|
|
|
// Calculate pagination metadata
|
|
totalPages := int(total) / pageSize
|
|
if int(total)%pageSize != 0 {
|
|
totalPages++
|
|
}
|
|
|
|
pagination := &domain.Pagination{
|
|
Total: int(total),
|
|
TotalPages: totalPages,
|
|
CurrentPage: page,
|
|
Limit: pageSize,
|
|
}
|
|
|
|
return result, pagination, nil
|
|
}
|