Skip to content

Commit 9b8d9c7

Browse files
committed
change coords field in api
1 parent b33e884 commit 9b8d9c7

File tree

2 files changed

+63
-25
lines changed

2 files changed

+63
-25
lines changed

services/location/database.go

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package location
1818
import (
1919
"context"
2020
"fmt"
21+
"strconv"
22+
"strings"
2123
"time"
2224

2325
"go.opentelemetry.io/otel/attribute"
@@ -55,29 +57,29 @@ CREATE TABLE IF NOT EXISTS locations
5557

5658
var seed = []Location{
5759
{
58-
ID: 1,
59-
Name: "My Home",
60-
Coordinates: "231,773",
60+
ID: 1,
61+
Name: "My Home",
62+
//Coordinates: "231,773",
6163
},
6264
{
63-
ID: 123,
64-
Name: "Rachel's Floral Designs",
65-
Coordinates: "115,277",
65+
ID: 123,
66+
Name: "Rachel's Floral Designs",
67+
//Coordinates: "115,277",
6668
},
6769
{
68-
ID: 567,
69-
Name: "Amazing Coffee Roasters",
70-
Coordinates: "211,653",
70+
ID: 567,
71+
Name: "Amazing Coffee Roasters",
72+
//Coordinates: "211,653",
7173
},
7274
{
73-
ID: 392,
74-
Name: "Trom Chocolatier",
75-
Coordinates: "577,322",
75+
ID: 392,
76+
Name: "Trom Chocolatier",
77+
//Coordinates: "577,322",
7678
},
7779
{
78-
ID: 731,
79-
Name: "Japanese Desserts",
80-
Coordinates: "728,326",
80+
ID: 731,
81+
Name: "Japanese Desserts",
82+
//Coordinates: "728,326",
8183
},
8284
}
8385

@@ -155,9 +157,26 @@ func (d *database) List(ctx context.Context) ([]Location, error) {
155157
var cs []Location
156158
for rows.Next() {
157159
c := Location{}
158-
if err := rows.Scan(&c.ID, &c.Name, &c.Coordinates); err != nil {
160+
coords := ""
161+
162+
if err := rows.Scan(&c.ID, &c.Name, &coords); err != nil {
159163
return nil, err
160164
}
165+
sLong, sLat, ok := strings.Cut(coords, ",")
166+
if !ok {
167+
panic("not ok")
168+
}
169+
long, err := strconv.Atoi(sLong)
170+
if err != nil {
171+
panic(err)
172+
}
173+
c.Longitude = long
174+
lat, err := strconv.Atoi(sLat)
175+
if err != nil {
176+
panic(err)
177+
}
178+
c.Latitue = lat
179+
161180
cs = append(cs, c)
162181
}
163182
if err := rows.Err(); err != nil {
@@ -169,12 +188,13 @@ func (d *database) List(ctx context.Context) ([]Location, error) {
169188

170189
func (d *database) Create(ctx context.Context, location *Location) (int64, error) {
171190
query := "INSERT INTO locations SET name = ?, coordinates = ?"
172-
res, err := d.db.Exec(query, location.Name, location.Coordinates)
191+
coords := fmt.Sprintf("%d,%d", location.Longitude, location.Latitue)
192+
res, err := d.db.Exec(query, location.Name, coords)
173193
if err != nil {
174194
if !d.shouldRetry(err) {
175195
return 0, err
176196
}
177-
res, err = d.db.Exec(query, location.Name, location.Coordinates)
197+
res, err = d.db.Exec(query, location.Name, coords)
178198
if err != nil {
179199
return 0, err
180200
}
@@ -188,12 +208,13 @@ func (d *database) Create(ctx context.Context, location *Location) (int64, error
188208

189209
func (d *database) Update(ctx context.Context, location *Location) error {
190210
query := "UPDATE locations SET name = ?, coordinates = ? WHERE id = ?"
191-
res, err := d.db.Exec(query, location.Name, location.Coordinates, location.ID)
211+
coords := fmt.Sprintf("%d,%d", location.Longitude, location.Latitue)
212+
res, err := d.db.Exec(query, location.Name, coords, location.ID)
192213
if err != nil {
193214
if !d.shouldRetry(err) {
194215
return err
195216
}
196-
res, err = d.db.Exec(query, location.Name, location.Coordinates, location.ID)
217+
res, err = d.db.Exec(query, location.Name, coords, location.ID)
197218
if err != nil {
198219
return err
199220
}
@@ -241,9 +262,24 @@ func (d *database) Get(ctx context.Context, locationID int) (*Location, error) {
241262
return nil, row.Err()
242263
}
243264
}
244-
if err := row.Scan(&c.ID, &c.Name, &c.Coordinates); err != nil {
265+
coords := ""
266+
if err := row.Scan(&c.ID, &c.Name, coords); err != nil {
245267
return nil, err
246268
}
269+
sLong, sLat, ok := strings.Cut(coords, ",")
270+
if !ok {
271+
panic("not ok")
272+
}
273+
long, err := strconv.Atoi(sLong)
274+
if err != nil {
275+
panic(err)
276+
}
277+
c.Longitude = long
278+
lat, err := strconv.Atoi(sLat)
279+
if err != nil {
280+
panic(err)
281+
}
282+
c.Latitue = lat
247283
return &c, nil
248284
}
249285

@@ -286,7 +322,8 @@ func (d *database) setupDB() {
286322
}
287323
for i := range seed {
288324
c := &seed[i]
289-
if _, err := stmt.Exec(c.ID, c.Name, c.Coordinates); err != nil {
325+
coords := fmt.Sprintf("%d,%d", c.Longitude, c.Latitue)
326+
if _, err := stmt.Exec(c.ID, c.Name, coords); err != nil {
290327
panic(err)
291328
}
292329
}

services/location/interface.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ import (
2121

2222
// Location contains data about a location.
2323
type Location struct {
24-
ID int64 `json:"idx"`
25-
Name string `json:"name"`
26-
Coordinates string `json:"coordinates"`
24+
ID int64 `json:"id"`
25+
Name string `json:"name"`
26+
Longitude int `json:"longitude"`
27+
Latitue int `json:"latitude"`
2728
}
2829

2930
// Interface exposed by the Location service.

0 commit comments

Comments
 (0)