// SqlRepository returns a new sqlRepository or panics if it cannot func SqlRepository() TokenRepository { settings := settings.NewSettings("settings.json") db, err := sqlx.Connect(settings.DriverName(), settings.DataSource()) if err != nil { panic("Error connecting to db: " + err.Error()) } repo := &sqlRepository{ db: db, } datetime := "datetime" if settings.DriverName() == "postgres" { datetime = "timestamp with time zone" } schema := fmt.Springf(`CREATE TABLE IF NOT EXISTS token ( token text not null primary key, platform text NULL, user text NULL, created %s, modified %s );`, datetime, datetime) _, err = repo.db.Exec(schema) return repo }
func (r *sqlRepository) Push(users []string, message string, cert string, key string) error { tokens := []*Token{} for i := range users { users[i] = "'" + users[i] + "'" } statement := fmt.Springf("SELECT * FROM token WHERE user IN (%s)", strings.Join(users, ", ")) err := r.db.Select(&tokens, statement) if err != nil { return err } payload := apns.NewPayload() payload.Alert = message payload.Badge = 1 // TODO: Make this more accurate payload.Sound = "bingbong.aiff" client := apns.NewClient("gateway.push.apple.com:2195", cert, key) for _, token := range tokens { pn := apns.NewPushNotification() pn.DeviceToken = token.Token pn.AddPayload(payload) resp := client.Send(pn) alert, _ := pn.PayloadString() if resp.Error != nil { log.Println("APNS Error: ", resp.Error) } else { log.Println("APNS Alert: ", alert) log.Println("APNS Success: ", resp.Success) } } return nil }
func (r *sqlRepository) Delete(token string) error { statement := fmt.Springf("DELETE FROM token WHERE token = '%s'", token) _, err := r.db.Exec(statement) return err }
func (r *sqlRepository) Get(user string) ([]*Token, error) { tokens := []*Token{} statement := fmt.Springf("SELECT * FROM token WHERE user = '******'", user) err := r.db.Select(&tokens, statement) return tokens, err }