Files
old-svijetlastrana/server/router/eligibilityroute/controller.go
2018-05-16 18:30:59 +02:00

68 lines
1.5 KiB
Go

package eligibilityroute
import (
"encoding/xml"
"fmt"
"html"
"strings"
"sync"
"bitbucket.org/nemt/nemt-portal-api/application/applicationservice"
"bitbucket.org/nemt/nemt-portal-api/application/third/eligibility/bcbsi"
"bitbucket.org/nemt/nemt-portal-api/application/viewmodel"
"bitbucket.org/nemt/nemt-portal-api/infra/config"
"bitbucket.org/nemt/nemt-portal-api/server/router/routeutils"
"github.com/labstack/echo"
)
var (
instance *controller
once sync.Once
)
type controller struct {
cfg *config.Config
svc *applicationservice.Service
bcbsi *bcbsi.Service
}
func controllerInstance(cfg *config.Config, svc *applicationservice.Service) *controller {
once.Do(func() {
instance = &controller{
cfg: cfg,
svc: svc,
bcbsi: bcbsi.New(cfg),
}
})
return instance
}
func (c *controller) handleEligibility(ctx echo.Context) error {
var eligibility viewmodel.Eligibility
if err := ctx.Bind(&eligibility); err != nil {
return routeutils.HandleAPIError(ctx, err)
}
ret, err := c.bcbsi.BXE.CheckEligibility(eligibility)
if err != nil {
fmt.Println("Error Here: ", err.Error())
return routeutils.HandleAPIError(ctx, err)
}
if ret.QueryResult.HIPPA271.T271 != "" {
xmlString := html.UnescapeString(ret.QueryResult.HIPPA271.T271)
xmlReader := strings.NewReader(xmlString)
var f viewmodel.Interchange271
err = xml.NewDecoder(xmlReader).Decode(&f)
if err != nil {
fmt.Println("Error to unmarshal: ", err.Error())
}
return ctx.JSON(200, f)
} else {
return routeutils.ResponseAPIOK(ctx, ret)
}
}