func UpdatePlant(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) plant := entity.NewPlant() plant.Populate(getPlantMap(r, params["id"])) val, errors := validation.ValidateDefaultEntity(plant, db, PLANT) if val { _, err := repository.UpdatePlant(plant, db, params["id"]) if err != nil { log.Println(err) sessionMessages.AddDanger("Failed to update plant.") } else { sessionMessages.AddSuccess("Successfully update plant.") updatePopularNames(db, id, PLANT, plant.GetPopularNames().GetValues()) createMedia(r, mediaManager, db, sessionMessages, PLANT, id) } ren.Redirect("/plant/") } else { for k, msg := range errors { sessionMessages.AddDanger(k + ": " + msg) } ren.Redirect("/plant/" + params["id"]) } }
func CreatePlant(ren render.Render, mediaManager *media.Manager, r *http.Request, db *sql.DB, params martini.Params, sessionMessages *template.Messages) { plant := entity.NewPlant() plant.Populate(getPlantMap(r, "")) val, errors := validation.ValidateDefaultEntity(plant, db, PLANT) if val { res, err := repository.InsertPlant(plant, db) if err != nil { log.Println(err) sessionMessages.AddDanger("Failed to create plant.") } else { sessionMessages.AddSuccess("Successfully created plant.") id, _ := res.LastInsertId() insertPopularNames(db, id, PLANT, plant.GetPopularNames().GetValues()) createMedia(r, mediaManager, db, sessionMessages, PLANT, id) } ren.Redirect("/plant") } else { for k, msg := range errors { sessionMessages.AddDanger(k + ": " + msg) } ren.Redirect("/plant/new") } }
func GetPlants(db *sql.DB) ([]*entity.Plant, error) { defer recoverDbPanic() plants := []*entity.Plant{} stmt := getStatement("plant_list", PLANT_LIST_QUERY, db) if stmt == nil { return plants, errors.New("Query could not be created.") } rows, err := stmt.Query() defer rows.Close() if err != nil { return plants, err } for rows.Next() { plant := entity.NewPlant() plant.MediaList = []entity.Media{} err = rows.Scan( &plant.Id, &plant.Name, &plant.Family, &plant.WaterConditions.Ph.Min, &plant.WaterConditions.Ph.Max, &plant.WaterConditions.Temperature.Min, &plant.WaterConditions.Temperature.Max, &plant.WaterConditions.TotalHardness.Min, &plant.WaterConditions.TotalHardness.Max, &plant.WaterConditions.CarbonateHardness.Min, &plant.WaterConditions.CarbonateHardness.Max, &plant.Difficulty, &plant.GrowthRate, &plant.GrowthHeight, &plant.LightRequirement, &plant.Position, &plant.AquaristicIncapable, &plant.WaterType, ) if err != nil { return plants, err } plants = append(plants, plant) } return plants, err }
func GetPlant(db *sql.DB, plant_id string) (*entity.Plant, error) { defer recoverDbPanic() stmt := getStatement("plant_item", PLANT_ITEM_QUERY, db) if stmt == nil { return nil, errors.New("Query could not be created.") } rows, err := stmt.Query(plant_id) defer rows.Close() if err != nil || !rows.Next() { return nil, err } plant := entity.NewPlant() err = rows.Scan( &plant.Id, &plant.Name, &plant.Family, &plant.WaterConditions.Ph.Min, &plant.WaterConditions.Ph.Max, &plant.WaterConditions.Temperature.Min, &plant.WaterConditions.Temperature.Max, &plant.WaterConditions.TotalHardness.Min, &plant.WaterConditions.TotalHardness.Max, &plant.WaterConditions.CarbonateHardness.Min, &plant.WaterConditions.CarbonateHardness.Max, &plant.Difficulty, &plant.GrowthRate, &plant.GrowthHeight, &plant.LightRequirement, &plant.Position, &plant.AquaristicIncapable, &plant.WaterType, ) return plant, err }