Esempio n. 1
0
func UpdateComponent(ren render.Render, mediaManager *media.Manager, r *http.Request, db *sql.DB, params martini.Params, sessionMessages *template.Messages) {
	id, _ := strconv.ParseInt(params["id"], 10, 64)

	component := entity.NewComponent()

	component.Populate(getComponentMap(r, params["id"]))

	val, errors := validation.ValidateComponent(component, db, COMPONENT)

	if val {
		_, err := repository.UpdateComponent(component, db, params["id"])

		if err != nil {
			log.Println(err)
			sessionMessages.AddDanger("Failed to update component.")
		} else {
			sessionMessages.AddSuccess("Successfully updated component.")

			createMedia(r, mediaManager, db, sessionMessages, COMPONENT, id)
		}

		ren.Redirect("/component")
	} else {
		for k, msg := range errors {
			sessionMessages.AddDanger(k + ": " + msg)
		}

		ren.Redirect("/component/" + params["id"])
	}
}
Esempio n. 2
0
func CreateComponent(ren render.Render, mediaManager *media.Manager, r *http.Request, db *sql.DB, params martini.Params, sessionMessages *template.Messages) {
	component := entity.NewComponent()

	component.Populate(getComponentMap(r, ""))

	val, errors := validation.ValidateComponent(component, db, COMPONENT)

	if val {
		res, err := repository.InsertComponent(component, db)

		if err != nil {
			log.Println(err)
			sessionMessages.AddDanger("Failed to create component.")
		} else {
			sessionMessages.AddSuccess("Successfully created component.")

			id, _ := res.LastInsertId()

			createMedia(r, mediaManager, db, sessionMessages, "component", id)
		}

		ren.Redirect("/component")
	} else {
		for k, msg := range errors {
			sessionMessages.AddDanger(k + ": " + msg)
		}

		ren.Redirect("/component/new")
	}
}