@@ -3,29 +3,53 @@ package controllers
3
3
import (
4
4
"net/http"
5
5
6
+ "github.com/FoxComm/highlander/remote/db"
6
7
"github.com/FoxComm/highlander/remote/models/phoenix"
7
8
"github.com/FoxComm/highlander/remote/payloads"
8
9
"github.com/FoxComm/highlander/remote/responses"
10
+ "github.com/jinzhu/gorm"
9
11
)
10
12
13
+ type Channels struct {
14
+ phxDB * gorm.DB
15
+ }
16
+
17
+ func NewChannels (phxDB * gorm.DB ) * Channels {
18
+ return & Channels {phxDB : phxDB }
19
+ }
20
+
11
21
// GetChannel finds a single channel by its ID.
12
- func GetChannel (id int ) ControllerFunc {
22
+ func ( ctrl * Channels ) GetChannel (id int ) ControllerFunc {
13
23
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
+ }
18
39
}
19
40
41
+ chResp := & responses.Channel {}
42
+ chResp .Build (channel )
43
+
20
44
return & responses.Response {
21
45
StatusCode : http .StatusOK ,
22
- Body : channel ,
46
+ Body : chResp ,
23
47
}
24
48
}
25
49
}
26
50
27
51
// CreateChannel creates a new channel.
28
- func CreateChannel (payload * payloads.CreateChannel ) ControllerFunc {
52
+ func ( ctrl * Channels ) CreateChannel (payload * payloads.CreateChannel ) ControllerFunc {
29
53
return func () * responses.Response {
30
54
return & responses.Response {
31
55
StatusCode : http .StatusCreated ,
0 commit comments