Skip to content

Commit

Permalink
Start to implement database
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasjackson committed Jan 6, 2020
1 parent 0785915 commit 15bbc43
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 25 deletions.
2 changes: 1 addition & 1 deletion conf.json
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"
}
30 changes: 23 additions & 7 deletions data/connection.go
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
}
15 changes: 14 additions & 1 deletion data/mockcon.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package data

import "github.com/stretchr/testify/mock"
import (
"github.com/stretchr/testify/mock"
"github.com/hashicorp-demoapp/product-api-go/data/model"
)

type MockConnection struct {
mock.Mock
Expand All @@ -9,3 +12,13 @@ type MockConnection struct {
func (c *MockConnection) IsConnected() (bool, error) {
return true, nil
}

func (c*MockConnection) GetProducts() (model.Coffees, error) {
args := c.Called()

if m, ok := args.Get(0).(model.Coffees); ok {
return m, args.Error(1)
}

return nil, args.Error(1)
}
23 changes: 23 additions & 0 deletions data/model/coffee.go
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"`
}
50 changes: 50 additions & 0 deletions data/model/coffee_test.go
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
}
]
`
24 changes: 12 additions & 12 deletions database/products.sql
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);
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/fsnotify/fsnotify v1.4.7
github.com/gorilla/mux v1.7.3
github.com/hashicorp/go-hclog v0.10.0
github.com/jinzhu/gorm v1.9.11
github.com/lib/pq v1.2.0
github.com/nicholasjackson/env v0.5.0
github.com/stretchr/testify v1.4.0
Expand Down
Loading

0 comments on commit 15bbc43

Please sign in to comment.