Created
November 15, 2019 16:14
-
-
Save Mentioum/ba7f1dd6c28af0538e0f893d40334288 to your computer and use it in GitHub Desktop.
Dgraph upsert exampe with dgo - Unknown best practices
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
// UpdateProposalCode takes a proposal code and updates it with the provided values | |
func (s *Storage) UpdateProposalCode(pc proposing.ProposalCode) error { | |
//Check if proposal code exists | |
//Update that propsal code to include the information in the passed propososalCode type | |
pc.UpdatedAt = time.Now() | |
q := fmt.Sprintf(` | |
query { | |
code as var(func: eq(code, %s)) | |
}`, pc.Code) | |
mus := fmt.Sprintf(` | |
uid(code) <redeemed> "%s"^^<xs:boolean> . | |
uid(code) <updated_at> "%s"^^<xs:date> . | |
uid(code) <redeemer> <%s> . | |
`, | |
strconv.FormatBool(pc.Redeemed), | |
pc.UpdatedAt.Format(time.RFC3339), | |
pc.Redeemer) | |
mu := &api.Mutation{ | |
SetNquads: []byte(mus), | |
CommitNow: true, | |
} | |
req := &api.Request{ | |
Query: q, | |
Mutations: []*api.Mutation{mu}, | |
} | |
ctx := context.Background() | |
txn := s.db.NewTxn() | |
_, err := txn.Do(ctx, req) | |
if err != nil { | |
return err | |
} | |
err = txn.Commit(ctx) | |
if err != nil { | |
return err | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment