Skip to content

Commit ae7f640

Browse files
author
Jeff Mataya
committed
WIP
1 parent b593a4e commit ae7f640

File tree

12 files changed

+262
-13
lines changed

12 files changed

+262
-13
lines changed

remote/controllers/channels.go

+31-7
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,53 @@ package controllers
33
import (
44
"net/http"
55

6+
"github.com/FoxComm/highlander/remote/db"
67
"github.com/FoxComm/highlander/remote/models/phoenix"
78
"github.com/FoxComm/highlander/remote/payloads"
89
"github.com/FoxComm/highlander/remote/responses"
10+
"github.com/jinzhu/gorm"
911
)
1012

13+
type Channels struct {
14+
phxDB *gorm.DB
15+
}
16+
17+
func NewChannels(phxDB *gorm.DB) *Channels {
18+
return &Channels{phxDB: phxDB}
19+
}
20+
1121
// GetChannel finds a single channel by its ID.
12-
func GetChannel(id int) ControllerFunc {
22+
func (ctrl *Channels) GetChannel(id int) ControllerFunc {
1323
return func() *responses.Response {
14-
channel := phoenix.Channel{
15-
ID: 1,
16-
Name: "The Perfect Gourmet",
17-
PurchaseLocation: 1,
24+
channel := &phoenix.Channel{}
25+
26+
err := db.FindChannelByID(ctrl.phxDB, id, channel)
27+
if err != nil {
28+
if err.Error() == "record not found" {
29+
return &responses.Response{
30+
StatusCode: http.StatusNotFound,
31+
Errs: []error{err},
32+
}
33+
}
34+
35+
return &responses.Response{
36+
StatusCode: http.StatusInternalServerError,
37+
Errs: []error{err},
38+
}
1839
}
1940

41+
chResp := &responses.Channel{}
42+
chResp.Build(channel)
43+
2044
return &responses.Response{
2145
StatusCode: http.StatusOK,
22-
Body: channel,
46+
Body: chResp,
2347
}
2448
}
2549
}
2650

2751
// CreateChannel creates a new channel.
28-
func CreateChannel(payload *payloads.CreateChannel) ControllerFunc {
52+
func (ctrl *Channels) CreateChannel(payload *payloads.CreateChannel) ControllerFunc {
2953
return func() *responses.Response {
3054
return &responses.Response{
3155
StatusCode: http.StatusCreated,

remote/controllers/controllers.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
package controllers
22

33
import (
4+
"log"
5+
6+
"github.com/FoxComm/highlander/remote/db"
7+
"github.com/FoxComm/highlander/remote/utils"
48
"github.com/labstack/echo"
59
)
610

711
// Start initializes all the controllers and routes.
812
func Start() {
13+
config, err := utils.NewConfig()
14+
if err != nil {
15+
log.Fatal(err)
16+
}
17+
18+
phxDB, err := db.NewPhoenixConnection(config)
19+
if err != nil {
20+
log.Fatal(err)
21+
}
22+
23+
defer phxDB.Close()
24+
channelsCtrl := NewChannels(phxDB)
25+
926
e := echo.New()
1027

1128
e.Use(func(h echo.HandlerFunc) echo.HandlerFunc {
@@ -18,7 +35,7 @@ func Start() {
1835
e.GET("/v1/public/channels/:id", func(c echo.Context) error {
1936
fc := c.(*FoxContext)
2037
id := fc.ParamInt("id")
21-
return fc.Run(GetChannel(id))
38+
return fc.Run(channelsCtrl.GetChannel(id))
2239
})
2340

2441
e.Start(":9898")

remote/glide.lock

+8-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

remote/glide.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ package: github.com/FoxComm/highlander/remote
22
import:
33
- package: github.com/labstack/echo
44
- package: github.com/lib/pq
5+
- package: github.com/jinzhu/gorm
6+
- package: github.com/jinzhu/inflection

remote/models/phoenix/channel.go

+52-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package phoenix
22

3-
import "time"
3+
import (
4+
"errors"
5+
"fmt"
6+
"time"
7+
)
48

59
// Channel represents an avenue for purchasing on the Fox Platform. This could
610
// be a website (theperfectgourmet.com), third-party (Amazon), or sale type (B2B).
@@ -11,3 +15,50 @@ type Channel struct {
1115
CreatedAt time.Time
1216
UpdatedAt time.Time
1317
}
18+
19+
func NewChannel(name string, purchaseLocation int) *Channel {
20+
return &Channel{
21+
ID: 0,
22+
Name: name,
23+
PurchaseLocation: purchaseLocation,
24+
CreatedAt: time.Now().UTC(),
25+
UpdatedAt: time.Now().UTC(),
26+
}
27+
}
28+
29+
func (c Channel) Table() string {
30+
return "channels"
31+
}
32+
33+
func (c Channel) Fields() map[string]interface{} {
34+
return map[string]interface{}{
35+
"name": c.Name,
36+
"purchase_location": c.PurchaseLocation,
37+
"created_at": c.CreatedAt,
38+
"updated_at": c.UpdatedAt,
39+
}
40+
}
41+
42+
func (c Channel) FieldRefs() []interface{} {
43+
return []interface{}{
44+
&c.ID,
45+
&c.Name,
46+
&c.PurchaseLocation,
47+
&c.CreatedAt,
48+
&c.UpdatedAt,
49+
}
50+
}
51+
52+
func (c Channel) Validate() error {
53+
if c.Name == "" {
54+
return errors.New("Channel name must not be empty")
55+
} else if c.PurchaseLocation != PurchaseOnFox && c.PurchaseLocation != PurchaseOffFox {
56+
return fmt.Errorf(
57+
"Expected PurchaseLocation to be %d or %d, got %d",
58+
PurchaseOnFox,
59+
PurchaseOffFox,
60+
c.PurchaseLocation)
61+
}
62+
63+
return nil
64+
}

remote/payloads/channel_payloads.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package payloads
22

3-
import "github.com/FoxComm/highlander/remote/models/phoenix"
3+
import (
4+
"time"
5+
6+
"github.com/FoxComm/highlander/remote/models/phoenix"
7+
)
48

59
// CreateChannel is the structure of payload needed to create a channel.
610
type CreateChannel struct {
@@ -11,7 +15,11 @@ type CreateChannel struct {
1115

1216
// PhoenixModel returns the phoenix model for this payload.
1317
func (c CreateChannel) PhoenixModel() *phoenix.Channel {
14-
model := &phoenix.Channel{Name: c.Name}
18+
model := &phoenix.Channel{
19+
Name: c.Name,
20+
CreatedAt: time.Now().UTC(),
21+
UpdatedAt: time.Now().UTC(),
22+
}
1523

1624
if c.PurchaseOnFox {
1725
model.PurchaseLocation = phoenix.PurchaseOnFox

remote/responses/channel.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package responses
2+
3+
import (
4+
"time"
5+
6+
"github.com/FoxComm/highlander/remote/models/phoenix"
7+
)
8+
9+
type Channel struct {
10+
ID int `json:"id"`
11+
Name string `json:"name"`
12+
PurchaseOnFox bool `json:"purchaseOnFox"`
13+
CreatedAt time.Time `json:"createdAt"`
14+
UpdatedAt time.Time `json:"updatedAt"`
15+
}
16+
17+
func (c *Channel) Build(phxChannel *phoenix.Channel) {
18+
c.ID = phxChannel.ID
19+
c.Name = phxChannel.Name
20+
c.CreatedAt = phxChannel.CreatedAt
21+
c.UpdatedAt = phxChannel.UpdatedAt
22+
23+
if phxChannel.PurchaseLocation == phoenix.PurchaseOnFox {
24+
c.PurchaseOnFox = true
25+
} else {
26+
c.PurchaseOnFox = false
27+
}
28+
}

remote/responses/response.go

+8
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,11 @@ type Response struct {
66
Body interface{}
77
Errs []error
88
}
9+
10+
func NewErrorResponse(err error) *Response {
11+
if err.Error() == "record not found" {
12+
return nil
13+
}
14+
15+
return nil
16+
}

remote/services/connection.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package services
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/FoxComm/highlander/remote/utils"
7+
"github.com/jinzhu/gorm"
8+
_ "github.com/lib/pq"
9+
)
10+
11+
func NewPhoenixConnection(config *utils.Config) (*gorm.DB, error) {
12+
connStr := fmt.Sprintf(
13+
"host=%s user=%s password=%s dbname=%s sslmode=%s",
14+
config.PhxDatabaseHost,
15+
config.PhxDatabaseUser,
16+
config.PhxDatabasePassword,
17+
config.PhxDatabaseName,
18+
config.PhxDatabaseSSL)
19+
20+
phxDB, err := gorm.Open("postgres", connStr)
21+
if err != nil {
22+
return nil, fmt.Errorf("Unable to connect to Phoenix DB with error %s", err.Error())
23+
}
24+
25+
return phxDB, nil
26+
}

remote/services/errors.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package services

remote/services/queries.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package services
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/FoxComm/highlander/remote/models/phoenix"
7+
"github.com/jinzhu/gorm"
8+
)
9+
10+
func FindChannelByID(phxDB *gorm.DB, id int, phxChannel *phoenix.Channel) error {
11+
err := phxDB.First(phxChannel, id).Error
12+
13+
if err != nil && err.Error() == "record not found" {
14+
return fmt.Errorf("Channel with ID %d not found", id)
15+
}
16+
17+
return err
18+
}
19+
20+
func InsertChannel(phxDB *gorm.DB, phxChannel *phoenix.Channel) error {
21+
return phxDB.Create(phxChannel).Error
22+
}

remote/utils/config.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
const (
9+
errEnvVarNotFound = "%s not found in the environment"
10+
11+
phoenixDatabaseName = "PHX_DATABASE_NAME"
12+
phoenixDatabaseHost = "PHX_DATABASE_HOST"
13+
phoenixDatabaseUser = "PHX_DATABASE_USER"
14+
phoenixDatabasePassword = "PHX_DATABASE_PASSWORD"
15+
phoenixDatabaseSSL = "PHX_DATABASE_SSL"
16+
17+
port = "PORT"
18+
)
19+
20+
type Config struct {
21+
PhxDatabaseName string
22+
PhxDatabaseHost string
23+
PhxDatabaseUser string
24+
PhxDatabasePassword string
25+
PhxDatabaseSSL string
26+
27+
Port string
28+
}
29+
30+
func NewConfig() (*Config, error) {
31+
config := &Config{}
32+
var err error
33+
34+
config.PhxDatabaseName, err = parseEnvVar(phoenixDatabaseName, nil)
35+
config.PhxDatabaseHost, err = parseEnvVar(phoenixDatabaseHost, err)
36+
config.PhxDatabaseUser, err = parseEnvVar(phoenixDatabaseUser, err)
37+
config.PhxDatabaseSSL, err = parseEnvVar(phoenixDatabaseSSL, err)
38+
config.PhxDatabasePassword = os.Getenv(phoenixDatabasePassword)
39+
40+
config.Port, err = parseEnvVar(port, err)
41+
42+
return config, err
43+
}
44+
45+
func parseEnvVar(varName string, err error) (string, error) {
46+
if err != nil {
47+
return "", err
48+
}
49+
50+
str := os.Getenv(varName)
51+
if str == "" {
52+
return "", fmt.Errorf(errEnvVarNotFound, varName)
53+
}
54+
55+
return str, nil
56+
}

0 commit comments

Comments
 (0)