func (ctl *ApplicationController) getEditApplicationAction(c *gin.Context) {

	id := c.Param("id")

	// Get the application
	application := ctl.getApplication(c, id)
	if application == nil {
		return
	}

	// Hydrate the form
	var form models.ApplicationUpdateForm
	form.Hydrate(application)

	c.HTML(http.StatusOK, "application_edit.html", map[string]interface{}{
		"form":        form,
		"application": application,
	})
}
func (ctl *ApplicationController) postEditApplicationAction(c *gin.Context) {

	id := c.Param("id")

	// Get the application
	application := ctl.getApplication(c, id)
	if application == nil {
		return
	}

	// Hydrate the form with the request values
	var form models.ApplicationUpdateForm
	if err := c.Bind(&form); err != nil {
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}

	// Check the form values
	if err := form.Validate(application); err != nil {
		fmt.Println(err.Errors)
		c.HTML(http.StatusOK, "application_edit.html", map[string]interface{}{
			"errors":      err.Errors,
			"form":        form,
			"application": application,
		})
		return
	}

	// Update the application
	application.Update(&form)

	if application.Type == models.APPLICATION_TYPE_SERVICE {

		if len(form.Packages) == 0 {
			c.HTML(http.StatusOK, "application_edit.html", map[string]interface{}{
				"errors": errors.New(errors.Error{
					Label: "invalid_packages",
					Field: "packages",
					Text:  "Invalid packages count, min 1",
				}).Errors,
				"form":        form,
				"application": application,
			})
			return
		}

		build, err := models.BuildMapper.FetchLast(application)
		if err != nil {
			panic(err)
		}

		if build == nil {
			panic("No last build for service: " + application.Name)
		}

		build.RuntimeCfg.Dependencies = build.RuntimeCfg.Dependencies.FromString(form.Packages)

		if err := models.BuildMapper.Update(build); err != nil {
			panic(err)
		}
	} else if application.Type == models.APPLICATION_TYPE_DOCKER {
		if len(form.Image) == 0 {
			c.HTML(http.StatusOK, "application_edit.html", map[string]interface{}{
				"errors": errors.New(errors.Error{
					Label: "invalid_image",
					Field: "image",
					Text:  "Invalid image",
				}).Errors,
				"form":        form,
				"application": application,
			})
			return
		}

		build, err := models.BuildMapper.FetchLast(application)
		if err != nil {
			panic(err)
		}

		if build == nil {
			panic("No last build for service: " + application.Name)
		}

		tmpBuild := models.BuildMapper.CreateDocker(application, &form.ApplicationDockerForm)
		build.RuntimeCfg.Docker = tmpBuild.RuntimeCfg.Docker

		if err := models.BuildMapper.Update(build); err != nil {
			panic(err)
		}

	}

	// Save the application
	if err := models.ApplicationMapper.Update(application); err != nil {
		panic(err)
	}

	c.Redirect(http.StatusFound, "/application/show/"+application.Id.Hex())
}