Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add field to location db #222

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions services/location/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ CREATE TABLE IF NOT EXISTS locations
(
id bigint unsigned NOT NULL,
name varchar(255) NOT NULL,
description varchar(255), //this is a new field
coordinates varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
Expand All @@ -54,27 +55,32 @@ CREATE TABLE IF NOT EXISTS locations
var seed = []Location{
{
ID: 1,
Name: "My Home",
Name: "My Home (see description)",
Description: "home",
Coordinates: "231,773",
},
{
ID: 123,
Name: "Rachel's Floral Designs",
Name: "Rachel's Floral Designs (see description)",
Description: "Rachel's",
Coordinates: "115,277",
},
{
ID: 567,
Name: "Amazing Coffee Roasters",
Name: "Amazing Coffee Roasters (see description)",
Description: "Coffee",
Coordinates: "211,653",
},
{
ID: 392,
Name: "Trom Chocolatier",
Name: "Trom Chocolatier (see description)",
Description: "Chocolate",
Coordinates: "577,322",
},
{
ID: 731,
Name: "Japanese Desserts",
Name: "Japanese Desserts (see description)",
Description: "Dessert",
Coordinates: "728,326",
},
}
Expand All @@ -97,13 +103,13 @@ func newDatabase(logger log.Factory) *database {
}
if count == 0 {
fmt.Println("seeding database")
stmt, err := db.Prepare("INSERT into locations (id, name, coordinates) values (?, ?, ?)")
stmt, err := db.Prepare("INSERT into locations (id, name, description, coordinates) values (?, ?, ?, ?)")
if err != nil {
panic(err)
}
for i := range seed {
c := &seed[i]
if _, err := stmt.Exec(c.ID, c.Name, c.Coordinates); err != nil {
if _, err := stmt.Exec(c.ID, c.Name, c.Description, c.Coordinates); err != nil {
panic(err)
}
}
Expand Down Expand Up @@ -149,19 +155,19 @@ func (d *database) List(ctx context.Context) ([]Location, error) {
semconv.PeerServiceKey.String("mysql"),
attribute.
Key("sql.query").
String("SELECT id, name, coordinates FROM locations"),
String("SELECT id, name, description, coordinates FROM locations"),
)
defer span.End()

rows, err := d.db.Query("SELECT id, name, coordinates FROM locations")
rows, err := d.db.Query("SELECT id, name, description, coordinates FROM locations")
if err != nil {
return nil, err
}
defer rows.Close()
var cs []Location
for rows.Next() {
c := Location{}
if err := rows.Scan(&c.ID, &c.Name, &c.Coordinates); err != nil {
if err := rows.Scan(&c.ID, &c.Name, &c.Description, &c.Coordinates); err != nil {
return nil, err
}
cs = append(cs, c)
Expand All @@ -174,8 +180,8 @@ func (d *database) List(ctx context.Context) ([]Location, error) {
}

func (d *database) Put(ctx context.Context, location *Location) error {
res, err := d.db.Exec("UPDATE locations set name = ?, coordinates = ? WHERE id = ?",
location.Name, location.Coordinates, location.ID)
res, err := d.db.Exec("UPDATE locations set name = ?, description = ?, coordinates = ? WHERE id = ?",
location.Name, location.Description, location.Coordinates, location.ID)
if err != nil {
return err
}
Expand All @@ -197,7 +203,7 @@ func (d *database) Get(ctx context.Context, locationID int) (*Location, error) {
semconv.PeerServiceKey.String("mysql"),
attribute.
Key("sql.query").
String(fmt.Sprintf("SELECT id, name, coordinates from locations where id = %d", locationID)),
String(fmt.Sprintf("SELECT id, name, description, coordinates from locations where id = %d", locationID)),
)
defer span.End()

Expand All @@ -210,8 +216,8 @@ func (d *database) Get(ctx context.Context, locationID int) (*Location, error) {
// simulate RPC delay
delay.Sleep(config.GetMySQLGetDelay(), config.GetMySQLGetDelayStdDev())
var c Location
if err := d.db.QueryRow("SELECT id, name, coordinates from locations where id = ?", locationID).
Scan(&c.ID, &c.Name, &c.Coordinates); err != nil {
if err := d.db.QueryRow("SELECT id, name, description, coordinates from locations where id = ?", locationID).
Scan(&c.ID, &c.Name, &c.Description, &c.Coordinates); err != nil {
return nil, err
}
return &c, nil
Expand Down
1 change: 1 addition & 0 deletions services/location/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
type Location struct {
ID int64
Name string
Description string
Coordinates string
}

Expand Down