added mechanism to query resources stored in DB.

Adding raw lookup from source.
This commit is contained in:
Jason Kulatunga 2022-09-08 22:53:54 -07:00
parent c9fc23e3a9
commit 187d72b085
6 changed files with 93 additions and 52 deletions

View File

@ -11,6 +11,7 @@ type DatabaseRepository interface {
GetCurrentUser() models.User
UpsertResource(context.Context, models.ResourceFhir) error
ListResources(context.Context, string, string) ([]models.ResourceFhir, error)
//UpsertProfile(context.Context, *models.Profile) error
//UpsertOrganziation(context.Context, *models.Organization) error

View File

@ -85,6 +85,10 @@ func (sr *sqliteRepository) GetCurrentUser() models.User {
return currentUser
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Resource
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func (sr *sqliteRepository) UpsertResource(ctx context.Context, resourceModel models.ResourceFhir) error {
sr.logger.Infof("insert/update (%T) %v", resourceModel, resourceModel)
@ -100,6 +104,27 @@ func (sr *sqliteRepository) UpsertResource(ctx context.Context, resourceModel mo
return nil
}
func (sr *sqliteRepository) ListResources(ctx context.Context, sourceResourceType string, sourceResourceId string) ([]models.ResourceFhir, error) {
queryParam := models.ResourceFhir{
OriginBase: models.OriginBase{
UserID: sr.GetCurrentUser().ID,
SourceResourceType: sourceResourceType,
},
}
if len(sourceResourceId) > 0 {
queryParam.SourceResourceID = sourceResourceId
}
var wrappedResourceModels []models.ResourceFhir
results := sr.gormClient.WithContext(ctx).
Where(queryParam).
Find(&wrappedResourceModels)
return wrappedResourceModels, results.Error
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ProviderCredentials
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -1,50 +0,0 @@
package raw
import (
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/database"
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/hub"
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/models"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"net/http"
)
func Request(c *gin.Context) {
logger := c.MustGet("LOGGER").(*logrus.Entry)
databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository)
sources, err := databaseRepo.GetSources(c)
if err != nil {
logger.Errorln("An error occurred while storing provider credential", err)
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
return
}
var foundSource *models.Source
for _, source := range sources {
if source.ProviderId == c.Param("sourceType") {
foundSource = &source
}
}
if foundSource == nil {
logger.Errorf("Did not source credentials for %s", c.Param("sourceType"))
c.JSON(http.StatusNotFound, gin.H{"success": false})
return
}
client, err := hub.NewClient(c.Param("sourceType"), nil, logger, *foundSource)
if err != nil {
logger.Errorf("Could not initialize source client", err)
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
return
}
var resp map[string]interface{}
err = client.GetRequest(c.Param("path"), &resp)
if err != nil {
logger.Errorf("Error making raw request", err)
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
return
}
c.JSON(http.StatusOK, gin.H{"success": true, "data": resp})
}

View File

@ -0,0 +1,25 @@
package handler
import (
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/database"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"net/http"
"strings"
)
func RequestResourceFhir(c *gin.Context) {
logger := c.MustGet("LOGGER").(*logrus.Entry)
databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository)
sourceResourceId := strings.Trim(c.Param("sourceResourceId"), "/")
wrappedResourceModels, err := databaseRepo.ListResources(c, c.Param("sourceResourceType"), sourceResourceId)
if err != nil {
logger.Errorln("An error occurred while retrieving resources", err)
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
return
}
c.JSON(http.StatusOK, gin.H{"success": true, "data": wrappedResourceModels})
}

View File

@ -58,3 +58,43 @@ func ListSource(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{"success": true, "data": providerCredentials})
}
func RawRequestSource(c *gin.Context) {
logger := c.MustGet("LOGGER").(*logrus.Entry)
databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository)
sources, err := databaseRepo.GetSources(c)
if err != nil {
logger.Errorln("An error occurred while storing provider credential", err)
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
return
}
var foundSource *models.Source
for _, source := range sources {
if source.ProviderId == c.Param("sourceType") {
foundSource = &source
}
}
if foundSource == nil {
logger.Errorf("Did not source credentials for %s", c.Param("sourceType"))
c.JSON(http.StatusNotFound, gin.H{"success": false})
return
}
client, err := hub.NewClient(c.Param("sourceType"), nil, logger, *foundSource)
if err != nil {
logger.Errorf("Could not initialize source client", err)
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
return
}
var resp map[string]interface{}
err = client.GetRequest(c.Param("path"), &resp)
if err != nil {
logger.Errorf("Error making raw request", err)
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
return
}
c.JSON(http.StatusOK, gin.H{"success": true, "data": resp})
}

View File

@ -4,7 +4,6 @@ import (
"fmt"
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/config"
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/web/handler"
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/web/handler/raw"
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/web/middleware"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
@ -42,8 +41,9 @@ func (ae *AppEngine) Setup(logger *logrus.Entry) *gin.Engine {
})
api.POST("/source", handler.CreateSource)
api.GET("/source", handler.ListSource)
api.GET("/source/raw/:sourceType/*path", handler.RawRequestSource)
api.GET("/raw/:sourceType/*path", raw.Request)
api.GET("/fhir/:sourceResourceType/*sourceResourceId", handler.RequestResourceFhir)
}
}