| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package api
- import (
- "encoding/json"
- "net/http"
- "net/url"
- "strconv"
- "github.com/go-chi/chi"
- "github.com/porter-dev/porter/internal/forms"
- )
- // HandleCreateEvent creates a new event in a project
- func (app *App) HandleCreateEvent(w http.ResponseWriter, r *http.Request) {
- projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
- if err != nil || projID == 0 {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- vals, err := url.ParseQuery(r.URL.RawQuery)
- clusterID, err := strconv.ParseUint(vals["cluster_id"][0], 10, 64)
- if err != nil {
- app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
- Code: ErrReleaseReadData,
- Errors: []string{"cluster not found"},
- }, w)
- }
- form := &forms.CreateEventForm{}
- // decode from JSON to form value
- if err := json.NewDecoder(r.Body).Decode(form); err != nil {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- // validate the form
- if err := app.validator.Struct(form); err != nil {
- app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
- return
- }
- // convert the form to an invite
- event := form.ToEvent(uint(projID), uint(clusterID))
- if err != nil {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- // handle write to the database
- event, err = app.Repo.Event.CreateEvent(event)
- if err != nil {
- app.handleErrorDataWrite(err, w)
- return
- }
- w.WriteHeader(http.StatusCreated)
- }
|