49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
|
|
package service
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"narasi-ahli-be/app/database/entity"
|
||
|
|
"narasi-ahli-be/app/module/user_agent/repository"
|
||
|
|
|
||
|
|
"github.com/rs/zerolog"
|
||
|
|
)
|
||
|
|
|
||
|
|
type UserAgentService interface {
|
||
|
|
UpdateUserAgents(userID uint, agentIDs []uint) error
|
||
|
|
GetAgentsByUser(userID uint) ([]entity.Agent, error)
|
||
|
|
}
|
||
|
|
|
||
|
|
type userAgentService struct {
|
||
|
|
Repo repository.UserAgentRepository
|
||
|
|
Log zerolog.Logger
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
func NewUserAgentService(repo repository.UserAgentRepository, log zerolog.Logger) UserAgentService {
|
||
|
|
return &userAgentService{Repo: repo, Log: log}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *userAgentService) UpdateUserAgents(userID uint, agentIDs []uint) error {
|
||
|
|
|
||
|
|
// kosong → valid (lepas semua agent)
|
||
|
|
if len(agentIDs) == 0 {
|
||
|
|
return s.Repo.ReplaceUserAgents(userID, agentIDs)
|
||
|
|
}
|
||
|
|
|
||
|
|
validIDs, err := s.Repo.ValidateAgents(agentIDs)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
// cek apakah ada agent yang tidak exist
|
||
|
|
if len(validIDs) != len(agentIDs) {
|
||
|
|
return errors.New("one or more agent_id not found")
|
||
|
|
}
|
||
|
|
|
||
|
|
return s.Repo.ReplaceUserAgents(userID, agentIDs)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *userAgentService) GetAgentsByUser(userID uint) ([]entity.Agent, error) {
|
||
|
|
return s.Repo.GetAgentsByUser(userID)
|
||
|
|
}
|