예제 #1
0
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"])
	}
}
예제 #2
0
func GetLatestNews(db *sql.DB, latest_news_id string) (*entity.LatestNews, error) {
	defer recoverDbPanic()

	stmt := getStatement("latest_news_item", LATEST_NEWS_ITEM_QUERY, db)

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

	rows, err := stmt.Query(latest_news_id)

	defer rows.Close()

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

	latest_news := entity.NewLatestNews()
	err = rows.Scan(
		&latest_news.Id,
		&latest_news.Message,
		&latest_news.Language,
		&latest_news.ValidFrom,
		&latest_news.ValidUntil,
	)

	return latest_news, err
}
예제 #3
0
func GetLatestNewsCollection(db *sql.DB, fake_ms bool) ([]*entity.LatestNews, error) {
	defer recoverDbPanic()

	latest_news_collection := []*entity.LatestNews{}

	stmt := getStatement("latest_news_list", LATEST_NEWS_LIST_QUERY, db)

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

	rows, err := stmt.Query()

	defer rows.Close()

	if err != nil {
		return latest_news_collection, err
	}

	for rows.Next() {
		latest_news := entity.NewLatestNews()

		err := rows.Scan(
			&latest_news.Id,
			&latest_news.Message,
			&latest_news.Language,
			&latest_news.ValidFrom,
			&latest_news.ValidUntil,
		)

		if fake_ms {
			latest_news.ValidFrom *= 1000
			latest_news.ValidUntil *= 1000
		}

		if err != nil {
			return latest_news_collection, err
		}

		latest_news_collection = append(latest_news_collection, latest_news)
	}

	return latest_news_collection, err
}
예제 #4
0
func EditLatestNews(ren render.Render, db *sql.DB, params martini.Params, t *template.TemplateData) {
	latest_news_id := params["id"]

	if latest_news_id != "new" {
		latest_news, err := repository.GetLatestNews(db, latest_news_id)

		if err != nil || latest_news == nil {
			t.SessionMessage.AddDanger("Failed to load news.")

			ren.Redirect("/latest_news")

			return
		}

		t.Data = latest_news

		ren.HTML(200, "blah/latest_news/edit", t)
	} else {
		t.Data = entity.NewLatestNews()

		ren.HTML(200, "blah/latest_news/edit", t)
	}
}