-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement property schema and mutations APIs
- Loading branch information
1 parent
6ef9ba0
commit 0f227d7
Showing
26 changed files
with
1,483 additions
and
522 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
app/resources/library/node_properties/properties_writer.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package node_properties | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/Southclaws/fault" | ||
"github.com/Southclaws/fault/fctx" | ||
"github.com/Southclaws/opt" | ||
"github.com/Southclaws/storyden/app/resources/library" | ||
"github.com/Southclaws/storyden/internal/ent" | ||
"github.com/Southclaws/storyden/internal/ent/property" | ||
"github.com/rs/xid" | ||
) | ||
|
||
type Writer struct { | ||
db *ent.Client | ||
} | ||
|
||
func (w *Writer) Update(ctx context.Context, nid library.NodeID, schema library.PropertySchema, props library.ExistingPropertyMutations) (*library.PropertyTable, error) { | ||
tx, err := w.db.Debug().Tx(ctx) | ||
if err != nil { | ||
return nil, fault.Wrap(err, fctx.With(ctx)) | ||
} | ||
|
||
defer func() { | ||
err = tx.Rollback() | ||
}() | ||
|
||
updated := library.PropertyTable{ | ||
Schema: schema, | ||
} | ||
|
||
for _, prop := range props { | ||
create := tx.Property.Create(). | ||
SetValue(prop.Value). | ||
SetFieldID(prop.ID). | ||
SetNodeID(xid.ID(nid)) | ||
|
||
create.OnConflictColumns(property.FieldFieldID, property.FieldNodeID).UpdateNewValues() | ||
|
||
r, err := create.Save(ctx) | ||
if err != nil { | ||
return nil, fault.Wrap(err, fctx.With(ctx)) | ||
} | ||
|
||
updated.Properties = append(updated.Properties, &library.Property{ | ||
Field: prop.PropertySchemaField, | ||
Value: opt.New(r.Value), | ||
}) | ||
} | ||
|
||
err = tx.Commit() | ||
if err != nil { | ||
return nil, fault.Wrap(err, fctx.With(ctx)) | ||
} | ||
|
||
return &updated, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.