@@ -12,45 +12,69 @@ package api
1212
1313import (
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}
0 commit comments