updated to use bun and updated hws modules.

This commit is contained in:
2026-01-11 23:39:10 +11:00
parent 6e03c98ae8
commit 1eedbc5220
33 changed files with 984 additions and 375 deletions

View File

@@ -2,19 +2,20 @@ package handler
import (
"context"
"database/sql"
"net/http"
"time"
"git.haelnorr.com/h/golib/hws"
"git.haelnorr.com/h/golib/hwsauth"
"projectreshoot/internal/models"
"projectreshoot/internal/view/component/account"
"projectreshoot/internal/view/page"
"git.haelnorr.com/h/golib/hws"
"git.haelnorr.com/h/golib/hwsauth"
"git.haelnorr.com/h/golib/cookies"
"github.com/pkg/errors"
"github.com/uptrace/bun"
)
// Renders the account page on the 'General' subpage
@@ -46,8 +47,8 @@ func AccountSubpage() http.Handler {
// Handles a request to change the users username
func ChangeUsername(
server *hws.Server,
auth *hwsauth.Authenticator[*models.User],
conn *sql.DB,
auth *hwsauth.Authenticator[*models.UserBun, bun.Tx],
db *bun.DB,
) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
@@ -55,7 +56,7 @@ func ChangeUsername(
defer cancel()
// Start the transaction
tx, err := conn.BeginTx(ctx, nil)
tx, err := db.BeginTx(ctx, nil)
if err != nil {
err := server.ThrowError(w, r, hws.HWSError{
StatusCode: http.StatusServiceUnavailable,
@@ -69,7 +70,7 @@ func ChangeUsername(
}
r.ParseForm()
newUsername := r.FormValue("username")
unique, err := models.CheckUsernameUnique(tx, newUsername)
unique, err := models.IsUsernameUnique(ctx, tx, newUsername)
if err != nil {
tx.Rollback()
err := server.ThrowError(w, r, hws.HWSError{
@@ -89,7 +90,7 @@ func ChangeUsername(
return
}
user := auth.CurrentModel(r.Context())
err = user.ChangeUsername(tx, newUsername)
err = user.ChangeUsername(ctx, tx, newUsername)
if err != nil {
tx.Rollback()
err := server.ThrowError(w, r, hws.HWSError{
@@ -111,8 +112,8 @@ func ChangeUsername(
// Handles a request to change the users bio
func ChangeBio(
server *hws.Server,
auth *hwsauth.Authenticator[*models.User],
conn *sql.DB,
auth *hwsauth.Authenticator[*models.UserBun, bun.Tx],
db *bun.DB,
) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
@@ -120,7 +121,7 @@ func ChangeBio(
defer cancel()
// Start the transaction
tx, err := conn.BeginTx(ctx, nil)
tx, err := db.BeginTx(ctx, nil)
if err != nil {
err := server.ThrowError(w, r, hws.HWSError{
StatusCode: http.StatusServiceUnavailable,
@@ -142,7 +143,7 @@ func ChangeBio(
return
}
user := auth.CurrentModel(r.Context())
err = user.ChangeBio(tx, newBio)
err = user.ChangeBio(ctx, tx, newBio)
if err != nil {
tx.Rollback()
err := server.ThrowError(w, r, hws.HWSError{
@@ -178,8 +179,8 @@ func validateChangePassword(
// Handles a request to change the users password
func ChangePassword(
server *hws.Server,
auth *hwsauth.Authenticator[*models.User],
conn *sql.DB,
auth *hwsauth.Authenticator[*models.UserBun, bun.Tx],
db *bun.DB,
) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
@@ -187,7 +188,7 @@ func ChangePassword(
defer cancel()
// Start the transaction
tx, err := conn.BeginTx(ctx, nil)
tx, err := db.BeginTx(ctx, nil)
if err != nil {
err := server.ThrowError(w, r, hws.HWSError{
StatusCode: http.StatusServiceUnavailable,
@@ -206,7 +207,7 @@ func ChangePassword(
return
}
user := auth.CurrentModel(r.Context())
err = user.SetPassword(tx, newPass)
err = user.SetPassword(ctx, tx, newPass)
if err != nil {
tx.Rollback()
err := server.ThrowError(w, r, hws.HWSError{