Beispiel #1
0
func UpdateCoral(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)

	coral := entity.NewCoral()

	coral.Populate(getCoralMap(r, params["id"]))

	val, errors := validation.ValidateCoral(coral, db, CORAL)

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

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

			updatePopularNames(db, id, CORAL, coral.GetPopularNames().GetValues())
			createMedia(r, mediaManager, db, sessionMessages, CORAL, id)
		}

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

		ren.Redirect("/coral/" + params["id"])
	}
}
Beispiel #2
0
func CreateCoral(ren render.Render, mediaManager *media.Manager, r *http.Request, db *sql.DB, params martini.Params, sessionMessages *template.Messages) {
	coral := entity.NewCoral()

	coral.Populate(getCoralMap(r, ""))

	val, errors := validation.ValidateCoral(coral, db, CORAL)

	if val {
		res, err := repository.InsertCoral(coral, db)

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

			id, _ := res.LastInsertId()

			insertPopularNames(db, id, CORAL, coral.GetPopularNames().GetValues())
			createMedia(r, mediaManager, db, sessionMessages, CORAL, id)
		}

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

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