working, on provider credential create, return created object.

This commit is contained in:
Jason Kulatunga 2022-08-27 08:39:55 -07:00
parent cc9c1277e0
commit cf24362806
5 changed files with 22 additions and 9 deletions

View File

@ -7,6 +7,7 @@ import (
type DatabaseRepository interface { type DatabaseRepository interface {
Close() error Close() error
GetCurrentUser() models.User
CreateProviderCredentials(ctx context.Context, providerCreds models.ProviderCredential) error CreateProviderCredentials(ctx context.Context, providerCreds *models.ProviderCredential) error
} }

View File

@ -52,6 +52,13 @@ func NewRepository(appConfig config.Interface, globalLogger logrus.FieldLogger)
return nil, fmt.Errorf("Failed to automigrate! - %v", err) return nil, fmt.Errorf("Failed to automigrate! - %v", err)
} }
// create/update admin user
adminUser := models.User{}
err = database.FirstOrCreate(&adminUser, models.User{Username: "admin"}).Error
if err != nil {
return nil, fmt.Errorf("Failed to create admin user! - %v", err)
}
deviceRepo := sqliteRepository{ deviceRepo := sqliteRepository{
appConfig: appConfig, appConfig: appConfig,
logger: globalLogger, logger: globalLogger,
@ -71,12 +78,17 @@ func (sr *sqliteRepository) Close() error {
return nil return nil
} }
func (sr *sqliteRepository) GetCurrentUser() models.User {
return models.User{Model: &gorm.Model{ID: 1}}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DeviceSummary // DeviceSummary
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func (sr *sqliteRepository) CreateProviderCredentials(ctx context.Context, providerCreds models.ProviderCredential) error { func (sr *sqliteRepository) CreateProviderCredentials(ctx context.Context, providerCreds *models.ProviderCredential) error {
return sr.gormClient.WithContext(ctx).Create(&providerCreds).Error providerCreds.UserId = sr.GetCurrentUser().ID
return sr.gormClient.WithContext(ctx).Create(providerCreds).Error
} }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -3,8 +3,8 @@ package models
type ProviderCredential struct { type ProviderCredential struct {
//TODO: PRIMARY KEY should be UserId + ProviderId + PatientId //TODO: PRIMARY KEY should be UserId + ProviderId + PatientId
User User `gorm:"foreignKey:ID;references:UserId"` User User `json:"user,omitempty" gorm:"foreignKey:ID;references:UserId"`
UserId int `json:"user_id"` UserId uint `json:"user_id"`
ProviderId string `json:"provider" gorm:"primaryKey"` ProviderId string `json:"provider" gorm:"primaryKey"`
PatientId string `json:"patient" gorm:"primaryKey"` PatientId string `json:"patient" gorm:"primaryKey"`

View File

@ -3,6 +3,6 @@ package models
import "gorm.io/gorm" import "gorm.io/gorm"
type User struct { type User struct {
gorm.Model *gorm.Model
Username string `json:"username"` Username string `json:"username" gorm:"unique"`
} }

View File

@ -12,7 +12,7 @@ func CreateProviderCredentials(c *gin.Context) {
logger := c.MustGet("LOGGER").(*logrus.Entry) logger := c.MustGet("LOGGER").(*logrus.Entry)
databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository) databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository)
var providerCred models.ProviderCredential providerCred := models.ProviderCredential{}
if err := c.ShouldBindJSON(&providerCred); err != nil { if err := c.ShouldBindJSON(&providerCred); err != nil {
logger.Errorln("An error occurred while parsing posted provider credential", err) logger.Errorln("An error occurred while parsing posted provider credential", err)
c.JSON(http.StatusBadRequest, gin.H{"success": false}) c.JSON(http.StatusBadRequest, gin.H{"success": false})
@ -21,7 +21,7 @@ func CreateProviderCredentials(c *gin.Context) {
logger.Infof("Parsed Create Provider Credentials Payload: %v", providerCred) logger.Infof("Parsed Create Provider Credentials Payload: %v", providerCred)
err := databaseRepo.CreateProviderCredentials(c, providerCred) err := databaseRepo.CreateProviderCredentials(c, &providerCred)
if err != nil { if err != nil {
logger.Errorln("An error occurred while storing provider credential", err) logger.Errorln("An error occurred while storing provider credential", err)
c.JSON(http.StatusInternalServerError, gin.H{"success": false}) c.JSON(http.StatusInternalServerError, gin.H{"success": false})