parent
20bc9fe17a
commit
4f68621bad
9 changed files with 126 additions and 42 deletions
|
@ -23,45 +23,98 @@ type AccountsIdPage struct {
|
|||
|
||||
var accountsIdTemplates = template.Must(template.ParseFS(htmlFS, "html/base.html", "html/accountsId.html"))
|
||||
|
||||
func prepareAccountsIdPage(db *database.DB, w http.ResponseWriter, r *http.Request) *AccountsIdPage {
|
||||
var accountId uuid.UUID
|
||||
if err := accountId.Parse(r.PathValue("id")); err != nil {
|
||||
errorResponse(w, r, http.StatusBadRequest, err)
|
||||
return nil
|
||||
}
|
||||
account, err := db.LoadAccountById(&accountId)
|
||||
if err != nil {
|
||||
errorResponse(w, r, http.StatusInternalServerError, err)
|
||||
return nil
|
||||
}
|
||||
if account == nil {
|
||||
errorResponse(w, r, http.StatusNotFound, fmt.Errorf("The account Id could not be found."))
|
||||
return nil
|
||||
}
|
||||
statePaths, err := db.LoadStatePaths()
|
||||
if err != nil {
|
||||
errorResponse(w, r, http.StatusInternalServerError, err)
|
||||
return nil
|
||||
}
|
||||
versions, err := db.LoadVersionsByAccount(account)
|
||||
if err != nil {
|
||||
errorResponse(w, r, http.StatusInternalServerError, err)
|
||||
return nil
|
||||
}
|
||||
isAdmin := ""
|
||||
if account.IsAdmin {
|
||||
isAdmin = "1"
|
||||
}
|
||||
return &AccountsIdPage{
|
||||
Account: account,
|
||||
IsAdmin: isAdmin,
|
||||
Page: makePage(r, &Page{
|
||||
Section: "accounts",
|
||||
Title: account.Username,
|
||||
}),
|
||||
StatePaths: statePaths,
|
||||
Versions: versions,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func handleAccountsIdGET(db *database.DB) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var accountId uuid.UUID
|
||||
if err := accountId.Parse(r.PathValue("id")); err != nil {
|
||||
page := prepareAccountsIdPage(db, w, r)
|
||||
if page != nil {
|
||||
render(w, accountsIdTemplates, http.StatusOK, page)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func handleAccountsIdPOST(db *database.DB) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
errorResponse(w, r, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
account, err := db.LoadAccountById(&accountId)
|
||||
if err != nil {
|
||||
errorResponse(w, r, http.StatusInternalServerError, err)
|
||||
if !verifyCSRFToken(w, r) {
|
||||
return
|
||||
}
|
||||
if account == nil {
|
||||
errorResponse(w, r, http.StatusNotFound, fmt.Errorf("The account Id could not be found."))
|
||||
page := prepareAccountsIdPage(db, w, r)
|
||||
if page == nil {
|
||||
return
|
||||
}
|
||||
statePaths, err := db.LoadStatePaths()
|
||||
if err != nil {
|
||||
errorResponse(w, r, http.StatusInternalServerError, err)
|
||||
action := r.FormValue("action")
|
||||
switch action {
|
||||
case "delete":
|
||||
errorResponse(w, r, http.StatusNotImplemented, nil)
|
||||
return
|
||||
case "edit":
|
||||
errorResponse(w, r, http.StatusNotImplemented, nil)
|
||||
return
|
||||
case "reset-password":
|
||||
if err := page.Account.ResetPassword(); err != nil {
|
||||
errorResponse(w, r, http.StatusNotImplemented,
|
||||
fmt.Errorf("failed to reset password: %w", err))
|
||||
return
|
||||
}
|
||||
if err := db.SaveAccount(page.Account); err != nil {
|
||||
errorResponse(w, r, http.StatusInternalServerError,
|
||||
fmt.Errorf("failed to save account: %w", err))
|
||||
return
|
||||
}
|
||||
if err := db.DeleteSessions(page.Account); err != nil {
|
||||
errorResponse(w, r, http.StatusInternalServerError,
|
||||
fmt.Errorf("failed to save account: %w", err))
|
||||
return
|
||||
}
|
||||
default:
|
||||
errorResponse(w, r, http.StatusBadRequest, nil)
|
||||
return
|
||||
}
|
||||
versions, err := db.LoadVersionsByAccount(account)
|
||||
if err != nil {
|
||||
errorResponse(w, r, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
isAdmin := ""
|
||||
if account.IsAdmin {
|
||||
isAdmin = "1"
|
||||
}
|
||||
render(w, accountsIdTemplates, http.StatusOK, AccountsIdPage{
|
||||
Account: account,
|
||||
IsAdmin: isAdmin,
|
||||
Page: makePage(r, &Page{
|
||||
Section: "accounts",
|
||||
Title: account.Username,
|
||||
}),
|
||||
StatePaths: statePaths,
|
||||
Versions: versions,
|
||||
})
|
||||
render(w, accountsIdTemplates, http.StatusOK, page)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue