Exemple #1
0
func notify() {
	pack := models.Pack{}
	now := time.Now().Unix()
	nowPlus30Min := now + 30*60
	nowMinus30Min := now - 30*30

	gteSlice := []string{"date|" + strconv.FormatInt(nowMinus30Min, 10)}
	lteSlice := []string{"date|" + strconv.FormatInt(nowPlus30Min, 10)}

	pack.Base.Query = make(map[string][]string)
	pack.Base.Query["gte"] = gteSlice
	pack.Base.Query["lte"] = lteSlice

	packs, err := pack.Retrieve(db)
	if err != nil {
		fmt.Println("[ERROR] ", err.Error())
		return
	}

	mailTo := []string{}
	for _, p := range packs {
		mailTo = append(mailTo, p.Email)
	}

	mail := mailer.Mail{To: mailTo, Subject: "Hora do remédio", Body: mailer.TemplatePackTime}
	b, err := json.Marshal(&mail)
	if err != nil {
		fmt.Println("[ERROR] ", err.Error())
		return
	}

	producer.Publish("send_mail", b)
}
Exemple #2
0
func retrievePacks(w http.ResponseWriter, r *http.Request) errors.Http {
	p := models.Pack{}
	if err := BuildStructFromQueryString(&p, r.URL.Query()); err != nil {
		return errors.BadRequest(err.Error())
	}

	p.Base.Query = r.URL.Query()

	packs, err := p.Retrieve(db)
	if err != nil {
		return errors.BadRequest(err.Error())
	}

	if len(packs) == 0 {
		return errors.NotFound("record not found")
	}
	rend.JSON(w, http.StatusOK, packs)

	return nil
}