|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "chameth.com/chameth.com/cmd/serve/admin/templates" |
| 9 | + "chameth.com/chameth.com/cmd/serve/db" |
| 10 | +) |
| 11 | + |
| 12 | +func ListSyndicationsHandler() func(http.ResponseWriter, *http.Request) { |
| 13 | + return func(w http.ResponseWriter, r *http.Request) { |
| 14 | + unpublished, err := db.GetUnpublishedSyndications() |
| 15 | + if err != nil { |
| 16 | + http.Error(w, "Failed to retrieve unpublished syndications", http.StatusInternalServerError) |
| 17 | + return |
| 18 | + } |
| 19 | + |
| 20 | + syndications, err := db.GetAllSyndications() |
| 21 | + if err != nil { |
| 22 | + http.Error(w, "Failed to retrieve syndications", http.StatusInternalServerError) |
| 23 | + return |
| 24 | + } |
| 25 | + |
| 26 | + unpublishedSummaries := make([]templates.SyndicationSummary, len(unpublished)) |
| 27 | + for i, s := range unpublished { |
| 28 | + unpublishedSummaries[i] = templates.SyndicationSummary{ |
| 29 | + ID: s.ID, |
| 30 | + Path: s.Path, |
| 31 | + ExternalURL: s.ExternalURL, |
| 32 | + Name: s.Name, |
| 33 | + Published: s.Published, |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + syndicationSummaries := make([]templates.SyndicationSummary, len(syndications)) |
| 38 | + for i, s := range syndications { |
| 39 | + syndicationSummaries[i] = templates.SyndicationSummary{ |
| 40 | + ID: s.ID, |
| 41 | + Path: s.Path, |
| 42 | + ExternalURL: s.ExternalURL, |
| 43 | + Name: s.Name, |
| 44 | + Published: s.Published, |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + data := templates.ListSyndicationsData{ |
| 49 | + Unpublished: unpublishedSummaries, |
| 50 | + Syndications: syndicationSummaries, |
| 51 | + } |
| 52 | + |
| 53 | + if err := templates.RenderListSyndications(w, data); err != nil { |
| 54 | + http.Error(w, "Failed to render template", http.StatusInternalServerError) |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func EditSyndicationHandler() func(http.ResponseWriter, *http.Request) { |
| 60 | + return func(w http.ResponseWriter, r *http.Request) { |
| 61 | + idStr := r.PathValue("id") |
| 62 | + id, err := strconv.Atoi(idStr) |
| 63 | + if err != nil { |
| 64 | + http.Error(w, "Invalid syndication ID", http.StatusBadRequest) |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + syndication, err := db.GetSyndicationByID(id) |
| 69 | + if err != nil { |
| 70 | + http.Error(w, "Syndication not found", http.StatusNotFound) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + data := templates.EditSyndicationData{ |
| 75 | + ID: syndication.ID, |
| 76 | + Path: syndication.Path, |
| 77 | + ExternalURL: syndication.ExternalURL, |
| 78 | + Name: syndication.Name, |
| 79 | + Published: syndication.Published, |
| 80 | + } |
| 81 | + |
| 82 | + if err := templates.RenderEditSyndication(w, data); err != nil { |
| 83 | + http.Error(w, "Failed to render template", http.StatusInternalServerError) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func CreateSyndicationHandler() func(http.ResponseWriter, *http.Request) { |
| 89 | + return func(w http.ResponseWriter, r *http.Request) { |
| 90 | + if err := r.ParseForm(); err != nil { |
| 91 | + http.Error(w, "Failed to parse form", http.StatusBadRequest) |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + path := r.FormValue("path") |
| 96 | + externalURL := r.FormValue("external_url") |
| 97 | + name := r.FormValue("name") |
| 98 | + |
| 99 | + if path == "" || externalURL == "" || name == "" { |
| 100 | + http.Error(w, "Path, external URL, and name are required", http.StatusBadRequest) |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + id, err := db.CreateSyndication(path, externalURL, name) |
| 105 | + if err != nil { |
| 106 | + http.Error(w, "Failed to create syndication", http.StatusInternalServerError) |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + http.Redirect(w, r, fmt.Sprintf("/syndications/edit/%d", id), http.StatusSeeOther) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func UpdateSyndicationHandler() func(http.ResponseWriter, *http.Request) { |
| 115 | + return func(w http.ResponseWriter, r *http.Request) { |
| 116 | + idStr := r.PathValue("id") |
| 117 | + id, err := strconv.Atoi(idStr) |
| 118 | + if err != nil { |
| 119 | + http.Error(w, "Invalid syndication ID", http.StatusBadRequest) |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + if err := r.ParseForm(); err != nil { |
| 124 | + http.Error(w, "Failed to parse form", http.StatusBadRequest) |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + path := r.FormValue("path") |
| 129 | + externalURL := r.FormValue("external_url") |
| 130 | + name := r.FormValue("name") |
| 131 | + published := r.FormValue("published") == "true" |
| 132 | + |
| 133 | + if err := db.UpdateSyndication(id, path, externalURL, name, published); err != nil { |
| 134 | + http.Error(w, "Failed to update syndication", http.StatusInternalServerError) |
| 135 | + return |
| 136 | + } |
| 137 | + |
| 138 | + http.Redirect(w, r, fmt.Sprintf("/syndications/edit/%d", id), http.StatusSeeOther) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func DeleteSyndicationHandler() func(http.ResponseWriter, *http.Request) { |
| 143 | + return func(w http.ResponseWriter, r *http.Request) { |
| 144 | + idStr := r.PathValue("id") |
| 145 | + id, err := strconv.Atoi(idStr) |
| 146 | + if err != nil { |
| 147 | + http.Error(w, "Invalid syndication ID", http.StatusBadRequest) |
| 148 | + return |
| 149 | + } |
| 150 | + |
| 151 | + if err := db.DeleteSyndication(id); err != nil { |
| 152 | + http.Error(w, "Failed to delete syndication", http.StatusInternalServerError) |
| 153 | + return |
| 154 | + } |
| 155 | + |
| 156 | + http.Redirect(w, r, "/syndications", http.StatusSeeOther) |
| 157 | + } |
| 158 | +} |
0 commit comments