コード例 #1
0
ファイル: coinflips.go プロジェクト: haarts/coinflips
func register(w http.ResponseWriter, r *http.Request) {
	/* not a GET request? redirect to home */
	if r.Method != "GET" {
		http.Redirect(w, r, "/", 302)
		return
	}

	coinflipKey := strings.Split(r.URL.Path, "/")[2]
	coinflip := mail.NewMailingCoinflip(coinflipKey)

	if coinflip.Result.String != "" {
		http.Redirect(w, r, "/show/"+coinflip.EncodedKey(), 302)
		return
	}

	participant, err := coinflip.FindParticipantByEmail(r.FormValue("email"))
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	participant.Register()

	count, err := coinflip.NumberOfUnregisteredParticipants()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if count == 0 {
		result := coinflip.MailResultToParticipants()
		coinflip.SetResult(result)
		coinflip.Update()
	}
	http.Redirect(w, r, "/show/"+coinflip.EncodedKey(), 302)
}
コード例 #2
0
ファイル: coinflips.go プロジェクト: haarts/coinflips
func create(w http.ResponseWriter, r *http.Request) {
	/* not a POST request? redirect to root */
	if r.Method != "POST" {
		http.Redirect(w, r, "/", 302)
		return
	}

	r.ParseForm()
	tail := stripUrls(r.Form["tail"][0])
	head := stripUrls(r.Form["head"][0])
	friends := r.Form["friends[]"]

	uniq_friends := uniq(friends)

	if tail == "" || head == "" || uniq_friends == nil {
		http.Redirect(w, r, "/", 302)
		return
	}

	coin := database.Coinflip{
		Head: head,
		Tail: tail,
	}

	err := coin.Create()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	for i := range uniq_friends {
		participant := database.Participant{Email: uniq_friends[i]}
		coin.CreateParticipant(&participant)
	}

	mailingCoinflip := mail.NewMailingCoinflip(coin.EncodedKey())
	mailingCoinflip.MailCreationToParticipants()

	http.Redirect(w, r, "/show/"+coin.EncodedKey(), 302)
}