From 57accbddbbaf0caa0b1af8fd7391d40171595c4e Mon Sep 17 00:00:00 2001 From: soukainna Date: Wed, 1 Feb 2023 09:53:01 +0100 Subject: [PATCH] add function to update and delete event --- go.mod | 2 +- handlers/application/agenda.go | 122 ++++++++++++++++++++++++++++++++- main.go | 5 ++ renderer/agenda.go | 23 +++++++ 4 files changed, 150 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 9173408..9317ccc 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ go 1.18 // replace git.coopgo.io/coopgo-platform/fleets => ../../coopgo-platform/fleets/ -// replace git.coopgo.io/coopgo-platform/agenda => ../../coopgo-platform/agenda/ +replace git.coopgo.io/coopgo-platform/agenda => ../../coopgo-platform/agenda/ // replace git.coopgo.io/coopgo-platform/emailing => ../../coopgo-platform/emailing/ diff --git a/handlers/application/agenda.go b/handlers/application/agenda.go index f9395a4..cd97a6f 100644 --- a/handlers/application/agenda.go +++ b/handlers/application/agenda.go @@ -7,6 +7,7 @@ import ( "net/http" "sort" "strconv" + "strings" "time" formvalidators "git.coopgo.io/coopgo-apps/parcoursmob/utils/form-validators" @@ -107,6 +108,7 @@ func (h *ApplicationHandler) AgendaCreateEvent(w http.ResponseWriter, r *http.Re Allday: eventForm.Allday, MaxSubscribers: int64(eventForm.MaxSubscribers), Data: data, + Deleted: false, }, } @@ -119,7 +121,7 @@ func (h *ApplicationHandler) AgendaCreateEvent(w http.ResponseWriter, r *http.Re } http.Redirect(w, r, fmt.Sprintf("/app/agenda/%s", resp.Event.Id), http.StatusFound) - + return } h.Renderer.AgendaCreateEvent(w, r) } @@ -330,6 +332,124 @@ func contains(s []*agenda.Subscription, e string) bool { return false } +///////////////////////////////Update Event///////////////////////////////////////// +func (h *ApplicationHandler) AgendaUpdateEvent(w http.ResponseWriter, r *http.Request) { + adm := strings.Split(r.URL.Path, "/") + eventID := adm[3] + request := &agenda.GetEventRequest{ + Id: eventID, + } + + resp, err := h.services.GRPC.Agenda.GetEvent(context.TODO(), request) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + if r.Method == "POST" { + g := r.Context().Value(identification.GroupKey) + if g == nil { + w.WriteHeader(http.StatusBadRequest) + return + } + + group := g.(storage.Group) + + eventForm, err := parseEventsForm(r) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusBadRequest) + return + } + + data, _ := structpb.NewStruct(map[string]any{ + "address": eventForm.Address, + }) + + request := &agenda.UpdateEventRequest{ + Event: &agenda.Event{ + Namespace: "parcoursmob_dispositifs", + Id: eventID, + Owners: []string{group.ID}, + Type: eventForm.Type, + Name: eventForm.Name, + Description: eventForm.Description, + Startdate: timestamppb.New(*eventForm.Startdate), + Enddate: timestamppb.New(*eventForm.Enddate), + Starttime: eventForm.Starttime, + Endtime: eventForm.Endtime, + Allday: eventForm.Allday, + MaxSubscribers: int64(eventForm.MaxSubscribers), + Data: data, + Subscriptions: resp.Event.Subscriptions, + }, + } + + resp, err := h.services.GRPC.Agenda.UpdateEvent(context.TODO(), request) + + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + http.Redirect(w, r, fmt.Sprintf("/app/agenda/%s", resp.Event.Id), http.StatusFound) + return + } + h.Renderer.AgendaUpdateEvent(w, r, resp.Event.ToStorageType()) +} + +func (h *ApplicationHandler) AgendaDeleteEvent(w http.ResponseWriter, r *http.Request) { + + vars := mux.Vars(r) + eventID := vars["eventid"] + request := &agenda.GetEventRequest{ + Id: eventID, + } + + resp, err := h.services.GRPC.Agenda.GetEvent(context.TODO(), request) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + if r.Method == "POST" { + + request := &agenda.UpdateEventRequest{ + Event: &agenda.Event{ + Namespace: resp.Event.Namespace, + Id: resp.Event.Id, + Owners: resp.Event.Owners, + Type: resp.Event.Type, + Name: resp.Event.Name, + Description: resp.Event.Description, + Startdate: resp.Event.Startdate, + Enddate: resp.Event.Enddate, + Starttime: resp.Event.Starttime, + Endtime: resp.Event.Endtime, + Allday: resp.Event.Allday, + MaxSubscribers: int64(resp.Event.MaxSubscribers), + Data: resp.Event.Data, + Subscriptions: resp.Event.Subscriptions, + Deleted: true, + }, + } + + _, err := h.services.GRPC.Agenda.UpdateEvent(context.TODO(), request) + + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + http.Redirect(w, r, "/app/agenda/", http.StatusFound) + return + } + h.Renderer.AgendaDeleteEvent(w, r, resp.Event.ToStorageType()) +} + // func contains[V string](s []V, e V) bool { // for _, a := range s { // if a == e { diff --git a/main.go b/main.go index 154930a..61ca9cc 100644 --- a/main.go +++ b/main.go @@ -104,6 +104,11 @@ func main() { application.HandleFunc("/agenda/", applicationHandler.AgendaHome) application.HandleFunc("/agenda/create-event", applicationHandler.AgendaCreateEvent) application.HandleFunc("/agenda/{eventid}", applicationHandler.AgendaDisplayEvent) + ///////////////////////////////Code to modify event/////////////////////// + application.HandleFunc("/agenda/{eventid}/update", applicationHandler.AgendaUpdateEvent) + application.HandleFunc("/agenda/{eventid}/delete", applicationHandler.AgendaDeleteEvent) + + ///////////////////////////////////////////////////// application.HandleFunc("/agenda/{eventid}/subscribe", applicationHandler.AgendaSubscribeEvent) application.HandleFunc("/directory/", applicationHandler.DirectoryHome) diff --git a/renderer/agenda.go b/renderer/agenda.go index 4197e01..3096a80 100644 --- a/renderer/agenda.go +++ b/renderer/agenda.go @@ -29,6 +29,7 @@ func (renderer *Renderer) AgendaCreateEvent(w http.ResponseWriter, r *http.Reque func (renderer *Renderer) AgendaDisplayEvent(w http.ResponseWriter, r *http.Request, event any, group any, subscribers map[string]any, beneficiaries any) { files := renderer.ThemeConfig.GetStringSlice("views.agenda.display_event.files") state := NewState(r, renderer.ThemeConfig, agendaMenu) + state.ViewState = map[string]any{ "event": event, "group": group, @@ -38,3 +39,25 @@ func (renderer *Renderer) AgendaDisplayEvent(w http.ResponseWriter, r *http.Requ renderer.Render("agenda create event", w, r, files, state) } + +func (renderer *Renderer) AgendaUpdateEvent(w http.ResponseWriter, r *http.Request, event any) { + files := renderer.ThemeConfig.GetStringSlice("views.agenda.update.files") + state := NewState(r, renderer.ThemeConfig, agendaMenu) + + state.ViewState = map[string]any{ + "event": event, + } + + renderer.Render("event_update", w, r, files, state) +} + +func (renderer *Renderer) AgendaDeleteEvent(w http.ResponseWriter, r *http.Request, event any) { + files := renderer.ThemeConfig.GetStringSlice("views.agenda.delete.files") + state := NewState(r, renderer.ThemeConfig, agendaMenu) + + state.ViewState = map[string]any{ + "event": event, + } + + renderer.Render("event_deleteEvent", w, r, files, state) +}