working, on provider credential create, return created object.
This commit is contained in:
parent
cc9c1277e0
commit
cf24362806
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
type DatabaseRepository interface {
|
||||
Close() error
|
||||
GetCurrentUser() models.User
|
||||
|
||||
CreateProviderCredentials(ctx context.Context, providerCreds models.ProviderCredential) error
|
||||
CreateProviderCredentials(ctx context.Context, providerCreds *models.ProviderCredential) error
|
||||
}
|
||||
|
|
|
@ -52,6 +52,13 @@ func NewRepository(appConfig config.Interface, globalLogger logrus.FieldLogger)
|
|||
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{
|
||||
appConfig: appConfig,
|
||||
logger: globalLogger,
|
||||
|
@ -71,12 +78,17 @@ func (sr *sqliteRepository) Close() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (sr *sqliteRepository) GetCurrentUser() models.User {
|
||||
return models.User{Model: &gorm.Model{ID: 1}}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// DeviceSummary
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
func (sr *sqliteRepository) CreateProviderCredentials(ctx context.Context, providerCreds models.ProviderCredential) error {
|
||||
return sr.gormClient.WithContext(ctx).Create(&providerCreds).Error
|
||||
func (sr *sqliteRepository) CreateProviderCredentials(ctx context.Context, providerCreds *models.ProviderCredential) error {
|
||||
providerCreds.UserId = sr.GetCurrentUser().ID
|
||||
return sr.gormClient.WithContext(ctx).Create(providerCreds).Error
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -3,8 +3,8 @@ package models
|
|||
type ProviderCredential struct {
|
||||
//TODO: PRIMARY KEY should be UserId + ProviderId + PatientId
|
||||
|
||||
User User `gorm:"foreignKey:ID;references:UserId"`
|
||||
UserId int `json:"user_id"`
|
||||
User User `json:"user,omitempty" gorm:"foreignKey:ID;references:UserId"`
|
||||
UserId uint `json:"user_id"`
|
||||
|
||||
ProviderId string `json:"provider" gorm:"primaryKey"`
|
||||
PatientId string `json:"patient" gorm:"primaryKey"`
|
||||
|
|
|
@ -3,6 +3,6 @@ package models
|
|||
import "gorm.io/gorm"
|
||||
|
||||
type User struct {
|
||||
gorm.Model
|
||||
Username string `json:"username"`
|
||||
*gorm.Model
|
||||
Username string `json:"username" gorm:"unique"`
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ func CreateProviderCredentials(c *gin.Context) {
|
|||
logger := c.MustGet("LOGGER").(*logrus.Entry)
|
||||
databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository)
|
||||
|
||||
var providerCred models.ProviderCredential
|
||||
providerCred := models.ProviderCredential{}
|
||||
if err := c.ShouldBindJSON(&providerCred); err != nil {
|
||||
logger.Errorln("An error occurred while parsing posted provider credential", err)
|
||||
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)
|
||||
|
||||
err := databaseRepo.CreateProviderCredentials(c, providerCred)
|
||||
err := databaseRepo.CreateProviderCredentials(c, &providerCred)
|
||||
if err != nil {
|
||||
logger.Errorln("An error occurred while storing provider credential", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
||||
|
|
Loading…
Reference in New Issue