101 lines
3.1 KiB
Go
101 lines
3.1 KiB
Go
package application
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.coopgo.io/coopgo-apps/parcoursmob/core/utils/identification"
|
|
"git.coopgo.io/coopgo-platform/groups-management/storage"
|
|
"github.com/gorilla/mux"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func (h *Handler) GroupsHTTPHandler() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
result, err := h.applicationHandler.GetGroups(r.Context())
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error getting groups")
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
h.renderer.Groups(w, r, result.Groups)
|
|
}
|
|
}
|
|
|
|
func (h *Handler) CreateGroupModuleHTTPHandler() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "POST" {
|
|
r.ParseForm()
|
|
|
|
name := r.FormValue("name")
|
|
groupType := r.FormValue("type")
|
|
description := r.FormValue("description")
|
|
address := r.PostFormValue("address")
|
|
|
|
result, err := h.applicationHandler.CreateGroupModule(r.Context(), name, groupType, description, address)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error creating group module")
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
http.Redirect(w, r, "/app/group_module/groups/"+result.GroupID, http.StatusFound)
|
|
return
|
|
}
|
|
|
|
result, err := h.applicationHandler.GetGroupModuleCreateData(r.Context())
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error getting group module create data")
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
h.renderer.CreateGroupModule(w, r, result.GroupTypes)
|
|
}
|
|
}
|
|
|
|
func (h *Handler) DisplayGroupModuleHTTPHandler() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
groupID := vars["groupid"]
|
|
|
|
if r.Method == "POST" || r.FormValue("beneficiaryid") != "" {
|
|
r.ParseForm()
|
|
beneficiaryID := r.FormValue("beneficiaryid")
|
|
|
|
err := h.applicationHandler.SubscribeBeneficiaryToGroup(r.Context(), groupID, beneficiaryID)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error subscribing beneficiary to group")
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
http.Redirect(w, r, "/app/group_module/groups/"+groupID, http.StatusFound)
|
|
return
|
|
}
|
|
|
|
// Get current user's group from context
|
|
g := r.Context().Value(identification.GroupKey)
|
|
if g == nil {
|
|
http.Error(w, "Bad Request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
currentUserGroup := g.(storage.Group)
|
|
|
|
// Parse search filter
|
|
searchFilter := ""
|
|
if searchFilters, ok := r.URL.Query()["search"]; ok && len(searchFilters) > 0 {
|
|
searchFilter = searchFilters[0]
|
|
}
|
|
|
|
result, err := h.applicationHandler.DisplayGroupModule(r.Context(), groupID, searchFilter, currentUserGroup)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("error displaying group module")
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
h.renderer.DisplayGroupModule(w, r, result.GroupID, result.Accounts, result.CacheID,
|
|
result.Searched, result.Beneficiary, result.Group, result.AccountsBeneficiaire)
|
|
}
|
|
} |