7
7
8
8
"gno.land/p/demo/mux"
9
9
"gno.land/p/demo/ufmt"
10
+
10
11
"gno.land/p/moul/addrset"
11
12
"gno.land/p/moul/md"
12
13
"gno.land/r/mouss/config"
@@ -44,16 +45,16 @@ var (
44
45
45
46
Github: "https://github.com/mous1985",
46
47
LinkedIn: "https://www.linkedin.com/in/mustapha-benazzouz-88646887/",
47
- Followers: make([]std.Address, 0) ,
48
+ Followers: addrset.Set{} ,
48
49
}
49
50
router = mux.NewRouter()
50
51
recipes []*Recipe
51
52
margheritaPizza = &Recipe{
52
53
Name: "Authentic Margherita Pizza 🤌",
53
54
Origin: "Naples, 🇮🇹",
54
55
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",
57
58
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.",
58
59
}
59
60
)
@@ -101,8 +102,8 @@ func validateRecipe(name, ingredients, instructions string) error {
101
102
// If the caller is not authorized, it returns an error.
102
103
// If the address is already being followed, it returns an error.
103
104
// 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
106
107
107
108
func Follow(addr std.Address) error {
108
109
caller := std.PrevRealm().Addr()
@@ -112,12 +113,12 @@ func Follow(addr std.Address) error {
112
113
if profile.Followers.Has(addr) {
113
114
return ufmt.Errorf("address %s is already following", addr)
114
115
}
115
- profile.Followers.Add(addr)
116
+ profile.Followers.Add(addr) //can't add the same address twice
116
117
return nil
117
118
}
118
119
119
120
func isUser(addr std.Address) bool {
120
- return !isAuthorized(addr) && !profile.Followers.Has(addr)
121
+ return !isAuthorized(addr)
121
122
}
122
123
func isAuthorized(addr std.Address) bool {
123
124
return addr == config.Address() || addr == config.Backup()
@@ -126,7 +127,8 @@ func isAuthorized(addr std.Address) bool {
126
127
func renderRecipes(res *mux.ResponseWriter, req *mux.Request) {
127
128
var b strings.Builder
128
129
b.WriteString("## World Kitchen\n\n------\n\n")
129
- writeRecipe(&b, margheritaPizza)
130
+
131
+ b.WriteString(margheritaPizza.Render())
130
132
131
133
if len(recipes) == 0 {
132
134
b.WriteString("No recipes yet. Be the first to add one!\n")
@@ -135,45 +137,52 @@ func renderRecipes(res *mux.ResponseWriter, req *mux.Request) {
135
137
}
136
138
137
139
for _, recipe := range recipes {
138
- writeRecipe(&b, recipe)
140
+ b.WriteString( recipe.Render() )
139
141
}
142
+
140
143
res.Write(b.String())
141
144
}
142
145
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")
152
156
}
153
157
154
158
b.WriteString(md.HorizontalRule() + "\n")
159
+ return b.String()
155
160
}
156
161
157
162
func renderHomepage(res *mux.ResponseWriter, req *mux.Request) {
158
163
var b strings.Builder
159
164
writeNavigation(&b)
160
- writeProfile(&b )
165
+ b.WriteString(profile.Render() )
161
166
res.Write(b.String())
162
167
}
163
168
164
- func writeProfile(b *strings.Builder) {
169
+ func (p Profile) Render() string {
170
+ var b strings.Builder
171
+
165
172
b.WriteString(md.H1("Welcome to my Homepage") + "\n\n" + md.HorizontalRule() + "\n\n")
166
- writeGnoArt(b)
173
+ writeGnoArt(& b)
167
174
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")
170
177
b.WriteString(md.H3("Contact") + "\n\n")
171
178
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),
175
182
}))
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()
177
186
}
178
187
func writeNavigation(b *strings.Builder) {
179
188
navItems := []string{
@@ -189,16 +198,16 @@ func Render(path string) string {
189
198
func writeGnoArt(b *strings.Builder) {
190
199
b.WriteString("```\n")
191
200
for _, line := range []string{
192
- " -==++. ",
193
- " *@@@@= @- -@",
194
- " #@@@@@: -==-.-- :-::===: .-++-. @- .===:.- .-.-==- .===:=@",
195
- " #@@@@@@@: -@@%**%@@ #@@#*#@@- *@@**@@* @- +%=::-*@ +@=-:-@* +%=::-*@",
196
- " +@%#**#%@@ %@+ :@@ *@+ #@=+@% %@+ @= :@: -@ +% +%.@: -@",
197
- " -: - *@%:..+@@ *@+ #@=-@@: :@@= @- .@= =@ +@ *%.@= =@",
198
- " --:==+=-:=. =%@%#*@@ *@+ #@+ =%@%%@%= #* %#=.:%*===*@ +% +% -%*===*@",
199
- " -++++=++++. =-:::*@# . . .::. .. :: .:: . . .:: .",
200
- " .-=+++=: .*###%#= ",
201
- " :: ",
201
+ " -==++. ",
202
+ " *@@@@= @- -@",
203
+ " #@@@@@: -==-.-- :-::===: .-++-. @- .===:.- .-.-==- .===:=@",
204
+ " #@@@@@@@: -@@%**%@@ #@@#*#@@- *@@**@@* @- +%=::-*@ +@=-:-@* +%=::-*@",
205
+ " +@%#**#%@@ %@+ :@@ *@+ #@=+@% %@+ @= :@: -@ +% +%.@: -@",
206
+ " -: - *@%:..+@@ *@+ #@=-@@: :@@= @- .@= =@ +@ *%.@= =@",
207
+ " --:==+=-:=. =%@%#*@@ *@+ #@+ =%@%%@%= #* %#=.:%*===*@ +% +% -%*===*@",
208
+ " -++++=++++. =-:::*@# . . .::. .. :: .:: . . .:: .",
209
+ " .-=+++=: .*###%#= ",
210
+ " :: ",
202
211
} {
203
212
b.WriteString(line + "\n")
204
213
}
0 commit comments