From ad1c53a6110ecd00cc317dfbd605df7dffcd06e9 Mon Sep 17 00:00:00 2001 From: Teajey <21069848+Teajey@users.noreply.github.com> Date: Tue, 7 May 2024 19:43:43 +1200 Subject: [PATCH] feat: fetch foreign user by reference --- auth/auth.go | 2 +- auth/login.go | 2 +- auth/register.go | 2 +- context/foreign_wishlist.go | 8 ++++---- context/home.go | 5 +++-- db/user.go | 26 +++++++++++++++++++------- main.go | 2 +- templates/home.gotmpl | 8 +++++++- 8 files changed, 37 insertions(+), 18 deletions(-) diff --git a/auth/auth.go b/auth/auth.go index d41b5ab..d28c1c7 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -33,7 +33,7 @@ func (auth *AuthMiddleware) ExpectUser(r *http.Request) *db.User { log.Fatalln("Failed to get username") } - user, err := db.GetUser(username) + user, err := db.GetUserByName(username) if err != nil { log.Fatalf("Failed to get user: %s\n", err) } diff --git a/auth/login.go b/auth/login.go index 3c10749..dc02f63 100644 --- a/auth/login.go +++ b/auth/login.go @@ -18,7 +18,7 @@ func (auth *AuthMiddleware) LoginPost(w http.ResponseWriter, r *http.Request) { username := r.Form.Get("username") password := r.Form.Get("password") - user, err := db.GetUser(username) + user, err := db.GetUserByName(username) if err != nil { time.Sleep(time.Second) http.Error(w, "Username or password invalid", http.StatusUnauthorized) diff --git a/auth/register.go b/auth/register.go index b745fc7..1f633a3 100644 --- a/auth/register.go +++ b/auth/register.go @@ -18,7 +18,7 @@ func (auth *AuthMiddleware) RegisterPost(w http.ResponseWriter, r *http.Request) newPassword := r.Form.Get("newPassword") confirmPassword := r.Form.Get("confirmPassword") - existingUser, _ := db.GetUser(username) + existingUser, _ := db.GetUserByName(username) if existingUser != nil { http.Error(w, "Username is taken", http.StatusBadRequest) return diff --git a/context/foreign_wishlist.go b/context/foreign_wishlist.go index 6ad508b..604911a 100644 --- a/context/foreign_wishlist.go +++ b/context/foreign_wishlist.go @@ -13,13 +13,13 @@ type ForeignWishlistProps struct { } func (ctx *Context) ViewForeignWishlist(w http.ResponseWriter, r *http.Request) { - otherUsername := r.PathValue("username") + userReference := r.PathValue("userReference") user := ctx.Auth.ExpectUser(r) - if user.Name == otherUsername { + if user.Reference == userReference { http.Error(w, "You can't view your own list, silly ;)", http.StatusForbidden) return } - otherUser, err := db.GetUser(otherUsername) + otherUser, err := db.GetUserByReference(userReference) if err != nil { log.Printf("An error occurred while fetching a user: %s\n", err) http.Error(w, "An error occurred while fetching this user :(", http.StatusInternalServerError) @@ -34,6 +34,6 @@ func (ctx *Context) ViewForeignWishlist(w http.ResponseWriter, r *http.Request) http.Error(w, "An error occurred while fetching this user's wishlist :(", http.StatusInternalServerError) return } - p := ForeignWishlistProps{Username: otherUsername, Gifts: gifts} + p := ForeignWishlistProps{Username: otherUser.Name, Gifts: gifts} templates.Execute(w, "foreign_wishlist.gotmpl", p) } diff --git a/context/home.go b/context/home.go index ac3dd07..f9c1101 100644 --- a/context/home.go +++ b/context/home.go @@ -8,7 +8,8 @@ import ( ) type HomeProps struct { - Gifts []db.Gift + Gifts []db.Gift + Reference string } func (ctx *Context) Home(w http.ResponseWriter, r *http.Request) { @@ -18,6 +19,6 @@ func (ctx *Context) Home(w http.ResponseWriter, r *http.Request) { http.Error(w, "An error occurred while fetching your wishlist :(", http.StatusInternalServerError) return } - p := HomeProps{Gifts: gifts} + p := HomeProps{Gifts: gifts, Reference: user.Reference} templates.Execute(w, "home.gotmpl", p) } diff --git a/db/user.go b/db/user.go index 9479285..cd9b0aa 100644 --- a/db/user.go +++ b/db/user.go @@ -8,8 +8,9 @@ import ( ) type User struct { - Id string - Name string + Id string + Name string + Reference string } type Gift struct { @@ -17,23 +18,34 @@ type Gift struct { Name string } -func GetUser(username string) (*User, error) { - stmt := "SELECT user.id, user.name FROM user WHERE user.name = ?" +func queryForUser(query string, args ...any) (*User, error) { var id string var name string - err := database.QueryRow(stmt, username).Scan(&id, &name) + var reference string + err := database.QueryRow(query, args...).Scan(&id, &name, &reference) if err == sql.ErrNoRows { return nil, nil } else if err != nil { return nil, err } user := User{ - Id: id, - Name: name, + Id: id, + Name: name, + Reference: reference, } return &user, nil } +func GetUserByName(username string) (*User, error) { + stmt := "SELECT user.id, user.name, user.reference FROM user WHERE user.name = ?" + return queryForUser(stmt, username) +} + +func GetUserByReference(reference string) (*User, error) { + stmt := "SELECT user.id, user.name, user.reference FROM user WHERE user.reference = ?" + return queryForUser(stmt, reference) +} + func CreateUser(username string, passHash []byte) (*User, error) { stmt := "INSERT INTO user (name, motto, reference, password_hash) VALUES (?, '', ?, ?)" reference, err := uuid.NewRandom() diff --git a/main.go b/main.go index c912f39..e28f842 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,7 @@ func main() { publicMux.HandleFunc("POST /", authMiddleware.LoginPost) protectedMux.HandleFunc("GET /", ctx.Home) - protectedMux.HandleFunc("GET /{username}", ctx.ViewForeignWishlist) + protectedMux.HandleFunc("GET /{userReference}", ctx.ViewForeignWishlist) protectedMux.HandleFunc("POST /wishlist/add", ctx.WishlistAdd) protectedMux.HandleFunc("POST /wishlist/delete", ctx.WishlistDelete) protectedMux.HandleFunc("POST /logout", authMiddleware.LogoutPost) diff --git a/templates/home.gotmpl b/templates/home.gotmpl index 67bdc4d..1c7293b 100644 --- a/templates/home.gotmpl +++ b/templates/home.gotmpl @@ -3,6 +3,12 @@

Lishwist

+
+
User reference:
+
+
{{.Reference}}
+
+

Your list