package applicationservice

import (
	"bitbucket.org/nemt/nemt-portal-api/application/entitymapping"
	"bitbucket.org/nemt/nemt-portal-api/application/viewmodel"
	"bitbucket.org/nemt/nemt-portal-api/domain/service"
)

// zipcodeService holds methods to participating zipcode application service
type zipcodeService struct {
	svc       *service.Service
	mapEntity *entitymapping.Mapper
}

// newZipcodeService returns a zipcodeService instance
func newZipcodeService(svc *service.Service, mapper *entitymapping.Mapper) *zipcodeService {
	return &zipcodeService{
		svc:       svc,
		mapEntity: mapper,
	}
}

func (s *zipcodeService) GetAll() ([]viewmodel.Zipcode, error) {
	result, err := s.svc.Zipcodes.GetAll()
	if err != nil {
		return nil, err
	}
	return s.mapEntity.Zipcode.ToZipcodeModelSlice(result), nil
}

func (s *zipcodeService) GetByParticipatingZipcode(zipcode string) (viewmodel.Zipcode, error) {
	result, err := s.svc.Zipcodes.GetByParticipatingZipcode(zipcode)
	if err != nil {
		return viewmodel.Zipcode{}, err
	}
	return s.mapEntity.Zipcode.ToZipcodeModel(result), nil
}