diff --git a/internal/models/guestbook.go b/internal/models/guestbook.go index 618bdd8..133330a 100644 --- a/internal/models/guestbook.go +++ b/internal/models/guestbook.go @@ -2,73 +2,68 @@ package models import ( "database/sql" - "strconv" "time" ) type Guestbook struct { - ID int64 - ShortId uint64 - SiteUrl string - UserId int64 - Created time.Time - IsDeleted bool - IsActive bool -} - -func (gb Guestbook) Slug() string { - return strconv.FormatUint(gb.ShortId, 36) + ID int64 + ShortId uint64 + SiteUrl string + UserId int64 + Created time.Time + IsDeleted bool + IsActive bool } type GuestbookModel struct { - DB *sql.DB + DB *sql.DB } func (m *GuestbookModel) Insert(shortId uint64, siteUrl string, userId int64) (int64, error) { - stmt := `INSERT INTO guestbooks (ShortId, SiteUrl, UserId, Created, IsDeleted, IsActive) + stmt := `INSERT INTO guestbooks (ShortId, SiteUrl, UserId, Created, IsDeleted, IsActive) VALUES(?, ?, ?, ?, FALSE, TRUE)` - result, err := m.DB.Exec(stmt, shortId, siteUrl, userId, time.Now().UTC()) - if err != nil { - return -1, err - } - id, err := result.LastInsertId() - if err != nil { - return -1, err - } - return id, nil + result, err := m.DB.Exec(stmt, shortId, siteUrl, userId, time.Now().UTC()) + if err != nil { + return -1, err + } + id, err := result.LastInsertId() + if err != nil { + return -1, err + } + return id, nil } func (m *GuestbookModel) Get(shortId uint64) (Guestbook, error) { - stmt := `SELECT Id, ShortId, SiteUrl, UserId, Created, IsDeleted, IsActive FROM guestbooks + stmt := `SELECT Id, ShortId, SiteUrl, UserId, Created, IsDeleted, IsActive FROM guestbooks WHERE ShortId = ?` - row := m.DB.QueryRow(stmt, shortId) - var g Guestbook - err := row.Scan(&g.ID, &g.ShortId, &g.SiteUrl, &g.UserId, &g.Created, &g.IsDeleted, &g.IsActive) - if err != nil { - return Guestbook{}, err - } - - return g, nil + row := m.DB.QueryRow(stmt, shortId) + var g Guestbook + err := row.Scan(&g.ID, &g.ShortId, &g.SiteUrl, &g.UserId, &g.Created, &g.IsDeleted, &g.IsActive) + if err != nil { + return Guestbook{}, err + } + + return g, nil } func (m *GuestbookModel) GetAll(userId int64) ([]Guestbook, error) { - stmt := `SELECT Id, ShortId, SiteUrl, UserId, Created, IsDeleted, IsActive FROM guestbooks + stmt := `SELECT Id, ShortId, SiteUrl, UserId, Created, IsDeleted, IsActive FROM guestbooks WHERE UserId = ?` - rows, err := m.DB.Query(stmt, userId) - if err != nil { - return nil, err - } - var guestbooks []Guestbook - for rows.Next() { - var g Guestbook - err = rows.Scan(&g.ID, &g.ShortId, &g.SiteUrl, &g.UserId, &g.Created, &g.IsDeleted, &g.IsActive) - if err != nil { - return nil, err - } - guestbooks = append(guestbooks, g) - } - if err = rows.Err(); err != nil { - return nil, err - } - return guestbooks, nil + rows, err := m.DB.Query(stmt, userId) + if err != nil { + return nil, err + } + var guestbooks []Guestbook + for rows.Next() { + var g Guestbook + err = rows.Scan(&g.ID, &g.ShortId, &g.SiteUrl, &g.UserId, &g.Created, &g.IsDeleted, &g.IsActive) + if err != nil { + return nil, err + } + guestbooks = append(guestbooks, g) + } + if err = rows.Err(); err != nil { + return nil, err + } + return guestbooks, nil } diff --git a/ui/views/common.templ b/ui/views/common.templ index 94627fb..b89b543 100644 --- a/ui/views/common.templ +++ b/ui/views/common.templ @@ -12,6 +12,10 @@ type CommonData struct { IsHtmx bool } +func shortIdToSlug(shortId uint64) string { + return strconv.FormatUint(shortId, 36) +} + func slugToShortId(slug string) uint64 { id, _ := strconv.ParseUint(slug, 36, 64) return id diff --git a/ui/views/common_templ.go b/ui/views/common_templ.go index f1ab759..2729180 100644 --- a/ui/views/common_templ.go +++ b/ui/views/common_templ.go @@ -20,6 +20,10 @@ type CommonData struct { IsHtmx bool } +func shortIdToSlug(shortId uint64) string { + return strconv.FormatUint(shortId, 36) +} + func slugToShortId(slug string) uint64 { id, _ := strconv.ParseUint(slug, 36, 64) return id @@ -87,7 +91,7 @@ func topNav(data CommonData) templ.Component { var templ_7745c5c3_Var3 string templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(data.CurrentUser.Username) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/common.templ`, Line: 36, Col: 52} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/common.templ`, Line: 40, Col: 52} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) if templ_7745c5c3_Err != nil { @@ -100,7 +104,7 @@ func topNav(data CommonData) templ.Component { var templ_7745c5c3_Var4 string templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(data.CSRFToken) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/common.templ`, Line: 39, Col: 81} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/common.templ`, Line: 43, Col: 81} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) if templ_7745c5c3_Err != nil { @@ -181,7 +185,7 @@ func base(title string, data CommonData) templ.Component { var templ_7745c5c3_Var7 string templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(title) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/common.templ`, Line: 62, Col: 26} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/common.templ`, Line: 66, Col: 26} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7)) if templ_7745c5c3_Err != nil { @@ -211,7 +215,7 @@ func base(title string, data CommonData) templ.Component { var templ_7745c5c3_Var8 string templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(data.Flash) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/common.templ`, Line: 73, Col: 51} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `ui/views/common.templ`, Line: 77, Col: 51} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8)) if templ_7745c5c3_Err != nil { diff --git a/ui/views/guestbooks.templ b/ui/views/guestbooks.templ index ced6047..411ecf4 100644 --- a/ui/views/guestbooks.templ +++ b/ui/views/guestbooks.templ @@ -5,7 +5,7 @@ import "git.32bit.cafe/32bitcafe/guestbook/internal/models" import "git.32bit.cafe/32bitcafe/guestbook/internal/forms" func gbUrl(gb models.Guestbook) string { - return fmt.Sprintf("/guestbooks/%s", gb.Slug()) + return fmt.Sprintf("/guestbooks/%s", shortIdToSlug(gb.ShortId)) } templ gbCreateForm(csrf_token string) { @@ -130,7 +130,7 @@ templ GuestbookDashboardCommentsView(title string, data CommonData, guestbook mo } templ commentForm(data CommonData, gb models.Guestbook, form forms.CommentCreateForm) { - {{ postUrl := fmt.Sprintf("/guestbooks/%s/comments/create", gb.Slug()) }} + {{ postUrl := fmt.Sprintf("/guestbooks/%s/comments/create", shortIdToSlug(gb.ShortId)) }}