MCPcopy
hub / github.com/benbjohnson/wtf / handleDialMembershipCreate

Method handleDialMembershipCreate

http/dial_membership.go:68–100  ·  view source on GitHub ↗

handleDialMembershipCreate handles the "POST /invite/:code" route. This route adds a new membership for the current user to a dial.

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

66// handleDialMembershipCreate handles the "POST /invite/:code" route.
67// This route adds a new membership for the current user to a dial.
68func (s *Server) handleDialMembershipCreate(w http.ResponseWriter, r *http.Request) {
69 // Read user ID for currently logged in user.
70 userID := wtf.UserIDFromContext(r.Context())
71
72 // Read invite code from URL path.
73 code := mux.Vars(r)["code"]
74
75 // Look up dial by invite code.
76 dials, _, err := s.DialService.FindDials(r.Context(), wtf.DialFilter{InviteCode: &code})
77 if err != nil {
78 Error(w, r, err)
79 return
80 } else if len(dials) == 0 {
81 Error(w, r, wtf.Errorf(wtf.ENOTFOUND, "Invalid invitation URL."))
82 return
83 }
84
85 // Create a new membership between the current user and the dial associated
86 // with the invite code.
87 membership := &wtf.DialMembership{
88 DialID: dials[0].ID,
89 UserID: userID,
90 }
91 if err := s.DialMembershipService.CreateDialMembership(r.Context(), membership); err != nil {
92 Error(w, r, err)
93 return
94 }
95
96 // Let the user know they've joined the dial and then redirect them to the
97 // dial's page.
98 SetFlash(w, fmt.Sprintf("You have now joined the %q dial.", membership.Dial.Name))
99 http.Redirect(w, r, fmt.Sprintf("/dials/%d", membership.DialID), http.StatusFound)
100}
101
102// handleDialMembershipUpdate handles the "PATCH /dial-memberships/:id" route.
103// This route is only called via JSON API on the dial view page.

Callers

nothing calls this directly

Calls 6

UserIDFromContextFunction · 0.92
ErrorfFunction · 0.92
SetFlashFunction · 0.85
ErrorFunction · 0.70
FindDialsMethod · 0.65
CreateDialMembershipMethod · 0.65

Tested by

no test coverage detected