From 187d72b085abaaf65e55640a573ff2d5e1acb920 Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Thu, 8 Sep 2022 22:53:54 -0700 Subject: [PATCH] added mechanism to query resources stored in DB. Adding raw lookup from source. --- backend/pkg/database/interface.go | 1 + backend/pkg/database/sqlite_repository.go | 25 ++++++++++++ backend/pkg/web/handler/raw/request.go | 50 ----------------------- backend/pkg/web/handler/resource_fhir.go | 25 ++++++++++++ backend/pkg/web/handler/source.go | 40 ++++++++++++++++++ backend/pkg/web/server.go | 4 +- 6 files changed, 93 insertions(+), 52 deletions(-) delete mode 100644 backend/pkg/web/handler/raw/request.go create mode 100644 backend/pkg/web/handler/resource_fhir.go diff --git a/backend/pkg/database/interface.go b/backend/pkg/database/interface.go index cc83da35..2bc614df 100644 --- a/backend/pkg/database/interface.go +++ b/backend/pkg/database/interface.go @@ -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 diff --git a/backend/pkg/database/sqlite_repository.go b/backend/pkg/database/sqlite_repository.go index 1edbe44e..2298e826 100644 --- a/backend/pkg/database/sqlite_repository.go +++ b/backend/pkg/database/sqlite_repository.go @@ -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 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/backend/pkg/web/handler/raw/request.go b/backend/pkg/web/handler/raw/request.go deleted file mode 100644 index 204c8ec0..00000000 --- a/backend/pkg/web/handler/raw/request.go +++ /dev/null @@ -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}) -} diff --git a/backend/pkg/web/handler/resource_fhir.go b/backend/pkg/web/handler/resource_fhir.go new file mode 100644 index 00000000..d63908bf --- /dev/null +++ b/backend/pkg/web/handler/resource_fhir.go @@ -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}) +} diff --git a/backend/pkg/web/handler/source.go b/backend/pkg/web/handler/source.go index 68a970e9..d0023523 100644 --- a/backend/pkg/web/handler/source.go +++ b/backend/pkg/web/handler/source.go @@ -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}) +} diff --git a/backend/pkg/web/server.go b/backend/pkg/web/server.go index 09c50eb5..d5b7a93a 100644 --- a/backend/pkg/web/server.go +++ b/backend/pkg/web/server.go @@ -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) } }