Skip to content

Commit be6d125

Browse files
committed
api build out
1 parent 4ee3168 commit be6d125

File tree

6 files changed

+108
-44
lines changed

6 files changed

+108
-44
lines changed

server/api/api.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ import (
1515
"net/http"
1616
)
1717

18-
// NotificationsAPIApiRouter defines the required methods for binding the api requests to a responses for the NotificationsAPIApi
19-
// The NotificationsAPIApiRouter implementation should parse necessary information from the http request,
20-
// pass the data to a NotificationsAPIApiServicer to perform the required actions, then write the service results to the http response.
21-
type NotificationsAPIApiRouter interface {
18+
// NotificationsApiRouter defines the required methods for binding the api requests to a responses for the NotificationsApi
19+
// The NotificationsApiRouter implementation should parse necessary information from the http request,
20+
// pass the data to a NotificationsApiServicer to perform the required actions, then write the service results to the http response.
21+
type NotificationsApiRouter interface {
2222
AddNotification(http.ResponseWriter, *http.Request)
2323
Ping(http.ResponseWriter, *http.Request)
2424
}
2525

26-
// NotificationsAPIApiServicer defines the api actions for the NotificationsAPIApi service
26+
// NotificationsApiServicer defines the api actions for the NotificationsAPIApi service
2727
// This interface intended to stay up to date with the openapi yaml used to generate it,
2828
// while the service implementation can ignored with the .openapi-generator-ignore file
2929
// and updated with the logic required for the API.
30-
type NotificationsAPIApiServicer interface {
30+
type NotificationsApiServicer interface {
3131
AddNotification(context.Context, NotificationDetails) (ImplResponse, error)
3232
Ping(context.Context) (ImplResponse, error)
3333
}

server/api/api_notifications_api.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ import (
1616
"strings"
1717
)
1818

19-
// A NotificationsAPIApiController binds http requests to an api service and writes the service results to the http response
20-
type NotificationsAPIApiController struct {
21-
service NotificationsAPIApiServicer
19+
// A NotificationsApiController binds http requests to an api service and writes the service results to the http response
20+
type NotificationsApiController struct {
21+
service NotificationsApiServicer
2222
}
2323

24-
// NewNotificationsAPIApiController creates a default api controller
25-
func NewNotificationsAPIApiController(s NotificationsAPIApiServicer) Router {
26-
return &NotificationsAPIApiController{service: s}
24+
// NewNotificationsApiController creates a default api controller
25+
func NewNotificationsApiController(s NotificationsApiServicer) Router {
26+
return &NotificationsApiController{service: s}
2727
}
2828

29-
// Routes returns all of the api route for the NotificationsAPIApiController
30-
func (c *NotificationsAPIApiController) Routes() Routes {
29+
// Routes returns all of the api route for the NotificationsApiController
30+
func (c *NotificationsApiController) Routes() Routes {
3131
return Routes{
3232
{
3333
"AddNotification",
@@ -45,7 +45,7 @@ func (c *NotificationsAPIApiController) Routes() Routes {
4545
}
4646

4747
// AddNotification - adds a new notification
48-
func (c *NotificationsAPIApiController) AddNotification(w http.ResponseWriter, r *http.Request) {
48+
func (c *NotificationsApiController) AddNotification(w http.ResponseWriter, r *http.Request) {
4949
notificationDetails := &NotificationDetails{}
5050
if err := json.NewDecoder(r.Body).Decode(&notificationDetails); err != nil {
5151
w.WriteHeader(http.StatusBadRequest)
@@ -63,7 +63,7 @@ func (c *NotificationsAPIApiController) AddNotification(w http.ResponseWriter, r
6363
}
6464

6565
// Ping - tests this api
66-
func (c *NotificationsAPIApiController) Ping(w http.ResponseWriter, r *http.Request) {
66+
func (c *NotificationsApiController) Ping(w http.ResponseWriter, r *http.Request) {
6767
result, err := c.service.Ping(r.Context())
6868
// If an error occurred, encode the error with the status code
6969
if err != nil {

server/api/api_notifications_api_service.go

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,69 @@ package api
1212

1313
import (
1414
"context"
15-
"errors"
15+
"database/sql"
16+
"fmt"
17+
_ "github.com/lib/pq"
1618
"net/http"
19+
"os"
1720
)
1821

19-
// NotificationsAPIApiService is a service that implents the logic for the NotificationsAPIApiServicer
22+
const (
23+
dbhost = "localhost"
24+
dbport = 5432
25+
dbuser = "postgres"
26+
dbname = "notifications"
27+
)
28+
29+
// NotificationsApiService is a service that implements the logic for the NotificationsApiServicer
2030
// This service should implement the business logic for every endpoint for the NotificationsAPIApi API.
2131
// Include any external packages or services that will be required by this service.
22-
type NotificationsAPIApiService struct {
32+
type NotificationsApiService struct {
2333
}
2434

25-
// NewNotificationsAPIApiService creates a default api service
26-
func NewNotificationsAPIApiService() NotificationsAPIApiServicer {
27-
return &NotificationsAPIApiService{}
35+
// NewNotificationsApiService creates a default api service
36+
func NewNotificationsApiService() NotificationsApiServicer {
37+
return &NotificationsApiService{}
2838
}
2939

30-
// AddNotification - adds a new notification
31-
func (s *NotificationsAPIApiService) AddNotification(ctx context.Context, notificationDetails NotificationDetails) (ImplResponse, error) {
32-
// TODO - update AddNotification with the required logic for this service method.
33-
// Add api_notifications_api_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.
40+
func (s *NotificationsApiService) getDbConnection() *sql.DB {
41+
connStr := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
42+
dbhost, dbport, dbuser, os.Getenv("POSTGRES_PASSWORD"), dbname)
3443

35-
//TODO: Uncomment the next line to return response Response(201, Id{}) or use other options such as http.Ok ...
36-
//return Response(201, Id{}), nil
44+
db, err := sql.Open("postgres", connStr)
45+
if err != nil {
46+
panic(err)
47+
}
3748

38-
//TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ...
39-
//return Response(400, nil),nil
49+
err = db.Ping()
50+
if err != nil {
51+
panic(err)
52+
}
4053

41-
//TODO: Uncomment the next line to return response Response(500, {}) or use other options such as http.Ok ...
42-
//return Response(500, nil),nil
43-
44-
return Response(http.StatusNotImplemented, nil), errors.New("AddNotification method not implemented")
54+
return db
4555
}
4656

47-
// Ping - tests this api
48-
func (s *NotificationsAPIApiService) Ping(ctx context.Context) (ImplResponse, error) {
49-
// TODO - update Ping with the required logic for this service method.
50-
// Add api_notifications_api_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.
57+
// AddNotification - adds a new notification
58+
func (s *NotificationsApiService) AddNotification(ctx context.Context, request NotificationDetails) (ImplResponse, error) {
59+
err := request.Validate()
60+
if err != nil {
61+
return Response(http.StatusBadRequest, nil), err
62+
}
63+
64+
db := s.getDbConnection()
65+
defer db.Close()
5166

52-
//TODO: Uncomment the next line to return response Response(201, PingResponse{}) or use other options such as http.Ok ...
53-
//return Response(201, PingResponse{}), nil
67+
var notificationId sql.NullInt32
68+
err = db.QueryRow("SELECT id FROM public.new_notification($1, $2);", request.NotificationType, request.NotificationText).Scan(&notificationId)
5469

55-
return Response(http.StatusNotImplemented, nil), errors.New("Ping method not implemented")
70+
return Response(http.StatusCreated, Id{Id: notificationId.Int32}), nil
71+
}
72+
73+
// Ping - tests this api
74+
func (s *NotificationsApiService) Ping(ctx context.Context) (ImplResponse, error) {
75+
hostname, err := os.Hostname()
76+
if err != nil {
77+
return Response(http.StatusInternalServerError, nil), err
78+
}
79+
return Response(http.StatusCreated, PingResponse{Hostname: hostname}), nil
5680
}

server/api/model_notification_details.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,19 @@
1010

1111
package api
1212

13+
import "github.com/go-playground/validator"
14+
1315
type NotificationDetails struct {
1416

1517
// describes the type of notification (0 = none, 1 = email, 2 = sms, 3 = slack)
16-
NotificationType int32 `json:"notificationType"`
18+
NotificationType int32 `json:"notificationType" validate:"gt=0,lte=3"`
1719

1820
// arbitrary notification data
19-
NotificationText string `json:"notificationText"`
21+
NotificationText string `json:"notificationText" validate:"required"`
22+
}
23+
24+
func (n *NotificationDetails) Validate() error {
25+
validate := validator.New()
26+
return validate.Struct(n)
2027
}
28+

server/go.mod

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,11 @@ module github.com/bebo-dot-dev/go-postgres-sockets/server
22

33
go 1.13
44

5-
require github.com/gorilla/mux v1.7.3
5+
require (
6+
github.com/go-playground/universal-translator v0.17.0 // indirect
7+
github.com/go-playground/validator v9.31.0+incompatible
8+
github.com/gorilla/mux v1.7.3
9+
github.com/leodido/go-urn v1.2.1 // indirect
10+
github.com/lib/pq v1.10.2
11+
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
12+
)

server/go.sum

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
4+
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
5+
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
6+
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
7+
github.com/go-playground/validator v9.31.0+incompatible h1:UA72EPEogEnq76ehGdEDp4Mit+3FDh548oRqwVgNsHA=
8+
github.com/go-playground/validator v9.31.0+incompatible/go.mod h1:yrEkQXlcI+PugkyDjY2bRrL/UBU4f3rvrgkN3V8JEig=
19
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
210
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
11+
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
12+
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
13+
github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=
14+
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
15+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
16+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
17+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
18+
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
19+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
20+
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
21+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
22+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
23+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
24+
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
25+
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
26+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
27+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)