Skip to content

Commit 4959189

Browse files
committed
ref: func writeRecipe to method (r Recipe)Render
func writeProfile to method (p Profile)Render
1 parent f64569a commit 4959189

File tree

1 file changed

+46
-37
lines changed

1 file changed

+46
-37
lines changed

examples/gno.land/r/mouss/home/home.gno

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"gno.land/p/demo/mux"
99
"gno.land/p/demo/ufmt"
10+
1011
"gno.land/p/moul/addrset"
1112
"gno.land/p/moul/md"
1213
"gno.land/r/mouss/config"
@@ -44,16 +45,16 @@ var (
4445
4546
Github: "https://github.com/mous1985",
4647
LinkedIn: "https://www.linkedin.com/in/mustapha-benazzouz-88646887/",
47-
Followers: make([]std.Address, 0),
48+
Followers: addrset.Set{},
4849
}
4950
router = mux.NewRouter()
5051
recipes []*Recipe
5152
margheritaPizza = &Recipe{
5253
Name: "Authentic Margherita Pizza 🤌",
5354
Origin: "Naples, 🇮🇹",
5455
Author: config.Address(),
55-
Ingredients: " \n\n- 1kg 00 flour\n\n- 500ml water\n\n- 3g fresh yeast\n\n- 20g sea salt\n\n- San Marzano tomatoes\n\n- Fresh buffalo mozzarella\n\n- Fresh basil\n\n- Extra virgin olive oil",
56-
Instructions: "\n\n1. Mix flour and water until incorporated\n\n2. Add yeast and salt, knead for 20 minutes\n\n3. Let rise for 2 hours at room temperature\n\n4. Divide into 250g balls\n\n5. Cold ferment for 24-48 hours\n\n6. Shape by hand, being gentle with the dough\n\n7. Top with crushed tomatoes, torn mozzarella, and basil\n\n8. Cook at 450°C for 60-90 seconds",
56+
Ingredients: " 1kg 00 flour\n 500ml water\n 3g fresh yeast\n 20g sea salt\n San Marzano tomatoes\n Fresh buffalo mozzarella\n Fresh basil\n Extra virgin olive oil",
57+
Instructions: " Mix flour and water until incorporated\n Add yeast and salt, knead for 20 minutes\n Let rise for 2 hours at room temperature\n Divide into 250g balls\n Cold ferment for 24-48 hours\n Shape by hand, being gentle with the dough\n Top with crushed tomatoes, torn mozzarella, and basil\n Cook at 450°C for 60-90 seconds",
5758
Tips: "Use a pizza steel or stone preheated for at least 1 hour. The dough should be soft and extensible. For best results, cook in a wood-fired oven.",
5859
}
5960
)
@@ -101,8 +102,8 @@ func validateRecipe(name, ingredients, instructions string) error {
101102
// If the caller is not authorized, it returns an error.
102103
// If the address is already being followed, it returns an error.
103104
// Otherwise, it adds the address to the list of followers and returns nil.
104-
//TODO:any user can follow and to be followed by any other user
105-
//TODO: add a function to unfollow
105+
// TODO:any user can follow and to be followed by any other user
106+
// TODO: add a function to unfollow
106107

107108
func Follow(addr std.Address) error {
108109
caller := std.PrevRealm().Addr()
@@ -112,12 +113,12 @@ func Follow(addr std.Address) error {
112113
if profile.Followers.Has(addr) {
113114
return ufmt.Errorf("address %s is already following", addr)
114115
}
115-
profile.Followers.Add(addr)
116+
profile.Followers.Add(addr) //can't add the same address twice
116117
return nil
117118
}
118119

119120
func isUser(addr std.Address) bool {
120-
return !isAuthorized(addr) && !profile.Followers.Has(addr)
121+
return !isAuthorized(addr)
121122
}
122123
func isAuthorized(addr std.Address) bool {
123124
return addr == config.Address() || addr == config.Backup()
@@ -126,7 +127,8 @@ func isAuthorized(addr std.Address) bool {
126127
func renderRecipes(res *mux.ResponseWriter, req *mux.Request) {
127128
var b strings.Builder
128129
b.WriteString("## World Kitchen\n\n------\n\n")
129-
writeRecipe(&b, margheritaPizza)
130+
131+
b.WriteString(margheritaPizza.Render())
130132

131133
if len(recipes) == 0 {
132134
b.WriteString("No recipes yet. Be the first to add one!\n")
@@ -135,45 +137,52 @@ func renderRecipes(res *mux.ResponseWriter, req *mux.Request) {
135137
}
136138

137139
for _, recipe := range recipes {
138-
writeRecipe(&b, recipe)
140+
b.WriteString(recipe.Render())
139141
}
142+
140143
res.Write(b.String())
141144
}
142145

143-
func writeRecipe(b *strings.Builder, recipe *Recipe) {
144-
b.WriteString(md.H2(recipe.Name))
145-
b.WriteString(md.Bold("Author: ") + recipe.Author.String() + "\n\n")
146-
b.WriteString(md.Bold("Origin: ") + recipe.Origin + "\n\n")
147-
b.WriteString(md.Bold("Ingredients:") + "\n" + md.BulletList(strings.Split(recipe.Ingredients, "\n")) + "\n")
148-
b.WriteString(md.Bold("Instructions:") + "\n" + md.OrderedList(strings.Split(recipe.Instructions, "\n")) + "\n")
149-
150-
if recipe.Tips != "" {
151-
b.WriteString(md.Italic("💡 Tips: "+recipe.Tips) + "\n\n")
146+
func (r Recipe) Render() string {
147+
var b strings.Builder
148+
b.WriteString(md.H2(r.Name))
149+
b.WriteString(md.Bold("Author:") + "\n" + r.Author.String() + "\n\n")
150+
b.WriteString(md.Bold("Origin:") + "\n" + r.Origin + "\n\n")
151+
b.WriteString(md.Bold("Ingredients:") + "\n" + md.BulletList(strings.Split(r.Ingredients, "\n")) + "\n\n")
152+
b.WriteString(md.Bold("Instructions:") + "\n" + md.OrderedList(strings.Split(r.Instructions, "\n")) + "\n\n")
153+
154+
if r.Tips != "" {
155+
b.WriteString(md.Italic("💡 Tips:"+"\n"+r.Tips) + "\n\n")
152156
}
153157

154158
b.WriteString(md.HorizontalRule() + "\n")
159+
return b.String()
155160
}
156161

157162
func renderHomepage(res *mux.ResponseWriter, req *mux.Request) {
158163
var b strings.Builder
159164
writeNavigation(&b)
160-
writeProfile(&b)
165+
b.WriteString(profile.Render())
161166
res.Write(b.String())
162167
}
163168

164-
func writeProfile(b *strings.Builder) {
169+
func (p Profile) Render() string {
170+
var b strings.Builder
171+
165172
b.WriteString(md.H1("Welcome to my Homepage") + "\n\n" + md.HorizontalRule() + "\n\n")
166-
writeGnoArt(b)
173+
writeGnoArt(&b)
167174
b.WriteString(md.HorizontalRule() + "\n\n" + md.H2("About Me") + "\n\n")
168-
b.WriteString(md.Image("avatar", profile.Avatar) + "\n\n")
169-
b.WriteString(profile.AboutMe + "\n\n" + md.HorizontalRule() + "\n\n")
175+
b.WriteString(md.Image("avatar", p.Avatar) + "\n\n")
176+
b.WriteString(p.AboutMe + "\n\n" + md.HorizontalRule() + "\n\n")
170177
b.WriteString(md.H3("Contact") + "\n\n")
171178
b.WriteString(md.BulletList([]string{
172-
"Email: " + profile.Email,
173-
"GitHub: " + md.Link("@mous1985", profile.Github),
174-
"LinkedIn: " + md.Link("Mustapha", profile.LinkedIn),
179+
"Email: " + p.Email,
180+
"GitHub: " + md.Link("@mous1985", p.Github),
181+
"LinkedIn: " + md.Link("Mustapha", p.LinkedIn),
175182
}))
176-
b.WriteString("\n\n" + md.Bold("👤 Followers: ") + strconv.Itoa(profile.Followers.Size()))
183+
b.WriteString("\n\n" + md.Bold("👤 Followers: ") + strconv.Itoa(p.Followers.Size()))
184+
185+
return b.String()
177186
}
178187
func writeNavigation(b *strings.Builder) {
179188
navItems := []string{
@@ -189,16 +198,16 @@ func Render(path string) string {
189198
func writeGnoArt(b *strings.Builder) {
190199
b.WriteString("```\n")
191200
for _, line := range []string{
192-
" -==++. ",
193-
" *@@@@= @- -@",
194-
" #@@@@@: -==-.-- :-::===: .-++-. @- .===:.- .-.-==- .===:=@",
195-
" #@@@@@@@: -@@%**%@@ #@@#*#@@- *@@**@@* @- +%=::-*@ +@=-:-@* +%=::-*@",
196-
" +@%#**#%@@ %@+ :@@ *@+ #@=+@% %@+ @= :@: -@ +% +%.@: -@",
197-
" -: - *@%:..+@@ *@+ #@=-@@: :@@= @- .@= =@ +@ *%.@= =@",
198-
" --:==+=-:=. =%@%#*@@ *@+ #@+ =%@%%@%= #* %#=.:%*===*@ +% +% -%*===*@",
199-
" -++++=++++. =-:::*@# . . .::. .. :: .:: . . .:: .",
200-
" .-=+++=: .*###%#= ",
201-
" :: ",
201+
" -==++. ",
202+
" *@@@@= @- -@",
203+
" #@@@@@: -==-.-- :-::===: .-++-. @- .===:.- .-.-==- .===:=@",
204+
" #@@@@@@@: -@@%**%@@ #@@#*#@@- *@@**@@* @- +%=::-*@ +@=-:-@* +%=::-*@",
205+
" +@%#**#%@@ %@+ :@@ *@+ #@=+@% %@+ @= :@: -@ +% +%.@: -@",
206+
" -: - *@%:..+@@ *@+ #@=-@@: :@@= @- .@= =@ +@ *%.@= =@",
207+
" --:==+=-:=. =%@%#*@@ *@+ #@+ =%@%%@%= #* %#=.:%*===*@ +% +% -%*===*@",
208+
" -++++=++++. =-:::*@# . . .::. .. :: .:: . . .:: .",
209+
" .-=+++=: .*###%#= ",
210+
" :: ",
202211
} {
203212
b.WriteString(line + "\n")
204213
}

0 commit comments

Comments
 (0)