add function to update and delete event

This commit is contained in:
soukainna 2023-02-01 09:53:01 +01:00
parent 1b847ea216
commit 57accbddbb
4 changed files with 150 additions and 2 deletions

2
go.mod
View File

@ -8,7 +8,7 @@ go 1.18
// replace git.coopgo.io/coopgo-platform/fleets => ../../coopgo-platform/fleets/ // 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/ // replace git.coopgo.io/coopgo-platform/emailing => ../../coopgo-platform/emailing/

View File

@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"sort" "sort"
"strconv" "strconv"
"strings"
"time" "time"
formvalidators "git.coopgo.io/coopgo-apps/parcoursmob/utils/form-validators" 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, Allday: eventForm.Allday,
MaxSubscribers: int64(eventForm.MaxSubscribers), MaxSubscribers: int64(eventForm.MaxSubscribers),
Data: data, 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) http.Redirect(w, r, fmt.Sprintf("/app/agenda/%s", resp.Event.Id), http.StatusFound)
return
} }
h.Renderer.AgendaCreateEvent(w, r) h.Renderer.AgendaCreateEvent(w, r)
} }
@ -330,6 +332,124 @@ func contains(s []*agenda.Subscription, e string) bool {
return false 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 { // func contains[V string](s []V, e V) bool {
// for _, a := range s { // for _, a := range s {
// if a == e { // if a == e {

View File

@ -104,6 +104,11 @@ func main() {
application.HandleFunc("/agenda/", applicationHandler.AgendaHome) application.HandleFunc("/agenda/", applicationHandler.AgendaHome)
application.HandleFunc("/agenda/create-event", applicationHandler.AgendaCreateEvent) application.HandleFunc("/agenda/create-event", applicationHandler.AgendaCreateEvent)
application.HandleFunc("/agenda/{eventid}", applicationHandler.AgendaDisplayEvent) 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("/agenda/{eventid}/subscribe", applicationHandler.AgendaSubscribeEvent)
application.HandleFunc("/directory/", applicationHandler.DirectoryHome) application.HandleFunc("/directory/", applicationHandler.DirectoryHome)

View File

@ -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) { 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") files := renderer.ThemeConfig.GetStringSlice("views.agenda.display_event.files")
state := NewState(r, renderer.ThemeConfig, agendaMenu) state := NewState(r, renderer.ThemeConfig, agendaMenu)
state.ViewState = map[string]any{ state.ViewState = map[string]any{
"event": event, "event": event,
"group": group, "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) 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)
}