-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0785915
commit 15bbc43
Showing
12 changed files
with
261 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"db_connection": "host=postgres port=3434 user=nic password=password dbname=wizzard sslmode=disable" | ||
"db_connection": "host=localhost port=15432 user=postgres password=password dbname=products sslmode=disable" | ||
} |
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 |
---|---|---|
@@ -1,34 +1,50 @@ | ||
package data | ||
|
||
import ( | ||
"database/sql" | ||
|
||
_ "github.com/lib/pq" | ||
"github.com/hashicorp-demoapp/product-api-go/data/model" | ||
|
||
"github.com/jinzhu/gorm" | ||
_ "github.com/jinzhu/gorm/dialects/postgres" | ||
) | ||
|
||
type Connection interface { | ||
IsConnected() (bool, error) | ||
GetProducts() (model.Coffees, error) | ||
} | ||
|
||
type PostgresSQL struct { | ||
db *sql.DB | ||
db *gorm.DB | ||
} | ||
|
||
// New creates a new connection to the database | ||
func New(connection string) (Connection, error) { | ||
db, err := sql.Open("postgres", connection) | ||
db, err := gorm.Open("postgres", connection) | ||
//db, err := sql.Open("postgres", connection) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &PostgresSQL{db}, nil | ||
} | ||
|
||
// IsConnection check the connection to the database and returns an error if not connected | ||
// IsConnected checks the connection to the database and returns an error if not connected | ||
func (c *PostgresSQL) IsConnected() (bool, error) { | ||
err := c.db.Ping() | ||
err := c.db.DB().Ping() | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
// GetProducts returns all products from the database | ||
func (c*PostgresSQL) GetProducts() (model.Coffees, error) { | ||
cos := model.Coffees{} | ||
|
||
db := c.db.Find(&cos) | ||
if db.Error != nil { | ||
return nil, db.Error | ||
} | ||
|
||
return cos,nil | ||
} |
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,23 @@ | ||
package model | ||
|
||
import "github.com/jinzhu/gorm" | ||
|
||
import "encoding/json" | ||
|
||
type Coffees []Coffee | ||
|
||
// FromJSON serializes data from json | ||
func (c*Coffees) FromJSON(data []byte) error { | ||
return json.Unmarshal(data, c) | ||
} | ||
|
||
func (c*Coffees) ToJSON() ([]byte, error) { | ||
return json.Marshal(c) | ||
} | ||
|
||
type Coffee struct { | ||
gorm.Model | ||
ID int `json:"id"` | ||
Name string `json:"name"` | ||
Price float64 `json:"price"` | ||
} |
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,50 @@ | ||
package model | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"encoding/json" | ||
"testing" | ||
) | ||
|
||
func TestCoffeesDeserializeFromJSON(t*testing.T) { | ||
c := Coffees{} | ||
|
||
err := c.FromJSON([]byte(coffeesData)) | ||
assert.NoError(t, err) | ||
|
||
assert.Len(t, c, 2) | ||
assert.Equal(t, 1, c[0].ID) | ||
assert.Equal(t, 2, c[1].ID) | ||
} | ||
|
||
func TestCoffeesSerializesToJSON(t*testing.T) { | ||
c := Coffees{ | ||
Coffee{ID: 1, Name: "test", Price: 120.12}, | ||
} | ||
|
||
d, err := c.ToJSON() | ||
assert.NoError(t, err) | ||
|
||
cd := make([]map[string]interface{}, 0) | ||
err = json.Unmarshal(d, &cd) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, float64(1), cd[0]["id"]) | ||
assert.Equal(t, "test", cd[0]["name"]) | ||
assert.Equal(t, float64(120.12), cd[0]["price"]) | ||
} | ||
|
||
var coffeesData = ` | ||
[ | ||
{ | ||
"id": 1, | ||
"name": "Latte", | ||
"price": 50.0 | ||
}, | ||
{ | ||
"id": 2, | ||
"name": "Americano", | ||
"price": 30.0 | ||
} | ||
] | ||
` |
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 |
---|---|---|
@@ -1,17 +1,17 @@ | ||
CREATE TABLE coffees (id serial PRIMARY KEY, name VARCHAR (255) NOT NULL, price NUMERIC(5, 2) NOT NULL, created_on TIMESTAMP NOT NULL, updated_on TIMESTAMP NOT NULL); | ||
CREATE TABLE ingredients (id serial PRIMARY KEY, name VARCHAR (255) NOT NULL, quantity VARCHAR (50) NOT NULL, created_on TIMESTAMP NOT NULL, updated_on TIMESTAMP NOT NULL); | ||
CREATE TABLE coffee_ingredients (id serial PRIMARY KEY, coffee_id int NOT NULL, ingredient_id int NOT NULL, created_on TIMESTAMP NOT NULL, updated_on TIMESTAMP NOT NULL); | ||
CREATE TABLE coffees (id serial PRIMARY KEY, name VARCHAR (255) NOT NULL, price NUMERIC(5, 2) NOT NULL, created_at TIMESTAMP NOT NULL, updated_at TIMESTAMP NOT NULL, deleted_at TIMESTAMP); | ||
CREATE TABLE ingredients (id serial PRIMARY KEY, name VARCHAR (255) NOT NULL, quantity VARCHAR (50) NOT NULL, created_at TIMESTAMP NOT NULL, updated_at TIMESTAMP NOT NULL, deleted_at TIMESTAMP); | ||
CREATE TABLE coffee_ingredients (id serial PRIMARY KEY, coffee_id int NOT NULL, ingredient_id int NOT NULL, created_at TIMESTAMP NOT NULL, updated_at TIMESTAMP NOT NULL, deleted_at TIMESTAMP); | ||
|
||
INSERT INTO ingredients (id, name, quantity, created_on, updated_on) VALUES (1, 'Double shot espresso', '20ml', CURRENT_DATE, CURRENT_DATE); | ||
INSERT INTO ingredients (id, name, quantity, created_on, updated_on) VALUES (2, 'Semi skimmed Milk', '500ml', CURRENT_DATE, CURRENT_DATE); | ||
INSERT INTO ingredients (id, name, quantity, created_on, updated_on) VALUES (3, 'Hot Water', '500ml', CURRENT_DATE, CURRENT_DATE); | ||
INSERT INTO ingredients (id, name, quantity, created_at, updated_at) VALUES (1, 'Double shot espresso', '20ml', CURRENT_DATE, CURRENT_DATE); | ||
INSERT INTO ingredients (id, name, quantity, created_at, updated_at) VALUES (2, 'Semi skimmed Milk', '500ml', CURRENT_DATE, CURRENT_DATE); | ||
INSERT INTO ingredients (id, name, quantity, created_at, updated_at) VALUES (3, 'Hot Water', '500ml', CURRENT_DATE, CURRENT_DATE); | ||
|
||
|
||
INSERT INTO coffees (name, price, created_on, updated_on) VALUES ('Latte (Medium)', 200.00, CURRENT_DATE, CURRENT_DATE); | ||
INSERT INTO coffee_ingredients (coffee_id, Ingredient_id, created_on, updated_on) VALUES (1,1, CURRENT_DATE, CURRENT_DATE); | ||
INSERT INTO coffee_ingredients (coffee_id, Ingredient_id, created_on, updated_on) VALUES (1,2, CURRENT_DATE, CURRENT_DATE); | ||
INSERT INTO coffees (name, price, created_at, updated_at) VALUES ('Latte (Medium)', 200.00, CURRENT_DATE, CURRENT_DATE); | ||
INSERT INTO coffee_ingredients (coffee_id, Ingredient_id, created_at, updated_at) VALUES (1,1, CURRENT_DATE, CURRENT_DATE); | ||
INSERT INTO coffee_ingredients (coffee_id, Ingredient_id, created_at, updated_at) VALUES (1,2, CURRENT_DATE, CURRENT_DATE); | ||
|
||
|
||
INSERT INTO coffees (name, price, created_on, updated_on) VALUES ('Americano (Medium)', 150.00, CURRENT_DATE, CURRENT_DATE); | ||
INSERT INTO coffee_ingredients (coffee_id, Ingredient_id, created_on, updated_on) VALUES (2,1, CURRENT_DATE, CURRENT_DATE); | ||
INSERT INTO coffee_ingredients (coffee_id, Ingredient_id, created_on, updated_on) VALUES (2,3, CURRENT_DATE, CURRENT_DATE); | ||
INSERT INTO coffees (name, price, created_at, updated_at) VALUES ('Americano (Medium)', 150.00, CURRENT_DATE, CURRENT_DATE); | ||
INSERT INTO coffee_ingredients (coffee_id, Ingredient_id, created_at, updated_at) VALUES (2,1, CURRENT_DATE, CURRENT_DATE); | ||
INSERT INTO coffee_ingredients (coffee_id, Ingredient_id, created_at, updated_at) VALUES (2,3, CURRENT_DATE, CURRENT_DATE); |
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
Oops, something went wrong.