Example #1
0
func UpdateInhabitant(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)

	inhabitant := entity.NewInhabitant()

	inhabitant.Populate(getInhabitantMap(r, params["id"]))

	val, errors := validation.ValidateDefaultEntity(inhabitant, db, INHABITANT)

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

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

			updatePopularNames(db, id, INHABITANT, inhabitant.GetPopularNames().GetValues())
			createMedia(r, mediaManager, db, sessionMessages, INHABITANT, id)
		}

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

		ren.Redirect("/inhabitant/" + params["id"])
	}
}
Example #2
0
func CreateInhabitant(ren render.Render, mediaManager *media.Manager, r *http.Request, db *sql.DB, sessionMessages *template.Messages) {
	inhabitant := entity.NewInhabitant()

	inhabitant.Populate(getInhabitantMap(r, ""))

	val, errors := validation.ValidateDefaultEntity(inhabitant, db, INHABITANT)

	if val {
		res, err := repository.InsertInhabitant(inhabitant, db)

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

			id, _ := res.LastInsertId()

			insertPopularNames(db, id, INHABITANT, inhabitant.GetPopularNames().GetValues())
			createMedia(r, mediaManager, db, sessionMessages, INHABITANT, id)
		}

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

		ren.Redirect("/inhabitant/new")
	}
}
Example #3
0
func GetInhabitants(db *sql.DB) ([]*entity.Inhabitant, error) {
	defer recoverDbPanic()

	inhabitants := []*entity.Inhabitant{}

	stmt := getStatement("inhabitant_list", INHABITANT_LIST_QUERY, db)

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

	rows, err := stmt.Query()

	defer rows.Close()

	if err != nil {
		return inhabitants, err
	}

	for rows.Next() {
		inhabitant := entity.NewInhabitant()

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

		if err != nil {
			return inhabitants, err
		}

		inhabitants = append(inhabitants, inhabitant)
	}

	return inhabitants, err
}
Example #4
0
func GetInhabitant(db *sql.DB, inhabitant_id string) (*entity.Inhabitant, error) {
	defer recoverDbPanic()

	stmt := getStatement("inhabitant_item", INHABITANT_ITEM_QUERY, db)

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

	rows, err := stmt.Query(inhabitant_id)

	defer rows.Close()

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

	inhabitant := entity.NewInhabitant()
	err = rows.Scan(
		&inhabitant.Id,
		&inhabitant.Name,
		&inhabitant.Family,
		&inhabitant.MaxLength,
		&inhabitant.RequiredEdgeLength,
		&inhabitant.SocialBehavior,
		&inhabitant.WaterConditions.Ph.Min,
		&inhabitant.WaterConditions.Ph.Max,
		&inhabitant.WaterConditions.Temperature.Min,
		&inhabitant.WaterConditions.Temperature.Max,
		&inhabitant.WaterConditions.TotalHardness.Min,
		&inhabitant.WaterConditions.TotalHardness.Max,
		&inhabitant.WaterConditions.CarbonateHardness.Min,
		&inhabitant.WaterConditions.CarbonateHardness.Max,
		&inhabitant.WaterType,
	)

	return inhabitant, err
}