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) {
if data.IsAuthenticated { + All Guestbooks | My Websites | Settings |
diff --git a/ui/views/common_templ.go b/ui/views/common_templ.go index 6ea4eaa..7a0ee24 100644 --- a/ui/views/common_templ.go +++ b/ui/views/common_templ.go @@ -103,14 +103,14 @@ func topNav(data CommonData) templ.Component { return templ_7745c5c3_Err } if data.IsAuthenticated { - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "My Websites | Settings | All Guestbooks | My Websites | Settings |
Welcome,
-My Websites | Settings | All Guestbooks | My Websites | Settings | Create an Account | Login
diff --git a/ui/views/guestbooks.templ b/ui/views/guestbooks.templ index 6afe799..e649c7c 100644 --- a/ui/views/guestbooks.templ +++ b/ui/views/guestbooks.templ @@ -158,3 +158,22 @@ templ GuestbookDashboardCommentView(data CommonData, w models.Website, c models.

} + +templ AllGuestbooksView(data CommonData, websites []models.Website) { + @base("All Guestbooks", data) { +
+

All Guestbooks

+

+ This page exists only for testing the service. +

+ +
+ } +} \ 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 + } + 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