| 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) createBug(w http.ResponseWriter, r *http.Request) { |
| 12 | r.Body = http.MaxBytesReader(w, r.Body, 64*1024) |
| 13 | if r.ParseForm() != nil || len(r.URL.Query()) != 0 { |
| 14 | a.failure(w, r, ErrInvalid) |
| 15 | return |
| 16 | } |
| 17 | for key, values := range r.PostForm { |
| 18 | if len(values) != 1 || key != "project_id" && key != "description" && key != "request_id" { |
| 19 | a.failure(w, r, ErrInvalid) |
| 20 | return |
| 21 | } |
| 22 | } |
| 23 | id, ok := identity.From(r) |
| 24 | if !ok { |
| 25 | a.failure(w, r, ErrForbidden) |
| 26 | return |
| 27 | } |
| 28 | projectID, err := strconv.ParseInt(r.PostForm.Get("project_id"), 10, 64) |
| 29 | if err != nil { |
| 30 | a.failure(w, r, ErrInvalid) |
| 31 | return |
| 32 | } |
| 33 | task, err := a.store.CreateBug(r.Context(), Actor{Address: id.Address}, projectID, r.PostForm.Get("description"), r.PostForm.Get("request_id")) |
| 34 | if err != nil { |
| 35 | a.failure(w, r, err) |
| 36 | return |
| 37 | } |
| 38 | http.Redirect(w, r, "/tasks/"+task.ID, http.StatusSeeOther) |
| 39 | } |
| 40 | |
| 41 | func (a *application) cancelTask(w http.ResponseWriter, r *http.Request) { |
| 42 | r.Body = http.MaxBytesReader(w, r.Body, 8192) |
| 43 | if r.ParseForm() != nil || len(r.URL.Query()) != 0 || len(r.PostForm) != 0 { |
| 44 | a.failure(w, r, ErrInvalid) |
| 45 | return |
| 46 | } |
| 47 | actor, ok := identity.From(r) |
| 48 | if !ok { |
| 49 | a.failure(w, r, ErrForbidden) |
| 50 | return |
| 51 | } |
| 52 | id := chi.URLParam(r, "id") |
| 53 | if _, err := a.store.CancelTask(r.Context(), Actor{Address: actor.Address}, id); err != nil { |
| 54 | a.failure(w, r, err) |
| 55 | return |
| 56 | } |
| 57 | http.Redirect(w, r, "/tasks/"+id, http.StatusSeeOther) |
| 58 | } |
| 59 | |
| 60 | func (a *application) oldRunnerStopped(w http.ResponseWriter, r *http.Request) { |
| 61 | r.Body = http.MaxBytesReader(w, r.Body, 8192) |
| 62 | if r.ParseForm() != nil || len(r.URL.Query()) != 0 || len(r.PostForm) != 0 { |
| 63 | a.failure(w, r, ErrInvalid) |
| 64 | return |
| 65 | } |
| 66 | actor, ok := identity.From(r) |
| 67 | if !ok { |
| 68 | a.failure(w, r, ErrForbidden) |
| 69 | return |
| 70 | } |
| 71 | id := chi.URLParam(r, "id") |
| 72 | if err := a.store.OldRunnerStopped(r.Context(), Actor{Address: actor.Address}, id); err != nil { |
| 73 | a.failure(w, r, err) |
| 74 | return |
| 75 | } |
| 76 | http.Redirect(w, r, "/tasks/"+id, http.StatusSeeOther) |
| 77 | } |
| 78 | |