Example #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"])
	}
}
Example #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")
	}
}
Example #3
0
func GetCorals(db *sql.DB) ([]*entity.Coral, error) {
	defer recoverDbPanic()

	corals := []*entity.Coral{}

	stmt := getStatement("coral_list", CORAL_LIST_QUERY, db)

	if stmt == nil {
		return corals, errors.New("Query could not be created.")
	}

	rows, err := stmt.Query()

	defer rows.Close()

	if err != nil {
		return corals, err
	}

	for rows.Next() {
		coral := entity.NewCoral()

		err := rows.Scan(
			&coral.Id,
			&coral.Name,
			&coral.Family,
			&coral.WaterConditions.Temperature.Min,
			&coral.WaterConditions.Temperature.Max,
			&coral.WaterConditions.CarbonateHardness.Min,
			&coral.WaterConditions.CarbonateHardness.Max,
			&coral.WaterConditions.Calcium.Min,
			&coral.WaterConditions.Calcium.Max,
			&coral.WaterConditions.Magnesium.Min,
			&coral.WaterConditions.Magnesium.Max,
			&coral.WaterConditions.No3.Min,
			&coral.WaterConditions.No3.Max,
			&coral.WaterConditions.Po4.Min,
			&coral.WaterConditions.Po4.Max,
			&coral.GrowthHeight,
			&coral.Flow,
			&coral.Toxic,
			&coral.LightRequirement,
			&coral.Difficulty,
			&coral.GrowthRate,
		)

		if err != nil {
			return corals, err
		}

		corals = append(corals, coral)
	}

	return corals, err
}
Example #4
0
func GetCoral(db *sql.DB, coral_id string) (*entity.Coral, error) {
	defer recoverDbPanic()

	stmt := getStatement("coral_item", CORAL_ITEM_QUERY, db)

	if stmt == nil {
		return nil, errors.New("Query could not be created.")
	}

	rows, err := stmt.Query(coral_id)

	defer rows.Close()

	if err != nil || !rows.Next() {
		return nil, err
	}

	coral := entity.NewCoral()
	err = rows.Scan(
		&coral.Id,
		&coral.Name,
		&coral.Family,
		&coral.WaterConditions.Temperature.Min,
		&coral.WaterConditions.Temperature.Max,
		&coral.WaterConditions.CarbonateHardness.Min,
		&coral.WaterConditions.CarbonateHardness.Max,
		&coral.WaterConditions.Calcium.Min,
		&coral.WaterConditions.Calcium.Max,
		&coral.WaterConditions.Magnesium.Min,
		&coral.WaterConditions.Magnesium.Max,
		&coral.WaterConditions.No3.Min,
		&coral.WaterConditions.No3.Max,
		&coral.WaterConditions.Po4.Min,
		&coral.WaterConditions.Po4.Max,
		&coral.GrowthHeight,
		&coral.Flow,
		&coral.Toxic,
		&coral.LightRequirement,
		&coral.Difficulty,
		&coral.GrowthRate,
	)

	return coral, err
}