Skip to content

Commit

Permalink
chore: improve update
Browse files Browse the repository at this point in the history
  • Loading branch information
martinsaporiti committed Dec 27, 2024
1 parent 1b83f9a commit c5f1592
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
7 changes: 6 additions & 1 deletion internal/core/services/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,10 @@ func (s *schema) Update(ctx context.Context, schema *domain.Schema) error {
return ErrDisplayMethodNotFound
}
}
return s.repo.Update(ctx, schema)
schemaInDatabase, err := s.repo.GetByID(ctx, schema.IssuerDID, schema.ID)
if err != nil {
return err
}
schemaInDatabase.DisplayMethodID = schema.DisplayMethodID
return s.repo.Save(ctx, schemaInDatabase)
}
12 changes: 8 additions & 4 deletions internal/repositories/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ func NewSchema(conn db.Storage) *schema {

// Save stores a new entry in schemas table
func (r *schema) Save(ctx context.Context, s *domain.Schema) error {
const insertSchema = `INSERT INTO schemas (id, issuer_id, url, type, context_url, hash, words, created_at, version, title, description, display_method_id) VALUES($1, $2, $3, $4, $5, $6, $7, $8,$9,$10,$11,$12);`
const insertSchema = `INSERT INTO schemas (id, issuer_id, url, type, context_url, hash, words, created_at, version, title, description, display_method_id) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
ON CONFLICT (id) DO
UPDATE
SET issuer_id=$2, url=$3, type=$4, context_url=$5, hash=$6, words=$7, created_at=$8, version=$9, title=$10, description=$11, display_method_id=$12`

hash, err := s.Hash.MarshalText()
if err != nil {
return err
Expand Down Expand Up @@ -86,9 +90,9 @@ func (r *schema) Save(ctx context.Context, s *domain.Schema) error {

func (r *schema) Update(ctx context.Context, schema *domain.Schema) error {
const updateSchema = `
UPDATE schemas
SET issuer_id=$2, url=$3, type=$4, context_url=$5, hash=$6, words=$7, created_at=$8, version=$9, title=$10, description=$11, display_method_id=$12
WHERE schemas.id = $1;`
UPDATE schemas
SET issuer_id=$2, url=$3, type=$4, context_url=$5, hash=$6, words=$7, created_at=$8, version=$9, title=$10, description=$11, display_method_id=$12
WHERE schemas.id = $1;`
hash, err := schema.Hash.MarshalText()
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions internal/repositories/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func TestCreateSchema(t *testing.T) {
}

require.NoError(t, store.Save(ctx, schema1))
schema1.ID = uuid.New()
assert.ErrorIs(t, ErrDuplicated, store.Save(ctx, schema1), "cannot have duplicated schemas with the same version for the same issuer and type")

schema2 := schema1
Expand Down

0 comments on commit c5f1592

Please sign in to comment.