slug and shortId conversion functions

This commit is contained in:
yequari 2025-03-15 10:57:22 -07:00
parent 5c8817aa2a
commit adaf6cf87d
5 changed files with 61 additions and 58 deletions

View File

@ -2,7 +2,6 @@ package models
import ( import (
"database/sql" "database/sql"
"strconv"
"time" "time"
) )
@ -16,10 +15,6 @@ type Guestbook struct {
IsActive bool IsActive bool
} }
func (gb Guestbook) Slug() string {
return strconv.FormatUint(gb.ShortId, 36)
}
type GuestbookModel struct { type GuestbookModel struct {
DB *sql.DB DB *sql.DB
} }

View File

@ -12,6 +12,10 @@ type CommonData struct {
IsHtmx bool IsHtmx bool
} }
func shortIdToSlug(shortId uint64) string {
return strconv.FormatUint(shortId, 36)
}
func slugToShortId(slug string) uint64 { func slugToShortId(slug string) uint64 {
id, _ := strconv.ParseUint(slug, 36, 64) id, _ := strconv.ParseUint(slug, 36, 64)
return id return id

View File

@ -20,6 +20,10 @@ type CommonData struct {
IsHtmx bool IsHtmx bool
} }
func shortIdToSlug(shortId uint64) string {
return strconv.FormatUint(shortId, 36)
}
func slugToShortId(slug string) uint64 { func slugToShortId(slug string) uint64 {
id, _ := strconv.ParseUint(slug, 36, 64) id, _ := strconv.ParseUint(slug, 36, 64)
return id return id
@ -87,7 +91,7 @@ func topNav(data CommonData) templ.Component {
var templ_7745c5c3_Var3 string var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(data.CurrentUser.Username) templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(data.CurrentUser.Username)
if templ_7745c5c3_Err != nil { 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)) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
@ -100,7 +104,7 @@ func topNav(data CommonData) templ.Component {
var templ_7745c5c3_Var4 string var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(data.CSRFToken) templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(data.CSRFToken)
if templ_7745c5c3_Err != nil { 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)) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
@ -181,7 +185,7 @@ func base(title string, data CommonData) templ.Component {
var templ_7745c5c3_Var7 string var templ_7745c5c3_Var7 string
templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(title) templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(title)
if templ_7745c5c3_Err != nil { 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)) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7))
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
@ -211,7 +215,7 @@ func base(title string, data CommonData) templ.Component {
var templ_7745c5c3_Var8 string var templ_7745c5c3_Var8 string
templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(data.Flash) templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(data.Flash)
if templ_7745c5c3_Err != nil { 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)) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8))
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {

View File

@ -5,7 +5,7 @@ import "git.32bit.cafe/32bitcafe/guestbook/internal/models"
import "git.32bit.cafe/32bitcafe/guestbook/internal/forms" import "git.32bit.cafe/32bitcafe/guestbook/internal/forms"
func gbUrl(gb models.Guestbook) string { 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) { 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) { 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)) }}
<form action={ templ.URL(postUrl) } method="post"> <form action={ templ.URL(postUrl) } method="post">
<input type="hidden" name="csrf_token" value={data.CSRFToken}> <input type="hidden" name="csrf_token" value={data.CSRFToken}>
<div> <div>

View File

@ -13,7 +13,7 @@ import "git.32bit.cafe/32bitcafe/guestbook/internal/models"
import "git.32bit.cafe/32bitcafe/guestbook/internal/forms" import "git.32bit.cafe/32bitcafe/guestbook/internal/forms"
func gbUrl(gb models.Guestbook) string { func gbUrl(gb models.Guestbook) string {
return fmt.Sprintf("/guestbooks/%s", gb.Slug()) return fmt.Sprintf("/guestbooks/%s", shortIdToSlug(gb.ShortId))
} }
func gbCreateForm(csrf_token string) templ.Component { func gbCreateForm(csrf_token string) templ.Component {
@ -575,7 +575,7 @@ func commentForm(data CommonData, gb models.Guestbook, form forms.CommentCreateF
templ_7745c5c3_Var28 = templ.NopComponent templ_7745c5c3_Var28 = templ.NopComponent
} }
ctx = templ.ClearChildren(ctx) ctx = templ.ClearChildren(ctx)
postUrl := fmt.Sprintf("/guestbooks/%s/comments/create", gb.Slug()) postUrl := fmt.Sprintf("/guestbooks/%s/comments/create", shortIdToSlug(gb.ShortId))
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 36, "<form action=\"") templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 36, "<form action=\"")
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err