func DeleteLatestNews(ren render.Render, db *sql.DB, params martini.Params, sessionMessages *template.Messages) { _, err := repository.DeleteLatestNews(db, params["id"]) if err != nil { log.Println(err) sessionMessages.AddDanger("Failed to delete news.") } else { sessionMessages.AddSuccess("Successfully deleted news.") } ren.Redirect("/latest_news") }
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 UpdateComponent(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) component := entity.NewComponent() component.Populate(getComponentMap(r, params["id"])) val, errors := validation.ValidateComponent(component, db, COMPONENT) if val { _, err := repository.UpdateComponent(component, db, params["id"]) if err != nil { log.Println(err) sessionMessages.AddDanger("Failed to update component.") } else { sessionMessages.AddSuccess("Successfully updated component.") createMedia(r, mediaManager, db, sessionMessages, COMPONENT, id) } ren.Redirect("/component") } else { for k, msg := range errors { sessionMessages.AddDanger(k + ": " + msg) } ren.Redirect("/component/" + params["id"]) } }
func CreateComponent(ren render.Render, mediaManager *media.Manager, r *http.Request, db *sql.DB, params martini.Params, sessionMessages *template.Messages) { component := entity.NewComponent() component.Populate(getComponentMap(r, "")) val, errors := validation.ValidateComponent(component, db, COMPONENT) if val { res, err := repository.InsertComponent(component, db) if err != nil { log.Println(err) sessionMessages.AddDanger("Failed to create component.") } else { sessionMessages.AddSuccess("Successfully created component.") id, _ := res.LastInsertId() createMedia(r, mediaManager, db, sessionMessages, "component", id) } ren.Redirect("/component") } else { for k, msg := range errors { sessionMessages.AddDanger(k + ": " + msg) } ren.Redirect("/component/new") } }
func DeleteComponent(ren render.Render, mediaManager *media.Manager, db *sql.DB, params martini.Params, sessionMessages *template.Messages) { var err error id, _ := strconv.ParseInt(params["id"], 10, 64) media_list := getMediaList(db, COMPONENT, id) if media_list != nil { _, errors := DeleteEntityMedia(*media_list, mediaManager, db) for _, err := range errors { log.Println(err) } _, err = repository.DeleteEntityMedia(db, id, COMPONENT) if err != nil { log.Println(err) sessionMessages.AddDanger("Failed to delete media for component.") } } _, err = repository.DeleteComponent(db, id) if err != nil { log.Println(err) sessionMessages.AddDanger("Failed to delete component.") } else { sessionMessages.AddSuccess("Successfully deleted component.") } ren.Redirect("/component") }
func (m *Manager) Upload(r *http.Request, sessionMessages *template.Messages, args ...string) map[int]string { filenames := make(map[int]string) for key, form_key := range args { if !m.hasUpload(r, form_key) { //sessionMessages.AddInfo(fmt.Sprintf("No file upload for upload field #%d", key+1)) continue } file, header, err := r.FormFile(form_key) if err != nil { sessionMessages.AddDanger(fmt.Sprintf(err.Error())) } data, err := ioutil.ReadAll(file) if err != nil { sessionMessages.AddDanger(fmt.Sprintf("Couldn't read uploaded file. #%d", key+1)) continue } content_type := header.Header.Get("Content-Type") filename := m.getFileName(data) extension := m.getExtensionName(content_type) for _, uploadHandler := range m.uploadHandlers { go uploadHandler.Upload(data, filename, extension, content_type) } filenames[key] = filename + "." + extension sessionMessages.AddSuccess(fmt.Sprintf("File is being uploaded for upload field #%d", key+1)) } return filenames }
func UpdateLatestNews(ren render.Render, r *http.Request, db *sql.DB, params martini.Params, sessionMessages *template.Messages) { latest_news := entity.NewLatestNews() latest_news.Populate(getLatestNewsMap(r)) val, errors := validation.ValidateLatestNews(latest_news, db, LATEST_NEWS) if val { _, err := repository.UpdateLatestNews(latest_news, db, params["id"]) if err != nil { log.Println(err) sessionMessages.AddDanger("Failed to upload news.") } else { sessionMessages.AddSuccess("Successfully upload news.") } ren.Redirect("/latest_news") } else { for k, msg := range errors { sessionMessages.AddDanger(k + ": " + msg) } ren.Redirect("/latest_news/" + params["id"]) } }
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"]) } }
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") } }
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"]) } }
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") } }
func DeleteInvertebrate(ren render.Render, mediaManager *media.Manager, db *sql.DB, params martini.Params, sessionMessages *template.Messages) { var err error id, _ := strconv.ParseInt(params["id"], 10, 64) media_list := getMediaList(db, INVERTEBRATE, id) if media_list != nil { _, errors := DeleteEntityMedia(*media_list, mediaManager, db) for _, err := range errors { log.Println(err) } _, err = repository.DeleteEntityMedia(db, id, INVERTEBRATE) if err != nil { log.Println(err) sessionMessages.AddDanger("Failed to delete media for invertebrate.") } } _, err = repository.DeleteEntityNames(db, id, INVERTEBRATE) if err != nil { log.Println(err) sessionMessages.AddDanger("Failed to delete popular names for invertabrate.") } _, err = repository.DeleteInvertebrate(db, id) if err != nil { log.Println(err) sessionMessages.AddDanger("Failed to delete invertebrate.") } else { sessionMessages.AddSuccess("Successfully deleted invertebrate.") } ren.Redirect("/invertebrate") }