Exemple #1
0
func (d *Postgres) addLogTrack(tx *sql.Tx, log *models.Log, track *models.Track) error {
	var name *string
	if track.Name != "" {
		name = &track.Name
	}
	query, args, dest := sqlbuilder.Insert().
		Dialect(sqlbuilder.Postgres).
		Into(`"track"`).
		Set(`"log_id"`, log.ID).
		Set(`"name"`, name).
		Set(`"start"`, track.Start).
		Set(`"end"`, track.End).
		Set(`"duration"`, track.Duration).
		Set(`"distance"`, track.Distance).
		Return(`"id"`, &track.ID).
		Build()

	if err := tx.QueryRow(query, args...).Scan(dest...); err != nil {
		return err
	}

	for _, point := range track.Points {
		if err := d.addTrackPoint(tx, track, point); err != nil {
			return err
		}
	}

	return nil
}
Exemple #2
0
func (d *Postgres) AddUserLog(user *models.User, log *models.Log) error {
	tx, err := d.db.Begin()
	if err != nil {
		return err
	}
	query, args, dest := sqlbuilder.Insert().
		Dialect(sqlbuilder.Postgres).
		Into(`"log"`).
		Set(`"user_id"`, user.ID).
		Set(`"start"`, log.Start).
		Set(`"end"`, log.End).
		Set(`"duration"`, log.Duration).
		Set(`"distance"`, log.Distance).
		Set(`"name"`, log.Name).
		Set(`"gpx"`, log.GPX).
		Return(`"id"`, &log.ID).
		Build()

	if err := tx.QueryRow(query, args...).Scan(dest...); err != nil {
		tx.Rollback()
		return err
	}

	for _, track := range log.Tracks {
		if err := d.addLogTrack(tx, log, track); err != nil {
			tx.Rollback()
			return err
		}
	}

	return tx.Commit()
}
Exemple #3
0
func (d *Postgres) addTrackPoint(tx *sql.Tx, track *models.Track, point *models.Point) error {
	query, args, dest := sqlbuilder.Insert().
		Dialect(sqlbuilder.Postgres).
		Into(`"trackpoint"`).
		Set(`"track_id"`, track.ID).
		SetSQL(`"point"`, fmt.Sprintf("point(%f,%f)", point.Longitude, point.Latitude)).
		Set(`"time"`, point.Time).
		Set(`"elevation"`, point.Elevation).
		Set(`"heartrate"`, point.Heartrate).
		Return(`"id"`, &point.ID).
		Build()

	if err := tx.QueryRow(query, args...).Scan(dest...); err != nil {
		return err
	}
	return nil
}
Exemple #4
0
func (d *Postgres) AddUser(user *models.User) error {
	var id int

	query, args, dest := sqlbuilder.Insert().
		Dialect(sqlbuilder.Postgres).
		Into(`"user"`).
		Set(`"username"`, user.Username).
		Set(`"password"`, user.Password).
		Return(`"id"`, &id).
		Build()

	err := d.db.QueryRow(query, args...).Scan(dest...)
	if err != nil {
		return err
	}

	user.ID = id
	return nil
}
Exemple #5
0
func (d *Postgres) replaceLogTags(tx *sql.Tx, log *models.Log) error {
	_, err := tx.Exec(`DELETE FROM "log_tag" WHERE "log_id" = $1`, log.ID)
	if err != nil {
		return err
	}

	for _, tag := range log.Tags {
		query, args, _ := sqlbuilder.Insert().
			Dialect(sqlbuilder.Postgres).
			Into(`"log_tag"`).
			Set(`"log_id"`, log.ID).
			Set(`"tag"`, tag).
			Build()
		_, err = tx.Exec(query, args...)
		if err != nil {
			return err
		}
	}

	return nil
}