| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strconv" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/equipo/identity" |
| 8 | "github.com/go-chi/chi/v5" |
| 9 | ) |
| 10 | |
| 11 | func (a *application) connectionForm(w http.ResponseWriter, r *http.Request) (Actor, int64, bool) { |
| 12 | r.Body = http.MaxBytesReader(w, r.Body, 16*1024) |
| 13 | if r.ParseForm() != nil { |
| 14 | a.failure(w, r, ErrInvalid) |
| 15 | return Actor{}, 0, false |
| 16 | } |
| 17 | id, ok := identity.From(r) |
| 18 | if !ok { |
| 19 | a.failure(w, r, ErrForbidden) |
| 20 | return Actor{}, 0, false |
| 21 | } |
| 22 | revision, err := strconv.ParseInt(r.PostForm.Get("revision"), 10, 64) |
| 23 | if err != nil || revision < 0 { |
| 24 | a.failure(w, r, ErrInvalid) |
| 25 | return Actor{}, 0, false |
| 26 | } |
| 27 | return Actor{Address: id.Address}, revision, true |
| 28 | } |
| 29 | func (a *application) saveConnectionChoice(w http.ResponseWriter, r *http.Request) { |
| 30 | actor, revision, ok := a.connectionForm(w, r) |
| 31 | if !ok { |
| 32 | return |
| 33 | } |
| 34 | project, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) |
| 35 | if err != nil { |
| 36 | a.failure(w, r, ErrInvalid) |
| 37 | return |
| 38 | } |
| 39 | choice := ConnectionChoice{ProjectID: project, Revision: revision, ConnectionIDs: r.PostForm["connection_id"], PinnedID: r.PostForm.Get("pinned_id"), Mode: r.PostForm.Get("mode")} |
| 40 | if err := a.store.SaveConnectionChoice(r.Context(), actor, choice); err != nil { |
| 41 | a.failure(w, r, err) |
| 42 | return |
| 43 | } |
| 44 | http.Redirect(w, r, "/connections", http.StatusSeeOther) |
| 45 | } |
| 46 | |
| 47 | func (a *application) confirmPlanSelection(w http.ResponseWriter, r *http.Request) { |
| 48 | actor, revision, ok := a.connectionForm(w, r) |
| 49 | if !ok { |
| 50 | return |
| 51 | } |
| 52 | id := chi.URLParam(r, "id") |
| 53 | _, err := a.store.ConfirmPlanSelection(r.Context(), actor, id, chi.URLParam(r, "key"), r.PostForm.Get("connection_id"), r.PostForm.Get("model"), Mutation{RequestID: r.PostForm.Get("request_id"), Revision: revision}) |
| 54 | if err != nil { |
| 55 | a.failure(w, r, err) |
| 56 | return |
| 57 | } |
| 58 | http.Redirect(w, r, "/tasks/"+id+"/plan", http.StatusSeeOther) |
| 59 | } |
| 60 | |