-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from synapse-organization/locations/init-api
feaat(loactions): Init api
- Loading branch information
Showing
8 changed files
with
231 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package models | ||
|
||
type Location struct { | ||
CafeID int32 `json:"cafe_id"` | ||
Lat float64 `json:"lat"` | ||
Lng float64 `json:"lng"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package repo | ||
|
||
import ( | ||
"barista/pkg/log" | ||
"barista/pkg/models" | ||
"context" | ||
"github.com/jackc/pgx/v5/pgxpool" | ||
) | ||
|
||
type LocationsRepo interface { | ||
SetLocation(ctx context.Context, location *models.Location) error | ||
FindAll(ctx context.Context) ([]*models.Location, error) | ||
} | ||
|
||
type LocationsRepoImp struct { | ||
postgres *pgxpool.Pool | ||
} | ||
|
||
func NewLocationsRepoImp(postgres *pgxpool.Pool) *LocationsRepoImp { | ||
_, err := postgres.Exec(context.Background(), | ||
`CREATE TABLE IF NOT EXISTS locations ( | ||
id BIGINT PRIMARY KEY, | ||
latitude FLOAT, | ||
longitude FLOAT | ||
);`) | ||
if err != nil { | ||
log.GetLog().WithError(err).WithField("table", "locations").Fatal("Unable to create table") | ||
} | ||
return &LocationsRepoImp{postgres: postgres} | ||
} | ||
|
||
func (r *LocationsRepoImp) SetLocation(ctx context.Context, location *models.Location) error { | ||
_, err := r.postgres.Exec(ctx, "INSERT INTO locations (id, latitude, longitude) VALUES ($1, $2, $3) ON CONFLICT (id) DO UPDATE SET latitude = $2, longitude = $3", location.CafeID, location.Lat, location.Lng) | ||
if err != nil { | ||
log.GetLog().Errorf("Unable to insert location. error: %v", err) | ||
} | ||
return err | ||
} | ||
|
||
func (r *LocationsRepoImp) FindAll(ctx context.Context) ([]*models.Location, error) { | ||
var locations []*models.Location | ||
rows, err := r.postgres.Query(ctx, "SELECT id, latitude, longitude FROM locations") | ||
if err != nil { | ||
log.GetLog().Errorf("Unable to get locations. error: %v", err) | ||
} | ||
for rows.Next() { | ||
var location models.Location | ||
err := rows.Scan(&location.CafeID, &location.Lat, &location.Lng) | ||
if err != nil { | ||
log.GetLog().Errorf("Unable to scan location. error: %v", err) | ||
continue | ||
} | ||
locations = append(locations, &location) | ||
} | ||
return locations, err | ||
} |