oficina / calendar Public

Clone
git clone https://amadan.net/oficina/calendar

Plain git — no account needed to clone.

Download

Download this file

1package calendar
2
3import (
4 "net/http"
5 "strconv"
6
7 "github.com/go-chi/chi/v5"
8)
9
10// deleteEvent handles POST /events/{id}/delete.
11func (h *handlers) deleteEvent(w http.ResponseWriter, r *http.Request) {
12 p, _ := CurrentPerson(r)
13 id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
14 if err != nil {
15 h.errorPage(w, r, http.StatusNotFound, "")
16 return
17 }
18 _ = r.ParseForm()
19 scope := r.FormValue("scope")
20 occurrence := r.FormValue("occurrence")
21
22 // "Just this one" is the default, and it is first in the form. The
23 // destructive end of the scale — every occurrence, with its count
24 // named — is last, so the cheap answer is never one keystroke away
25 // from the expensive one.
26 if scope == "this" && occurrence != "" {
27 if err := h.store.ExcludeOccurrence(r.Context(), p.ID, id, occurrence); err != nil {
28 h.deleteFailed(w, r, err)
29 return
30 }
31 http.Redirect(w, r, "/", http.StatusSeeOther)
32 return
33 }
34 if err := h.store.DeleteEvent(r.Context(), p.ID, id); err != nil {
35 h.deleteFailed(w, r, err)
36 return
37 }
38 http.Redirect(w, r, "/", http.StatusSeeOther)
39}
40
41func (h *handlers) deleteFailed(w http.ResponseWriter, r *http.Request, err error) {
42 switch {
43 case NotFound(err):
44 h.errorPage(w, r, http.StatusNotFound, "")
45 case err == ErrReadOnly:
46 h.errorPage(w, r, http.StatusForbidden, "")
47 default:
48 h.fail(w, r, "delete event", err)
49 }
50}
51
52// confirmDelete is the destructive action's own URL, never a modal —
53// so it works with no script, can be linked to, and can be backed out
54// of with the browser's own back button.
55func (h *handlers) confirmDelete(w http.ResponseWriter, r *http.Request) {
56 p, _ := CurrentPerson(r)
57 id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
58 if err != nil {
59 h.errorPage(w, r, http.StatusNotFound, "")
60 return
61 }
62 e, err := h.store.Event(r.Context(), p.ID, id)
63 if NotFound(err) {
64 h.errorPage(w, r, http.StatusNotFound, "")
65 return
66 }
67 if err != nil {
68 h.fail(w, r, "read event", err)
69 return
70 }
71 count, forever := h.store.CountOccurrences(e)
72 render(w, "event_delete", h.page(r, "Delete", map[string]any{
73 "Event": e,
74 "Occurrence": r.URL.Query().Get("occurrence"),
75 "Series": e.RRule != "",
76 "Count": count,
77 "Forever": forever,
78 }))
79}
80