From 64ff09500278b4cb14ca8f4196249c1e85bb54fc Mon Sep 17 00:00:00 2001
From: yequari 
Date: Sun, 23 Mar 2025 15:15:08 -0700
Subject: [PATCH] add page to display all guestbooks
---
 cmd/web/handlers_guestbook.go |  8 ++++
 cmd/web/handlers_website.go   |  2 +-
 cmd/web/routes.go             |  1 +
 internal/models/website.go    | 26 ++++++++++-
 ui/views/common.templ         |  1 +
 ui/views/common_templ.go      |  8 ++--
 ui/views/common_templ.txt     |  2 +-
 ui/views/guestbooks.templ     | 19 ++++++++
 ui/views/guestbooks_templ.go  | 84 +++++++++++++++++++++++++++++++++++
 ui/views/guestbooks_templ.txt |  8 +++-
 10 files changed, 151 insertions(+), 8 deletions(-)
diff --git a/cmd/web/handlers_guestbook.go b/cmd/web/handlers_guestbook.go
index e619ff9..b7adbeb 100644
--- a/cmd/web/handlers_guestbook.go
+++ b/cmd/web/handlers_guestbook.go
@@ -237,3 +237,11 @@ func (app *application) deleteGuestbookComment(w http.ResponseWriter, r *http.Re
 		app.serverError(w, r, err)
 	}
 }
+
+func (app *application) getAllGuestbooks(w http.ResponseWriter, r *http.Request) {
+	websites, err := app.websites.GetAll()
+	if err != nil {
+		app.serverError(w, r, err)
+	}
+	views.AllGuestbooksView(app.newCommonData(r), websites).Render(r.Context(), w)
+}
diff --git a/cmd/web/handlers_website.go b/cmd/web/handlers_website.go
index 833e0b2..8cb45b8 100644
--- a/cmd/web/handlers_website.go
+++ b/cmd/web/handlers_website.go
@@ -82,7 +82,7 @@ func (app *application) getWebsiteDashboard(w http.ResponseWriter, r *http.Reque
 func (app *application) getWebsiteList(w http.ResponseWriter, r *http.Request) {
 
 	userId := app.sessionManager.GetInt64(r.Context(), "authenticatedUserId")
-	websites, err := app.websites.GetAll(userId)
+	websites, err := app.websites.GetAllUser(userId)
 	if err != nil {
 		app.serverError(w, r, err)
 		return
diff --git a/cmd/web/routes.go b/cmd/web/routes.go
index 263d651..6bea60f 100644
--- a/cmd/web/routes.go
+++ b/cmd/web/routes.go
@@ -30,6 +30,7 @@ func (app *application) routes() http.Handler {
 	mux.Handle("POST /users/logout", protected.ThenFunc(app.postUserLogout))
 	mux.Handle("GET /users/settings", protected.ThenFunc(app.notImplemented))
 	mux.Handle("GET /users/privacy", protected.ThenFunc(app.notImplemented))
+	mux.Handle("GET /guestbooks", protected.ThenFunc(app.getAllGuestbooks))
 
 	mux.Handle("GET /websites", protected.ThenFunc(app.getWebsiteList))
 	mux.Handle("GET /websites/create", protected.ThenFunc(app.getWebsiteCreate))
diff --git a/internal/models/website.go b/internal/models/website.go
index 54d023b..4142697 100644
--- a/internal/models/website.go
+++ b/internal/models/website.go
@@ -65,7 +65,7 @@ func (m *WebsiteModel) GetById(id int64) (Website, error) {
 	return w, nil
 }
 
-func (m *WebsiteModel) GetAll(userId int64) ([]Website, error) {
+func (m *WebsiteModel) GetAllUser(userId int64) ([]Website, error) {
 	stmt := `SELECT w.Id, w.ShortId, w.Name, w.SiteUrl, w.AuthorName, w.UserId, w.Created, 
 	g.Id, g.ShortId, g.Created, g.IsActive
 	FROM websites AS w INNER JOIN guestbooks AS g ON w.Id = g.WebsiteId
@@ -89,3 +89,27 @@ func (m *WebsiteModel) GetAll(userId int64) ([]Website, error) {
 	}
 	return websites, nil
 }
+
+func (m *WebsiteModel) GetAll() ([]Website, error) {
+	stmt := `SELECT w.Id, w.ShortId, w.Name, w.SiteUrl, w.AuthorName, w.UserId, w.Created, 
+	g.Id, g.ShortId, g.Created, g.IsActive
+	FROM websites AS w INNER JOIN guestbooks AS g ON w.Id = g.WebsiteId`
+	rows, err := m.DB.Query(stmt)
+	if err != nil {
+		return nil, err
+	}
+	var websites []Website
+	for rows.Next() {
+		var w Website
+		err := rows.Scan(&w.ID, &w.ShortId, &w.Name, &w.SiteUrl, &w.AuthorName, &w.UserId, &w.Created,
+			&w.Guestbook.ID, &w.Guestbook.ShortId, &w.Guestbook.Created, &w.Guestbook.IsActive)
+		if err != nil {
+			return nil, err
+		}
+		websites = append(websites, w)
+	}
+	if err = rows.Err(); err != nil {
+		return nil, err
+	}
+	return websites, nil
+}
diff --git a/ui/views/common.templ b/ui/views/common.templ
index 3f081a3..7d5d911 100644
--- a/ui/views/common.templ
+++ b/ui/views/common.templ
@@ -36,6 +36,7 @@ templ topNav(data CommonData) {
         
         
 }
+
+templ AllGuestbooksView(data CommonData, websites []models.Website) {
+    @base("All Guestbooks", data) {
+        
+            
All Guestbooks
+            
+                This page exists only for testing the service.
+            
+            
+                for _, w := range websites {
+                    - 
+                        {{  gbUrl := fmt.Sprintf("/websites/%s/guestbook", shortIdToSlug(w.ShortId))}}
+                        { w.Name }
+                    
 
+                }
+            
+        
 
+    }
+}
\ No newline at end of file
diff --git a/ui/views/guestbooks_templ.go b/ui/views/guestbooks_templ.go
index 4d9eaab..ddb0a1d 100644
--- a/ui/views/guestbooks_templ.go
+++ b/ui/views/guestbooks_templ.go
@@ -752,4 +752,88 @@ func GuestbookDashboardCommentView(data CommonData, w models.Website, c models.G
 	})
 }
 
+func AllGuestbooksView(data CommonData, websites []models.Website) templ.Component {
+	return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
+		templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
+		if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
+			return templ_7745c5c3_CtxErr
+		}
+		templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
+		if !templ_7745c5c3_IsBuffer {
+			defer func() {
+				templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
+				if templ_7745c5c3_Err == nil {
+					templ_7745c5c3_Err = templ_7745c5c3_BufErr
+				}
+			}()
+		}
+		ctx = templ.InitializeContext(ctx)
+		templ_7745c5c3_Var36 := templ.GetChildren(ctx)
+		if templ_7745c5c3_Var36 == nil {
+			templ_7745c5c3_Var36 = templ.NopComponent
+		}
+		ctx = templ.ClearChildren(ctx)
+		templ_7745c5c3_Var37 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
+			templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
+			templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
+			if !templ_7745c5c3_IsBuffer {
+				defer func() {
+					templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
+					if templ_7745c5c3_Err == nil {
+						templ_7745c5c3_Err = templ_7745c5c3_BufErr
+					}
+				}()
+			}
+			ctx = templ.InitializeContext(ctx)
+			templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 56, "All Guestbooks
This page exists only for testing the service.
")
+			if templ_7745c5c3_Err != nil {
+				return templ_7745c5c3_Err
+			}
+			for _, w := range websites {
+				templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 57, "- ")
+				if templ_7745c5c3_Err != nil {
+					return templ_7745c5c3_Err
+				}
+				gbUrl := fmt.Sprintf("/websites/%s/guestbook", shortIdToSlug(w.ShortId))
+				templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 58, "")
+				if templ_7745c5c3_Err != nil {
+					return templ_7745c5c3_Err
+				}
+				var templ_7745c5c3_Var39 string
+				templ_7745c5c3_Var39, templ_7745c5c3_Err = templ.JoinStringErrs(w.Name)
+				if templ_7745c5c3_Err != nil {
+					return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/guestbooks.templ`, Line: 173, Col: 77}
+				}
+				_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var39))
+				if templ_7745c5c3_Err != nil {
+					return templ_7745c5c3_Err
+				}
+				templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 60, "
 ")
+				if templ_7745c5c3_Err != nil {
+					return templ_7745c5c3_Err
+				}
+			}
+			templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 61, "
 ")
+			if templ_7745c5c3_Err != nil {
+				return templ_7745c5c3_Err
+			}
+			return nil
+		})
+		templ_7745c5c3_Err = base("All Guestbooks", data).Render(templ.WithChildren(ctx, templ_7745c5c3_Var37), templ_7745c5c3_Buffer)
+		if templ_7745c5c3_Err != nil {
+			return templ_7745c5c3_Err
+		}
+		return nil
+	})
+}
+
 var _ = templruntime.GeneratedTemplate
diff --git a/ui/views/guestbooks_templ.txt b/ui/views/guestbooks_templ.txt
index 1302bef..9830fd1 100644
--- a/ui/views/guestbooks_templ.txt
+++ b/ui/views/guestbooks_templ.txt
@@ -52,4 +52,10 @@ Publish
 Hide
 
 
-
\ No newline at end of file
+
+All Guestbooks
This page exists only for testing the service.
 
\ No newline at end of file