check user owns guestbook
This commit is contained in:
		
							parent
							
								
									6c18752230
								
							
						
					
					
						commit
						11c0815676
					
				@ -1,213 +1,216 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        "git.32bit.cafe/32bitcafe/guestbook/internal/forms"
 | 
						"git.32bit.cafe/32bitcafe/guestbook/internal/forms"
 | 
				
			||||||
	"git.32bit.cafe/32bitcafe/guestbook/internal/models"
 | 
						"git.32bit.cafe/32bitcafe/guestbook/internal/models"
 | 
				
			||||||
	"git.32bit.cafe/32bitcafe/guestbook/internal/validator"
 | 
						"git.32bit.cafe/32bitcafe/guestbook/internal/validator"
 | 
				
			||||||
	"git.32bit.cafe/32bitcafe/guestbook/ui/views"
 | 
						"git.32bit.cafe/32bitcafe/guestbook/ui/views"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (app *application) getGuestbookCreate(w http.ResponseWriter, r* http.Request) {
 | 
					func (app *application) getGuestbookCreate(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
    data := app.newCommonData(r)
 | 
						data := app.newCommonData(r)
 | 
				
			||||||
    views.GuestbookCreate("New Guestbook", data).Render(r.Context(), w)
 | 
						views.GuestbookCreate("New Guestbook", data).Render(r.Context(), w)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (app *application) postGuestbookCreate(w http.ResponseWriter, r* http.Request) {
 | 
					func (app *application) postGuestbookCreate(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
    userId := app.sessionManager.GetInt64(r.Context(), "authenticatedUserId")
 | 
						userId := app.sessionManager.GetInt64(r.Context(), "authenticatedUserId")
 | 
				
			||||||
    err := r.ParseForm()
 | 
						err := r.ParseForm()
 | 
				
			||||||
    if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
        app.serverError(w, r, err)
 | 
							app.serverError(w, r, err)
 | 
				
			||||||
        return
 | 
							return
 | 
				
			||||||
    }
 | 
						}
 | 
				
			||||||
    siteUrl := r.Form.Get("siteurl")
 | 
						siteUrl := r.Form.Get("siteurl")
 | 
				
			||||||
    shortId := app.createShortId()
 | 
						shortId := app.createShortId()
 | 
				
			||||||
    _, err = app.guestbooks.Insert(shortId, siteUrl, userId)
 | 
						_, err = app.guestbooks.Insert(shortId, siteUrl, userId)
 | 
				
			||||||
    if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
        app.serverError(w, r, err)
 | 
							app.serverError(w, r, err)
 | 
				
			||||||
        return
 | 
							return
 | 
				
			||||||
    }
 | 
						}
 | 
				
			||||||
    app.sessionManager.Put(r.Context(), "flash", "Guestbook successfully created!")
 | 
						app.sessionManager.Put(r.Context(), "flash", "Guestbook successfully created!")
 | 
				
			||||||
    if r.Header.Get("HX-Request") == "true" {
 | 
						if r.Header.Get("HX-Request") == "true" {
 | 
				
			||||||
        w.Header().Add("HX-Trigger", "newGuestbook")
 | 
							w.Header().Add("HX-Trigger", "newGuestbook")
 | 
				
			||||||
        data := app.newTemplateData(r)
 | 
							data := app.newTemplateData(r)
 | 
				
			||||||
        app.renderHTMX(w, r, http.StatusOK, "guestbookcreatebutton.part.html", data)
 | 
							app.renderHTMX(w, r, http.StatusOK, "guestbookcreatebutton.part.html", data)
 | 
				
			||||||
        return
 | 
							return
 | 
				
			||||||
    }
 | 
						}
 | 
				
			||||||
    http.Redirect(w, r, fmt.Sprintf("/guestbooks/%s", shortIdToSlug(shortId)), http.StatusSeeOther)
 | 
						http.Redirect(w, r, fmt.Sprintf("/guestbooks/%s", shortIdToSlug(shortId)), http.StatusSeeOther)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (app *application) getGuestbookList(w http.ResponseWriter, r *http.Request) {
 | 
					func (app *application) getGuestbookList(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
    userId := app.sessionManager.GetInt64(r.Context(), "authenticatedUserId")
 | 
						userId := app.sessionManager.GetInt64(r.Context(), "authenticatedUserId")
 | 
				
			||||||
    guestbooks, err := app.guestbooks.GetAll(userId)
 | 
						guestbooks, err := app.guestbooks.GetAll(userId)
 | 
				
			||||||
    if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
        app.serverError(w, r, err)
 | 
							app.serverError(w, r, err)
 | 
				
			||||||
        return
 | 
							return
 | 
				
			||||||
    }
 | 
						}
 | 
				
			||||||
    data := app.newCommonData(r)
 | 
						data := app.newCommonData(r)
 | 
				
			||||||
    views.GuestbookList("Guestbooks", data, guestbooks).Render(r.Context(), w)
 | 
						views.GuestbookList("Guestbooks", data, guestbooks).Render(r.Context(), w)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (app *application) getGuestbook(w http.ResponseWriter, r *http.Request) {
 | 
					func (app *application) getGuestbook(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
    slug := r.PathValue("id")
 | 
						slug := r.PathValue("id")
 | 
				
			||||||
    guestbook, err := app.guestbooks.Get(slugToShortId(slug))
 | 
						guestbook, err := app.guestbooks.Get(slugToShortId(slug))
 | 
				
			||||||
    if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
        if errors.Is(err, models.ErrNoRecord) {
 | 
							if errors.Is(err, models.ErrNoRecord) {
 | 
				
			||||||
            http.NotFound(w, r)
 | 
								http.NotFound(w, r)
 | 
				
			||||||
        } else {
 | 
							} else {
 | 
				
			||||||
            app.serverError(w, r, err)
 | 
								app.serverError(w, r, err)
 | 
				
			||||||
        }
 | 
							}
 | 
				
			||||||
        return
 | 
							return
 | 
				
			||||||
    }
 | 
						}
 | 
				
			||||||
    comments, err := app.guestbookComments.GetAll(guestbook.ID)
 | 
						comments, err := app.guestbookComments.GetAll(guestbook.ID)
 | 
				
			||||||
    if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
        app.serverError(w, r, err)
 | 
							app.serverError(w, r, err)
 | 
				
			||||||
        return
 | 
							return
 | 
				
			||||||
    }
 | 
						}
 | 
				
			||||||
    data := app.newCommonData(r)
 | 
						data := app.newCommonData(r)
 | 
				
			||||||
    views.GuestbookView("Guestbook", data, guestbook, comments, forms.CommentCreateForm{}).Render(r.Context(), w)
 | 
						views.GuestbookView("Guestbook", data, guestbook, comments, forms.CommentCreateForm{}).Render(r.Context(), w)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (app *application) getGuestbookDashboard(w http.ResponseWriter, r *http.Request) {
 | 
					func (app *application) getGuestbookDashboard(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
    slug := r.PathValue("id")
 | 
						slug := r.PathValue("id")
 | 
				
			||||||
    guestbook, err := app.guestbooks.Get(slugToShortId(slug))
 | 
						guestbook, err := app.guestbooks.Get(slugToShortId(slug))
 | 
				
			||||||
    if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
        if errors.Is(err, models.ErrNoRecord) {
 | 
							if errors.Is(err, models.ErrNoRecord) {
 | 
				
			||||||
            http.NotFound(w, r)
 | 
								http.NotFound(w, r)
 | 
				
			||||||
        } else {
 | 
							} else {
 | 
				
			||||||
            app.serverError(w, r, err)
 | 
								app.serverError(w, r, err)
 | 
				
			||||||
        }
 | 
							}
 | 
				
			||||||
        return
 | 
							return
 | 
				
			||||||
    }
 | 
						}
 | 
				
			||||||
    comments, err := app.guestbookComments.GetAll(guestbook.ID)
 | 
						user := app.getCurrentUser(r)
 | 
				
			||||||
    if err != nil {
 | 
						if user.ID != guestbook.UserId {
 | 
				
			||||||
        app.serverError(w, r, err)
 | 
							app.clientError(w, http.StatusUnauthorized)
 | 
				
			||||||
        return
 | 
						}
 | 
				
			||||||
    }
 | 
						comments, err := app.guestbookComments.GetAll(guestbook.ID)
 | 
				
			||||||
    data := app.newCommonData(r)
 | 
						if err != nil {
 | 
				
			||||||
    views.GuestbookDashboardView("Guestbook", data, guestbook, comments).Render(r.Context(), w)
 | 
							app.serverError(w, r, err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						data := app.newCommonData(r)
 | 
				
			||||||
 | 
						views.GuestbookDashboardView("Guestbook", data, guestbook, comments).Render(r.Context(), w)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (app *application) getGuestbookComments(w http.ResponseWriter, r *http.Request) {
 | 
					func (app *application) getGuestbookComments(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
    slug := r.PathValue("id")
 | 
						slug := r.PathValue("id")
 | 
				
			||||||
    guestbook, err := app.guestbooks.Get(slugToShortId(slug))
 | 
						guestbook, err := app.guestbooks.Get(slugToShortId(slug))
 | 
				
			||||||
    if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
        if errors.Is(err, models.ErrNoRecord) {
 | 
							if errors.Is(err, models.ErrNoRecord) {
 | 
				
			||||||
            http.NotFound(w, r)
 | 
								http.NotFound(w, r)
 | 
				
			||||||
        } else {
 | 
							} else {
 | 
				
			||||||
            app.serverError(w, r, err)
 | 
								app.serverError(w, r, err)
 | 
				
			||||||
        }
 | 
							}
 | 
				
			||||||
        return
 | 
							return
 | 
				
			||||||
    }
 | 
						}
 | 
				
			||||||
    comments, err := app.guestbookComments.GetAll(guestbook.ID)
 | 
						comments, err := app.guestbookComments.GetAll(guestbook.ID)
 | 
				
			||||||
    if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
        app.serverError(w, r, err)
 | 
							app.serverError(w, r, err)
 | 
				
			||||||
        return
 | 
							return
 | 
				
			||||||
    }
 | 
						}
 | 
				
			||||||
    data := app.newCommonData(r)
 | 
						data := app.newCommonData(r)
 | 
				
			||||||
    views.GuestbookDashboardCommentsView("Comments", data, guestbook, comments).Render(r.Context(), w)
 | 
						views.GuestbookDashboardCommentsView("Comments", data, guestbook, comments).Render(r.Context(), w)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (app *application) getGuestbookCommentCreate(w http.ResponseWriter, r *http.Request) {
 | 
					func (app *application) getGuestbookCommentCreate(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
    // TODO: This will be the embeddable form
 | 
						// TODO: This will be the embeddable form
 | 
				
			||||||
    slug := r.PathValue("id")
 | 
						slug := r.PathValue("id")
 | 
				
			||||||
    guestbook, err := app.guestbooks.Get(slugToShortId(slug))
 | 
						guestbook, err := app.guestbooks.Get(slugToShortId(slug))
 | 
				
			||||||
    if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
        if errors.Is(err, models.ErrNoRecord) {
 | 
							if errors.Is(err, models.ErrNoRecord) {
 | 
				
			||||||
            http.NotFound(w, r)
 | 
								http.NotFound(w, r)
 | 
				
			||||||
        } else {
 | 
							} else {
 | 
				
			||||||
            app.serverError(w, r, err)
 | 
								app.serverError(w, r, err)
 | 
				
			||||||
        }
 | 
							}
 | 
				
			||||||
        return
 | 
							return
 | 
				
			||||||
    }
 | 
						}
 | 
				
			||||||
    data := app.newTemplateData(r)
 | 
						data := app.newTemplateData(r)
 | 
				
			||||||
    data.Guestbook = guestbook
 | 
						data.Guestbook = guestbook
 | 
				
			||||||
    data.Form = forms.CommentCreateForm{}
 | 
						data.Form = forms.CommentCreateForm{}
 | 
				
			||||||
    app.render(w, r, http.StatusOK, "commentcreate.view.tmpl.html", data)
 | 
						app.render(w, r, http.StatusOK, "commentcreate.view.tmpl.html", data)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (app *application) postGuestbookCommentCreate(w http.ResponseWriter, r *http.Request) {
 | 
					func (app *application) postGuestbookCommentCreate(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
    guestbookSlug := r.PathValue("id")
 | 
						guestbookSlug := r.PathValue("id")
 | 
				
			||||||
    guestbook, err := app.guestbooks.Get(slugToShortId(guestbookSlug))
 | 
						guestbook, err := app.guestbooks.Get(slugToShortId(guestbookSlug))
 | 
				
			||||||
    if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
        if errors.Is(err, models.ErrNoRecord) {
 | 
							if errors.Is(err, models.ErrNoRecord) {
 | 
				
			||||||
            http.NotFound(w, r)
 | 
								http.NotFound(w, r)
 | 
				
			||||||
        } else {
 | 
							} else {
 | 
				
			||||||
            app.serverError(w, r, err)
 | 
								app.serverError(w, r, err)
 | 
				
			||||||
        }
 | 
							}
 | 
				
			||||||
        return
 | 
							return
 | 
				
			||||||
    }
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    var form forms.CommentCreateForm
 | 
						var form forms.CommentCreateForm
 | 
				
			||||||
    err = app.decodePostForm(r, &form)
 | 
						err = app.decodePostForm(r, &form)
 | 
				
			||||||
    if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
        app.clientError(w, http.StatusBadRequest)
 | 
							app.clientError(w, http.StatusBadRequest)
 | 
				
			||||||
        return
 | 
							return
 | 
				
			||||||
    }
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    form.CheckField(validator.NotBlank(form.AuthorName), "authorName", "This field cannot be blank")
 | 
						form.CheckField(validator.NotBlank(form.AuthorName), "authorName", "This field cannot be blank")
 | 
				
			||||||
    form.CheckField(validator.MaxChars(form.AuthorName, 256), "authorName", "This field cannot be more than 256 characters long")
 | 
						form.CheckField(validator.MaxChars(form.AuthorName, 256), "authorName", "This field cannot be more than 256 characters long")
 | 
				
			||||||
    form.CheckField(validator.NotBlank(form.AuthorEmail), "authorEmail", "This field cannot be blank")
 | 
						form.CheckField(validator.NotBlank(form.AuthorEmail), "authorEmail", "This field cannot be blank")
 | 
				
			||||||
    form.CheckField(validator.MaxChars(form.AuthorEmail, 256), "authorEmail", "This field cannot be more than 256 characters long")
 | 
						form.CheckField(validator.MaxChars(form.AuthorEmail, 256), "authorEmail", "This field cannot be more than 256 characters long")
 | 
				
			||||||
    form.CheckField(validator.NotBlank(form.AuthorSite), "authorSite", "This field cannot be blank")
 | 
						form.CheckField(validator.NotBlank(form.AuthorSite), "authorSite", "This field cannot be blank")
 | 
				
			||||||
    form.CheckField(validator.MaxChars(form.AuthorSite, 256), "authorSite", "This field cannot be more than 256 characters long")
 | 
						form.CheckField(validator.MaxChars(form.AuthorSite, 256), "authorSite", "This field cannot be more than 256 characters long")
 | 
				
			||||||
    form.CheckField(validator.NotBlank(form.Content), "content", "This field cannot be blank")
 | 
						form.CheckField(validator.NotBlank(form.Content), "content", "This field cannot be blank")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if !form.Valid() {
 | 
						if !form.Valid() {
 | 
				
			||||||
        data := app.newTemplateData(r)
 | 
							data := app.newTemplateData(r)
 | 
				
			||||||
        data.Guestbook = guestbook
 | 
							data.Guestbook = guestbook
 | 
				
			||||||
        data.Form = form
 | 
							data.Form = form
 | 
				
			||||||
        app.render(w, r, http.StatusUnprocessableEntity, "commentcreate.view.tmpl.html", data)
 | 
							app.render(w, r, http.StatusUnprocessableEntity, "commentcreate.view.tmpl.html", data)
 | 
				
			||||||
        return
 | 
							return
 | 
				
			||||||
    }
 | 
						}
 | 
				
			||||||
    
 | 
					
 | 
				
			||||||
    shortId := app.createShortId()
 | 
						shortId := app.createShortId()
 | 
				
			||||||
    _, err = app.guestbookComments.Insert(shortId, guestbook.ID, 0, form.AuthorName, form.AuthorEmail, form.AuthorSite, form.Content, "", true)
 | 
						_, err = app.guestbookComments.Insert(shortId, guestbook.ID, 0, form.AuthorName, form.AuthorEmail, form.AuthorSite, form.Content, "", true)
 | 
				
			||||||
    if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
        app.serverError(w, r, err)
 | 
							app.serverError(w, r, err)
 | 
				
			||||||
        return
 | 
							return
 | 
				
			||||||
    }
 | 
						}
 | 
				
			||||||
    // app.sessionManager.Put(r.Context(), "flash", "Comment successfully posted!")
 | 
						// app.sessionManager.Put(r.Context(), "flash", "Comment successfully posted!")
 | 
				
			||||||
    http.Redirect(w, r, fmt.Sprintf("/guestbooks/%s", guestbookSlug), http.StatusSeeOther)
 | 
						http.Redirect(w, r, fmt.Sprintf("/guestbooks/%s", guestbookSlug), http.StatusSeeOther)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (app *application) updateGuestbookComment(w http.ResponseWriter, r *http.Request) {
 | 
					func (app *application) updateGuestbookComment(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (app *application) deleteGuestbookComment(w http.ResponseWriter, r *http.Request) {
 | 
					func (app *application) deleteGuestbookComment(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
    // slug := r.PathValue("id")
 | 
						// slug := r.PathValue("id")
 | 
				
			||||||
    // shortId := slugToShortId(slug)
 | 
						// shortId := slugToShortId(slug)
 | 
				
			||||||
    // app.guestbookComments.Delete(shortId)
 | 
						// app.guestbookComments.Delete(shortId)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (app *application) getCommentQueue(w http.ResponseWriter, r *http.Request) {
 | 
					func (app *application) getCommentQueue(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
    guestbookSlug := r.PathValue("id")
 | 
						guestbookSlug := r.PathValue("id")
 | 
				
			||||||
    guestbook, err := app.guestbooks.Get(slugToShortId(guestbookSlug))
 | 
						guestbook, err := app.guestbooks.Get(slugToShortId(guestbookSlug))
 | 
				
			||||||
    if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
        if errors.Is(err, models.ErrNoRecord) {
 | 
							if errors.Is(err, models.ErrNoRecord) {
 | 
				
			||||||
            http.NotFound(w, r)
 | 
								http.NotFound(w, r)
 | 
				
			||||||
        } else {
 | 
							} else {
 | 
				
			||||||
            app.serverError(w, r, err)
 | 
								app.serverError(w, r, err)
 | 
				
			||||||
        }
 | 
							}
 | 
				
			||||||
        return
 | 
							return
 | 
				
			||||||
    }
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    comments, err := app.guestbookComments.GetQueue(guestbook.ID)
 | 
						comments, err := app.guestbookComments.GetQueue(guestbook.ID)
 | 
				
			||||||
    if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
        if errors.Is(err, models.ErrNoRecord) {
 | 
							if errors.Is(err, models.ErrNoRecord) {
 | 
				
			||||||
            http.NotFound(w, r)
 | 
								http.NotFound(w, r)
 | 
				
			||||||
        } else {
 | 
							} else {
 | 
				
			||||||
            app.serverError(w, r, err)
 | 
								app.serverError(w, r, err)
 | 
				
			||||||
        }
 | 
							}
 | 
				
			||||||
        return
 | 
							return
 | 
				
			||||||
    }
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    data := app.newCommonData(r)
 | 
						data := app.newCommonData(r)
 | 
				
			||||||
    views.GuestbookDashboardCommentsView("Message Queue", data, guestbook, comments).Render(r.Context(), w)
 | 
						views.GuestbookDashboardCommentsView("Message Queue", data, guestbook, comments).Render(r.Context(), w)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (app *application) putHideGuestbookComment(w http.ResponseWriter, r *http.Request) {
 | 
					func (app *application) putHideGuestbookComment(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
				
			|||||||
@ -11,7 +11,7 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type User struct {
 | 
					type User struct {
 | 
				
			||||||
	ID             int
 | 
						ID             int64
 | 
				
			||||||
	ShortId        uint64
 | 
						ShortId        uint64
 | 
				
			||||||
	Username       string
 | 
						Username       string
 | 
				
			||||||
	Email          string
 | 
						Email          string
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user