-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
516 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ func init() { | |
Patch001, | ||
Patch002, | ||
Patch003, | ||
Patch004, | ||
} | ||
} | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package pizza | ||
|
||
import ( | ||
"html/template" | ||
"net/http" | ||
"path" | ||
|
||
"github.com/mpoegel/rsvp.pizza/pkg/types" | ||
zap "go.uber.org/zap" | ||
) | ||
|
||
type Preference struct { | ||
Name string | ||
IsSelected bool | ||
} | ||
|
||
type ProfilePageData struct { | ||
LoggedIn bool | ||
Name string | ||
Toppings []Preference | ||
Cheese []Preference | ||
Sauce []Preference | ||
Doneness []Preference | ||
} | ||
|
||
func (s *Server) HandleGetProfile(w http.ResponseWriter, r *http.Request) { | ||
plate, err := template.ParseFiles(path.Join(s.config.StaticDir, "html/profile.html")) | ||
if err != nil { | ||
Log.Error("template index failure", zap.Error(err)) | ||
s.Handle500(w, r) | ||
return | ||
} | ||
|
||
toppings := make(map[types.Topping]bool) | ||
cheeses := make(map[types.Cheese]bool) | ||
sauces := make(map[types.Sauce]bool) | ||
var doneness types.Doneness | ||
|
||
data := ProfilePageData{ | ||
LoggedIn: false, | ||
} | ||
|
||
claims, ok := s.authenticateRequest(r) | ||
if ok { | ||
data.LoggedIn = true | ||
data.Name = claims.GivenName | ||
|
||
prefs, err := s.store.GetPreferences(claims.Email) | ||
if err != nil { | ||
Log.Error("failed to get preferences", zap.Error(err), zap.String("email", claims.Email)) | ||
} | ||
for _, t := range prefs.Toppings { | ||
toppings[t] = true | ||
} | ||
for _, c := range prefs.Cheese { | ||
cheeses[c] = true | ||
} | ||
for _, s := range prefs.Sauce { | ||
sauces[s] = true | ||
} | ||
doneness = prefs.Doneness | ||
} | ||
|
||
data.Toppings = []Preference{ | ||
{Name: types.Banana_Peppers.String(), IsSelected: toppings[types.Banana_Peppers]}, | ||
{Name: types.Basil.String(), IsSelected: toppings[types.Basil]}, | ||
{Name: types.Barbecue_Chicken.String(), IsSelected: toppings[types.Barbecue_Chicken]}, | ||
{Name: types.Buffalo_Chicken.String(), IsSelected: toppings[types.Buffalo_Chicken]}, | ||
{Name: types.Jalapeno.String(), IsSelected: toppings[types.Jalapeno]}, | ||
{Name: types.Pepperoni.String(), IsSelected: toppings[types.Pepperoni]}, | ||
{Name: types.Prosciutto.String(), IsSelected: toppings[types.Prosciutto]}, | ||
{Name: types.Soppressata.String(), IsSelected: toppings[types.Soppressata]}, | ||
} | ||
data.Cheese = []Preference{ | ||
{Name: types.Shredded_Mozzarella.String(), IsSelected: cheeses[types.Shredded_Mozzarella]}, | ||
{Name: types.Whole_Mozzarella.String(), IsSelected: cheeses[types.Whole_Mozzarella]}, | ||
{Name: types.Cheddar.String(), IsSelected: cheeses[types.Cheddar]}, | ||
{Name: types.Ricotta.String(), IsSelected: cheeses[types.Ricotta]}, | ||
} | ||
data.Sauce = []Preference{ | ||
{Name: types.Raw_Tomatoes.String(), IsSelected: sauces[types.Raw_Tomatoes]}, | ||
{Name: types.Cooked_Tomatoes.String(), IsSelected: sauces[types.Cooked_Tomatoes]}, | ||
{Name: types.Basil_Pesto.String(), IsSelected: sauces[types.Basil_Pesto]}, | ||
} | ||
data.Doneness = []Preference{ | ||
{Name: types.Well_Done.String(), IsSelected: doneness == types.Well_Done}, | ||
{Name: types.Medium_Well.String(), IsSelected: doneness == types.Medium_Well}, | ||
{Name: types.Medium.String(), IsSelected: doneness == types.Medium}, | ||
{Name: types.Medium_Rare.String(), IsSelected: doneness == types.Medium_Rare}, | ||
{Name: types.Rare.String(), IsSelected: doneness == types.Rare}, | ||
} | ||
|
||
if err = plate.ExecuteTemplate(w, "Profile", data); err != nil { | ||
Log.Error("template execution failure", zap.Error(err)) | ||
s.Handle500(w, r) | ||
return | ||
} | ||
} | ||
|
||
func (s *Server) HandleUpdateProfile(w http.ResponseWriter, r *http.Request) { | ||
claims, ok := s.authenticateRequest(r) | ||
if !ok { | ||
w.Write(getToast("not logged in")) | ||
return | ||
} | ||
|
||
if err := r.ParseForm(); err != nil { | ||
Log.Error("form parse failure on profile edit", zap.Error(err)) | ||
w.Write(getToast("bad request")) | ||
return | ||
} | ||
|
||
prefs := Preferences{ | ||
Toppings: types.ParseToppings(r.Form["toppings"]), | ||
Cheese: types.ParseCheeses(r.Form["cheese"]), | ||
Sauce: types.ParseSauces(r.Form["sauce"]), | ||
Doneness: types.ParseDoneness(r.Form["doneness"][0]), | ||
} | ||
|
||
Log.Info("got profile update", zap.Any("preferences", prefs)) | ||
|
||
if err := s.store.SetPreferences(claims.Email, prefs); err != nil { | ||
Log.Error("failed to set preferences", zap.Error(err), zap.String("email", claims.Email)) | ||
w.Write(getToast("failed to set preferences")) | ||
return | ||
} | ||
|
||
w.Write(getToast("preferences updated")) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package types | ||
|
||
type Cheese int | ||
|
||
const ( | ||
Shredded_Mozzarella Cheese = iota + 1 | ||
Whole_Mozzarella | ||
Cheddar | ||
Ricotta | ||
) | ||
|
||
func ParseCheeses(cheeses []string) []Cheese { | ||
res := make([]Cheese, 0) | ||
for _, c := range cheeses { | ||
res = append(res, ParseCheese(c)) | ||
} | ||
return res | ||
} | ||
|
||
func ParseCheese(cheese string) Cheese { | ||
switch cheese { | ||
case "Shredded Mozzarella": | ||
return Shredded_Mozzarella | ||
case "Whole Mozzarella": | ||
return Whole_Mozzarella | ||
case "Cheddar": | ||
return Cheddar | ||
case "Ricotta": | ||
return Ricotta | ||
default: | ||
return 0 | ||
} | ||
} | ||
|
||
func (c Cheese) String() string { | ||
switch c { | ||
case Shredded_Mozzarella: | ||
return "Shredded Mozzarella" | ||
case Whole_Mozzarella: | ||
return "Whole Mozzarella" | ||
case Cheddar: | ||
return "Cheddar" | ||
case Ricotta: | ||
return "Ricotta" | ||
default: | ||
return "" | ||
} | ||
} |
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,45 @@ | ||
package types | ||
|
||
type Doneness int | ||
|
||
const ( | ||
Well_Done Doneness = iota + 1 | ||
Medium_Well | ||
Medium | ||
Medium_Rare | ||
Rare | ||
) | ||
|
||
func ParseDoneness(doneness string) Doneness { | ||
switch doneness { | ||
case "Well Done": | ||
return Well_Done | ||
case "Medium Well": | ||
return Medium_Well | ||
case "Medium": | ||
return Medium | ||
case "Medium Rare": | ||
return Medium_Rare | ||
case "Rare": | ||
return Rare | ||
default: | ||
return 0 | ||
} | ||
} | ||
|
||
func (d Doneness) String() string { | ||
switch d { | ||
case Well_Done: | ||
return "Well Done" | ||
case Medium_Well: | ||
return "Medium Well" | ||
case Medium: | ||
return "Medium" | ||
case Medium_Rare: | ||
return "Medium Rare" | ||
case Rare: | ||
return "Rare" | ||
default: | ||
return "" | ||
} | ||
} |
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,43 @@ | ||
package types | ||
|
||
type Sauce int | ||
|
||
const ( | ||
Raw_Tomatoes Sauce = iota + 1 | ||
Cooked_Tomatoes | ||
Basil_Pesto | ||
) | ||
|
||
func ParseSauces(sauces []string) []Sauce { | ||
res := make([]Sauce, 0) | ||
for _, s := range sauces { | ||
res = append(res, ParseSauce(s)) | ||
} | ||
return res | ||
} | ||
|
||
func ParseSauce(sauce string) Sauce { | ||
switch sauce { | ||
case "Raw Tomatoes": | ||
return Raw_Tomatoes | ||
case "Cooked Tomatoes": | ||
return Cooked_Tomatoes | ||
case "Basil Pesto": | ||
return Basil_Pesto | ||
default: | ||
return 0 | ||
} | ||
} | ||
|
||
func (s Sauce) String() string { | ||
switch s { | ||
case Raw_Tomatoes: | ||
return "Raw Tomatoes" | ||
case Cooked_Tomatoes: | ||
return "Cooked Tomatoes" | ||
case Basil_Pesto: | ||
return "Basil Pesto" | ||
default: | ||
return "" | ||
} | ||
} |
Oops, something went wrong.