我试图在带有 mySQL 后端的大猩猩会话中为我的模型保存一个结构,但当我尝试检索它时,venueID 只得到 0。我可以毫不费力地保存和获取即显消息。我的目标是在会话中保存模型结构并检索它以获取编辑、更新和删除功能中的 ID 号。
这是我的代码:
type appResource struct {
tmpl *template.Template // net/http
store *mysqlstore.MySQLStore
db *sql.DB // database/sql
}
// newAppResource function to pass global var
func newAppResource(store *mysqlstore.MySQLStore, db *sql.DB, tmpl *template.Template) *appResource {
return &appResource{
store: store,
db: db,
tmpl: tmpl,
}
}
func main() {
sessionKey := os.Getenv("sessionKey")
endpoint := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true&loc=Local", dbUser, dbPass, dbHost, dbPort, dbName)
tableName := "sessions"
path := "/"
maxAge := 3600
codecs := []byte(sessionKey)
store, err := mysqlstore.NewMySQLStore(endpoint, tableName, path, maxAge, codecs)
if err != nil {
log.Println("SESSIONS STORE error")
log.Fatal(err)
....
}
}
type Venue struct {
VenueID int
Name string
Email string
Phone string
Active bool
}
// VenueData template variable for show and edit
type VenueData struct {
Venue Venue
Flashes []interface{}
}
func (rs *appResource) venuesShow(w http.ResponseWriter, r *http.Request) {
var venue Venue
var data VenueData
id := r.URL.Query().Get("id")
venueID, err := strconv.Atoi(id)
if err != nil {
log.Println("show venue ID not > 0")
http.Redirect(w, r, "/login", http.StatusUnauthorized)
return
}
if !(venueID > 0) {
log.Println("update venue ID not > 0")
http.Redirect(w, r, "/login", http.StatusUnauthorized)
return
}
query, err := rs.db.Query("SELECT id, name, email, phone, active FROM Venues WHERE id=?", venueID)
if err != nil {
log.Fatal(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
呼唤远方
相关分类