From 47a2b1a3cb96566f5ae80cab4b6ebe5b0135573b Mon Sep 17 00:00:00 2001 From: soukainna Date: Thu, 15 Dec 2022 17:08:30 +0100 Subject: [PATCH] add update profile --- handlers/application/conseillers.go | 99 +++++++++++++++++++++++++++++ main.go | 4 +- renderer/conseillers.go | 7 ++ 3 files changed, 108 insertions(+), 2 deletions(-) diff --git a/handlers/application/conseillers.go b/handlers/application/conseillers.go index bb823a2..8d8ec42 100644 --- a/handlers/application/conseillers.go +++ b/handlers/application/conseillers.go @@ -2,13 +2,25 @@ package application import ( "context" + "encoding/json" "fmt" "net/http" "strings" + formvalidators "git.coopgo.io/coopgo-apps/parcoursmob/utils/form-validators" mobilityaccounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi" + "google.golang.org/protobuf/types/known/structpb" ) +type UserForm struct { + FirstName string `json:"first_name" validate:"required"` + LastName string `json:"last_name" validate:"required"` + Email string `json:"email" validate:"required,email"` + PhoneNumber string `json:"phone_number" validate:"required,phoneNumber"` + Address any `json:"address,omitempty"` + Gender string `json:"gender"` +} + func (h *ApplicationHandler) ConseillerDisplay(w http.ResponseWriter, r *http.Request) { adm := strings.Split(r.URL.Path, "/") @@ -27,3 +39,90 @@ func (h *ApplicationHandler) ConseillerDisplay(w http.ResponseWriter, r *http.Re h.Renderer.ConseillerDisplay(w, r, resp.Account.ToStorageType()) } + +func (h *ApplicationHandler) ConseillerUpdate(w http.ResponseWriter, r *http.Request) { + adm := strings.Split(r.URL.Path, "/") + userID := adm[3] + + if r.Method == "POST" { + + dataMap, err := parseUserForm(r) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusBadRequest) + return + } + + data, err := structpb.NewValue(dataMap) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + request := &mobilityaccounts.UpdateDataRequest{ + Account: &mobilityaccounts.Account{ + Id: userID, + Namespace: "parcoursmob", + Data: data.GetStructValue(), + }, + } + + resp, err := h.services.GRPC.MobilityAccounts.UpdateData(context.TODO(), request) + + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + http.Redirect(w, r, fmt.Sprintf("/app/profile/%s", resp.Account.Id), http.StatusFound) + + return + } + + request := &mobilityaccounts.GetAccountRequest{ + Id: userID, + } + + resp, err := h.services.GRPC.MobilityAccounts.GetAccount(context.TODO(), request) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + h.Renderer.ConseillerUpdate(w, r, resp.Account.ToStorageType()) +} + +func parseUserForm(r *http.Request) (map[string]any, error) { + if err := r.ParseForm(); err != nil { + return nil, err + } + + formData := UserForm{ + FirstName: r.PostFormValue("first_name"), + LastName: r.PostFormValue("last_name"), + Email: r.PostFormValue("email"), + PhoneNumber: r.PostFormValue("phone_number"), + Gender: r.PostFormValue("gender"), + } + + validate := formvalidators.New() + if err := validate.Struct(formData); err != nil { + return nil, err + } + + d, err := json.Marshal(formData) + if err != nil { + return nil, err + } + + var dataMap map[string]any + err = json.Unmarshal(d, &dataMap) + if err != nil { + return nil, err + } + + return dataMap, nil +} diff --git a/main.go b/main.go index 3831d9e..d4ef44f 100644 --- a/main.go +++ b/main.go @@ -134,8 +134,8 @@ func main() { appAdmin.HandleFunc("/groups/{groupid}/invite-member", applicationHandler.AdministrationGroupInviteMember) ///////////////////////////code page profile of admin /////////////////////////// - application.HandleFunc("/conseillers/{adminid}", applicationHandler.ConseillerDisplay) - + application.HandleFunc("/profile/{adminid}", applicationHandler.ConseillerDisplay) + application.HandleFunc("/profile/{adminid}/update", applicationHandler.ConseillerUpdate) //TODO Secure with Middleware checking for modules fmt.Println("-> HTTP server listening on", address) diff --git a/renderer/conseillers.go b/renderer/conseillers.go index 411c2b4..cdf6c1b 100644 --- a/renderer/conseillers.go +++ b/renderer/conseillers.go @@ -15,3 +15,10 @@ func (renderer *Renderer) ConseillerDisplay(w http.ResponseWriter, r *http.Reque } renderer.Render("conseillers_list", w, r, files, state) } + +func (renderer *Renderer) ConseillerUpdate(w http.ResponseWriter, r *http.Request, user any) { + files := renderer.ThemeConfig.GetStringSlice("views.conseillers.update.files") + state := NewState(r, renderer.ThemeConfig, conseillersMenu) + state.ViewState = user + renderer.Render("conseillers_update", w, r, files, state) +}