Exemplo n.º 1
0
func UpdateInvertebrate(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)

	invertebrate := entity.NewInvertebrate()

	invertebrate.Populate(getInvertebrateMap(r, params["id"]))

	val, errors := validation.ValidateDefaultEntity(invertebrate, db, INVERTEBRATE)

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

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

			updatePopularNames(db, id, INVERTEBRATE, invertebrate.GetPopularNames().GetValues())
			createMedia(r, mediaManager, db, sessionMessages, INVERTEBRATE, id)
		}

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

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

	invertebrate.Populate(getInvertebrateMap(r, ""))

	val, errors := validation.ValidateDefaultEntity(invertebrate, db, INVERTEBRATE)

	if val {
		res, err := repository.InsertInvertebrate(invertebrate, db)

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

			id, _ := res.LastInsertId()

			insertPopularNames(db, id, INVERTEBRATE, invertebrate.GetPopularNames().GetValues())
			createMedia(r, mediaManager, db, sessionMessages, INVERTEBRATE, id)
		}

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

		ren.Redirect("/invertebrate/new")
	}
}
Exemplo n.º 3
0
func GetInvertebrates(db *sql.DB) ([]*entity.Invertebrate, error) {
	defer recoverDbPanic()

	invertebrates := []*entity.Invertebrate{}

	stmt := getStatement("invertebrate_list", INVERTEBRATE_LIST_QUERY, db)

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

	rows, err := stmt.Query()

	defer rows.Close()

	if err != nil {
		return invertebrates, err
	}

	for rows.Next() {
		invertebrate := entity.NewInvertebrate()

		err := rows.Scan(
			&invertebrate.Id,
			&invertebrate.Name,
			&invertebrate.Family,
			&invertebrate.MaxLength,
			&invertebrate.WaterConditions.Ph.Min,
			&invertebrate.WaterConditions.Ph.Max,
			&invertebrate.WaterConditions.Temperature.Min,
			&invertebrate.WaterConditions.Temperature.Max,
			&invertebrate.WaterConditions.TotalHardness.Min,
			&invertebrate.WaterConditions.TotalHardness.Max,
			&invertebrate.WaterConditions.CarbonateHardness.Min,
			&invertebrate.WaterConditions.CarbonateHardness.Max,
			&invertebrate.WaterType,
		)

		if err != nil {
			return invertebrates, nil
		}

		invertebrates = append(invertebrates, invertebrate)
	}

	return invertebrates, err
}
Exemplo n.º 4
0
func GetInvertebrate(db *sql.DB, invertebrate_id string) (*entity.Invertebrate, error) {
	defer recoverDbPanic()

	stmt := getStatement("invertebrate_item", INVERTEBRATE_ITEM_QUERY, db)

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

	rows, err := stmt.Query(invertebrate_id)

	defer rows.Close()

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

	invertebrate := entity.NewInvertebrate()
	err = rows.Scan(
		&invertebrate.Id,
		&invertebrate.Name,
		&invertebrate.Family,
		&invertebrate.MaxLength,
		&invertebrate.WaterConditions.Ph.Min,
		&invertebrate.WaterConditions.Ph.Max,
		&invertebrate.WaterConditions.Temperature.Min,
		&invertebrate.WaterConditions.Temperature.Max,
		&invertebrate.WaterConditions.TotalHardness.Min,
		&invertebrate.WaterConditions.TotalHardness.Max,
		&invertebrate.WaterConditions.CarbonateHardness.Min,
		&invertebrate.WaterConditions.CarbonateHardness.Max,
		&invertebrate.WaterType,
	)

	return invertebrate, err
}