- Implemented `CreateSyndicate` with transaction support - Added `InviteToSyndicate` with membership check and invite insert - Created syndicate invite model and DB logic for accepting/declining - Ensured consistent error handling with `fmt.Errorf` - Cleaned up unused invite handlers/routes
92 lines
2.6 KiB
Go
92 lines
2.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"database/sql"
|
|
"net/http"
|
|
"strconv"
|
|
"synlotto-website/helpers"
|
|
"synlotto-website/storage"
|
|
)
|
|
|
|
func SyndicateInviteHandler(db *sql.DB) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
userID, ok := helpers.GetCurrentUserID(r)
|
|
if !ok {
|
|
helpers.RenderError(w, r, http.StatusForbidden)
|
|
return
|
|
}
|
|
|
|
switch r.Method {
|
|
case http.MethodGet:
|
|
syndicateID := helpers.Atoi(r.URL.Query().Get("id"))
|
|
data := BuildTemplateData(db, w, r)
|
|
context := helpers.TemplateContext(w, r, data)
|
|
context["SyndicateID"] = syndicateID
|
|
|
|
tmpl := helpers.LoadTemplateFiles("invite-syndicate.html", "templates/syndicates/invite.html")
|
|
err := tmpl.ExecuteTemplate(w, "layout", context)
|
|
if err != nil {
|
|
helpers.RenderError(w, r, 500)
|
|
}
|
|
case http.MethodPost:
|
|
syndicateID := helpers.Atoi(r.FormValue("syndicate_id"))
|
|
username := r.FormValue("username")
|
|
|
|
err := storage.InviteToSyndicate(db, userID, syndicateID, username)
|
|
if err != nil {
|
|
helpers.SetFlash(w, r, "Failed to send invite: "+err.Error())
|
|
} else {
|
|
helpers.SetFlash(w, r, "Invite sent successfully.")
|
|
}
|
|
http.Redirect(w, r, "/account/syndicates/view?id="+strconv.Itoa(syndicateID), http.StatusSeeOther)
|
|
|
|
default:
|
|
helpers.RenderError(w, r, http.StatusMethodNotAllowed)
|
|
}
|
|
}
|
|
}
|
|
|
|
func ViewInvitesHandler(db *sql.DB) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
userID, ok := helpers.GetCurrentUserID(r)
|
|
if !ok {
|
|
helpers.RenderError(w, r, 403)
|
|
return
|
|
}
|
|
|
|
invites := storage.GetPendingInvites(db, userID)
|
|
data := BuildTemplateData(db, w, r)
|
|
context := helpers.TemplateContext(w, r, data)
|
|
context["Invites"] = invites
|
|
|
|
tmpl := helpers.LoadTemplateFiles("invites.html", "templates/syndicates/invites.html")
|
|
tmpl.ExecuteTemplate(w, "layout", context)
|
|
}
|
|
}
|
|
|
|
func AcceptInviteHandler(db *sql.DB) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
inviteID := helpers.Atoi(r.URL.Query().Get("id"))
|
|
userID, ok := helpers.GetCurrentUserID(r)
|
|
if !ok {
|
|
helpers.RenderError(w, r, 403)
|
|
return
|
|
}
|
|
err := storage.AcceptInvite(db, inviteID, userID)
|
|
if err != nil {
|
|
helpers.SetFlash(w, r, "Failed to accept invite")
|
|
} else {
|
|
helpers.SetFlash(w, r, "You have joined the syndicate")
|
|
}
|
|
http.Redirect(w, r, "/account/syndicates", http.StatusSeeOther)
|
|
}
|
|
}
|
|
|
|
func DeclineInviteHandler(db *sql.DB) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
inviteID := helpers.Atoi(r.URL.Query().Get("id"))
|
|
_ = storage.UpdateInviteStatus(db, inviteID, "declined")
|
|
http.Redirect(w, r, "/account/syndicates/invites", http.StatusSeeOther)
|
|
}
|
|
}
|