示例#1
0
func handleUpdateBeerRequest(c appengine.Context, w http.ResponseWriter, r *http.Request) {

	cellarID, err := strconv.Atoi(r.PostFormValue("cellarID"))
	if err != nil {
		writeError(w, err)
		return
	}

	beerID, err := strconv.Atoi(r.PostFormValue("beerID"))
	if err != nil {
		writeError(w, err)
		return
	}

	quantity, err := strconv.Atoi(r.PostFormValue("quantity"))
	if err != nil {
		writeError(w, err)
		return
	}

	account := getAccount(c)
	if account == nil {
		writeError(w, errors.New("no account"))
		return
	}

	cellar := account.CellarsByID[cellarID]
	if cellar == nil {
		writeError(w, errors.New("Could not update beer, cellar not found"))
		return
	}

	beer := cellar.BeersByID[beerID]
	if beer == nil {
		writeError(w, errors.New("Could not update beer, beer not found"))
		return
	}

	name := r.PostFormValue("name")
	notes := r.PostFormValue("notes")

	brewed := r.PostFormValue("brewed")
	var brewDate *models.Date
	if brewed == "" {
		brewDate = models.Now()
	} else {
		brewDate = models.ParseDate(brewed)
	}

	added := r.PostFormValue("added")
	var addedDate *models.Date
	if added == "" {
		addedDate = models.Now()
	} else {
		addedDate = models.ParseDate(added)
	}

	delete(cellar.Beers, beer.Name)

	beer.Name = name
	beer.Quantity = quantity
	beer.Notes = notes
	beer.Brewed = brewDate
	beer.Added = addedDate

	cellar.Beers[beer.Name] = beer

	err = models.SaveAccount(c, account)
	if err != nil {
		writeError(w, err)
		return
	}
	writeSuccess(w, simpleBeer{
		ID:            beer.ID,
		Name:          beer.Name,
		AverageRating: 0,
		Quantity:      quantity,
		Notes:         beer.Notes,
		Brewed:        beer.Brewed.ToString(),
		Added:         beer.Added.ToString(),
		Age:           beer.GetAgeString(),
	})
}
示例#2
0
func handleUpdateTastingRequest(c appengine.Context, w http.ResponseWriter, r *http.Request) {

	cellarID, err := strconv.Atoi(r.PostFormValue("cellarID"))
	if err != nil {
		writeError(w, err)
		return
	}

	beerID, err := strconv.Atoi(r.PostFormValue("beerID"))
	if err != nil {
		writeError(w, err)
		return
	}

	tastingID, err := strconv.Atoi(r.PostFormValue("tastingID"))
	if err != nil {
		writeError(w, err)
		return
	}

	tastedDateStr := r.PostFormValue("tastedDate")
	var tastedDate *models.Date
	if tastedDateStr == "" {
		tastedDate = models.Now()
	} else {
		tastedDate = models.ParseDate(tastedDateStr)
	}

	rating, err := strconv.Atoi(r.PostFormValue("rating"))
	if err != nil {
		writeError(w, err)
		return
	}

	notes := r.PostFormValue("notes")

	account := getAccount(c)
	if account == nil {
		writeError(w, errors.New("no account"))
		return
	}

	cellar := account.CellarsByID[cellarID]
	if cellar == nil {
		writeError(w, errors.New("cellar not found"))
		return
	}

	beer := cellar.BeersByID[beerID]
	if beer == nil {
		writeError(w, errors.New("beer not found"))
		return
	}

	tasting := beer.TastingsByID[tastingID]
	if tasting == nil {
		writeError(w, errors.New("tasting not found"))
		return
	}

	tasting.Notes = notes
	tasting.Rating = rating
	tasting.Date = tastedDate

	err = models.SaveAccount(c, account)
	if err != nil {
		writeError(w, err)
		return
	}

	writeSuccess(w, simpleTasting{
		ID:            tasting.ID,
		Rating:        tasting.Rating,
		Notes:         tasting.Notes,
		TastedDate:    tasting.Date.ToString(),
		AgeTastedDate: beer.GetTastingAge(tasting),
		Quantity:      beer.Quantity,
		AverageRating: beer.GetAverageRating(),
	})
}
示例#3
0
func handleNewBeerRequest(c appengine.Context, w http.ResponseWriter, r *http.Request) {

	cellarID, err := strconv.Atoi(r.PostFormValue("cellarID"))
	if err != nil {
		writeError(w, err)
		return
	}

	quantity, err := strconv.Atoi(r.PostFormValue("quantity"))
	if err != nil {
		writeError(w, err)
		return
	}

	name := r.PostFormValue("name")
	if name == "" {
		writeError(w, errors.New("No beer supplied"))
		return
	}

	notes := r.PostFormValue("notes")

	brewed := r.PostFormValue("brewed")
	var brewDate *models.Date
	if brewed == "" {
		brewDate = models.Now()
	} else {
		brewDate = models.ParseDate(brewed)
	}

	added := r.PostFormValue("added")
	var addedDate *models.Date
	if added == "" {
		addedDate = models.Now()
	} else {
		addedDate = models.ParseDate(added)
	}

	account := getAccount(c)
	if account == nil {
		writeError(w, errors.New("no account"))
		return
	}

	cellar := account.CellarsByID[cellarID]
	if cellar == nil {
		writeError(w, errors.New("cellar not found"))
		return
	}

	ubid, err := models.GetAndIncrementUniversalBeerID(c)
	if err != nil {
		writeError(w, err)
		return
	}

	beer := &models.Beer{
		UBID:          ubid,
		ID:            cellar.NextBeerID,
		Name:          name,
		Notes:         notes,
		Brewed:        brewDate,
		Added:         addedDate,
		Quantity:      quantity,
		NextTastingID: 0,
		TastingsByID:  map[int]*models.Tasting{},
	}

	cellar.NextBeerID++

	cellar.Beers[beer.Name] = beer
	cellar.BeersByID[beer.ID] = beer

	err = models.SaveAccount(c, account)
	if err != nil {
		writeError(w, err)
		return
	}

	writeSuccess(w, simpleBeer{
		ID:            beer.ID,
		Name:          beer.Name,
		AverageRating: 0,
		Quantity:      quantity,
		Notes:         beer.Notes,
		Brewed:        beer.Brewed.ToString(),
		Added:         beer.Added.ToString(),
		Age:           beer.GetAgeString(),
	})
}