github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/api/server/apps_update.go (about)

     1  package server
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  
     7  	"github.com/gin-gonic/gin"
     8  	"github.com/iron-io/functions/api"
     9  	"github.com/iron-io/functions/api/models"
    10  	"github.com/iron-io/runner/common"
    11  )
    12  
    13  func (s *Server) handleAppUpdate(c *gin.Context) {
    14  	ctx := c.MustGet("ctx").(context.Context)
    15  	log := common.Logger(ctx)
    16  
    17  	wapp := models.AppWrapper{}
    18  
    19  	err := c.BindJSON(&wapp)
    20  	if err != nil {
    21  		log.WithError(err).Debug(models.ErrInvalidJSON)
    22  		c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON))
    23  		return
    24  	}
    25  
    26  	if wapp.App == nil {
    27  		log.Debug(models.ErrAppsMissingNew)
    28  		c.JSON(http.StatusBadRequest, simpleError(models.ErrAppsMissingNew))
    29  		return
    30  	}
    31  
    32  	if wapp.App.Name != "" {
    33  		log.Debug(models.ErrAppsNameImmutable)
    34  		c.JSON(http.StatusBadRequest, simpleError(models.ErrAppsNameImmutable))
    35  		return
    36  	}
    37  
    38  	wapp.App.Name = c.MustGet(api.AppName).(string)
    39  
    40  	err = s.FireAfterAppUpdate(ctx, wapp.App)
    41  	if err != nil {
    42  		log.WithError(err).Error(models.ErrAppsUpdate)
    43  		c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
    44  		return
    45  	}
    46  
    47  	app, err := s.Datastore.UpdateApp(ctx, wapp.App)
    48  	if err != nil {
    49  		handleErrorResponse(c, err)
    50  		return
    51  	}
    52  
    53  	err = s.FireAfterAppUpdate(ctx, wapp.App)
    54  	if err != nil {
    55  		log.WithError(err).Error(models.ErrAppsUpdate)
    56  		c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
    57  		return
    58  	}
    59  
    60  	c.JSON(http.StatusOK, appResponse{"App successfully updated", app})
    61  }