-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from tapglue/invite-flow
Invite flow
- Loading branch information
Showing
14 changed files
with
1,036 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package core | ||
|
||
import ( | ||
"github.com/tapglue/snaas/service/app" | ||
"github.com/tapglue/snaas/service/invite" | ||
) | ||
|
||
// InviteCreateFunc stores the key and value for the users invite. | ||
type InviteCreateFunc func( | ||
currentApp *app.App, | ||
origin Origin, | ||
key, value string, | ||
) error | ||
|
||
func InviteCreate(invites invite.Service) InviteCreateFunc { | ||
return func( | ||
currentApp *app.App, | ||
origin Origin, | ||
key, value string, | ||
) error { | ||
is, err := invites.Query(currentApp.Namespace(), invite.QueryOptions{ | ||
Keys: []string{ | ||
key, | ||
}, | ||
UserIDs: []uint64{ | ||
origin.UserID, | ||
}, | ||
Values: []string{ | ||
value, | ||
}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(is) == 1 { | ||
return nil | ||
} | ||
|
||
_, err = invites.Put(currentApp.Namespace(), &invite.Invite{ | ||
Key: key, | ||
UserID: origin.UserID, | ||
Value: value, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return 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
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,40 @@ | ||
package http | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"golang.org/x/net/context" | ||
|
||
"github.com/tapglue/snaas/core" | ||
) | ||
|
||
// InviteCreate stores the key and value for a users invite. | ||
func InviteCreate(fn core.InviteCreateFunc) Handler { | ||
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) { | ||
var ( | ||
currentApp = appFromContext(ctx) | ||
origin = originFromContext(ctx) | ||
p = payloadInvite{} | ||
) | ||
|
||
err := json.NewDecoder(r.Body).Decode(&p) | ||
if err != nil { | ||
respondError(w, 0, wrapError(ErrBadRequest, err.Error())) | ||
return | ||
} | ||
|
||
err = fn(currentApp, origin, p.Key, p.Value) | ||
if err != nil { | ||
respondError(w, 0, err) | ||
return | ||
} | ||
|
||
respondJSON(w, http.StatusNoContent, nil) | ||
} | ||
} | ||
|
||
type payloadInvite struct { | ||
Key string `json:"key"` | ||
Value string `json:"value"` | ||
} |
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
Oops, something went wrong.