Browse Source

Adds implementation for create and list submissions

master
Noah Pederson 3 years ago
parent
commit
53fb1c8ca7
  1. 1
      .gitignore
  2. 13
      cmd/main.go
  3. 27
      pkg/contact/contact.go
  4. 36
      pkg/contact/web.go

1
.gitignore

@ -14,3 +14,4 @@
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/
.vscode/

13
cmd/main.go

@ -2,7 +2,6 @@ package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
@ -14,12 +13,14 @@ import (
func main() {
POSTGRES_USER := os.Getenv("POSTGRES_USER")
POSTGRES_PASSWORD := os.Getenv("POSTGRES_PASSWORD")
POSTGRES_ADDR := os.Getenv("POSTGRES_ADDR")
// POSTGRES_USER := os.Getenv("POSTGRES_USER")
// POSTGRES_PASSWORD := os.Getenv("POSTGRES_PASSWORD")
// POSTGRES_ADDR := os.Getenv("POSTGRES_ADDR")
connStr := fmt.Sprintf("host=%s user=%s password='%s' dbname=packetlostandfound sslmode=disable", POSTGRES_ADDR, POSTGRES_USER, POSTGRES_PASSWORD)
db, err := sql.Open("postgres", connStr)
POSTGRES_URI := os.Getenv("POSTGRES_URI")
// connStr := fmt.Sprintf("host=%s user=%s password='%s' dbname=packetlostandfound sslmode=disable", POSTGRES_ADDR, POSTGRES_USER, POSTGRES_PASSWORD)
db, err := sql.Open("postgres", POSTGRES_URI)
if err != nil {
panic(err)
}

27
pkg/contact/contact.go

@ -21,12 +21,33 @@ type Service struct {
//Init initializes the service and creates any tables necessary in Service.DB to run the service
func (c *Service) Init() {
if _, err := c.DB.Exec("CREATE TABLE IF NOT EXISTS CONTACT_SUBMISSIONS(ID INT PRIMARY KEY, EMAIL TEXT NOT NULL, MESSAGE TEXT NOT NULL, SUBMITTED DATE NOT NULL DEFAULT CURRENT_DATE)"); err != nil {
if _, err := c.DB.Exec("CREATE DATABASE IF NOT EXISTS packetlostandfound"); err != nil {
log.Fatal(err)
}
if _, err := c.DB.Exec("CREATE TABLE IF NOT EXISTS CONTACT_SUBMISSIONS(ID SERIAL PRIMARY KEY, EMAIL TEXT NOT NULL, MESSAGE TEXT NOT NULL, SUBMITTED DATE NOT NULL DEFAULT CURRENT_DATE)"); err != nil {
log.Fatal(err)
}
}
//Create creates and stores a contact submission in the database
func (c *Service) Create(cs *Submission) {
panic("Not implemented")
func (c *Service) Create(cs *Submission) error {
_, err := c.DB.Exec("INSERT INTO CONTACT_SUBMISSIONS(EMAIL, MESSAGE) VALUES($1, $2)", cs.Email, cs.Message)
return err
}
func (c *Service) List() (*[]Submission, error) {
rows, err := c.DB.Query("SELECT ID, EMAIL, MESSAGE, SUBMITTED FROM CONTACT_SUBMISSIONS")
if err != nil {
log.Printf("Unable to get submissions: %s", err)
return nil, err
}
submissions := make([]Submission, 0, 5)
for rows.Next() {
var sub Submission
if err := rows.Scan(&sub.ID, &sub.Email, &sub.Message, &sub.Date); err != nil {
log.Printf(err.Error())
}
submissions = append(submissions, sub)
}
return &submissions, nil
}

36
pkg/contact/web.go

@ -1,6 +1,8 @@
package contact
import (
"encoding/json"
"fmt"
"log"
"net/http"
@ -10,6 +12,38 @@ import (
//BindEndpoints binds service functions to their corresponding RESTful endpoints
func BindEndpoints(r chi.Router, s Service) {
r.Post("/contact_submission", func(w http.ResponseWriter, r *http.Request) {
log.Println("Received contact submission request")
log.Printf("%s called...", r.URL.Path)
decoder := json.NewDecoder(r.Body)
defer r.Body.Close()
var submission Submission
err := decoder.Decode(&submission)
if err != nil {
log.Printf("Called: %s - Invalid JSON payload: %s", r.URL.Path, err)
http.Error(w, "Invalid JSON request", http.StatusBadRequest)
return
}
err = s.Create(&submission)
if err != nil {
log.Printf("Error creating submission: %s", err)
http.Error(w, "Unable to submit", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusAccepted)
})
r.Get("/list_submissions", func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s called...", r.URL.Path)
subs, err := s.List()
if err != nil {
log.Printf("Unable to get submissions: %s", err.Error())
http.Error(w, "Unable to list submissions", http.StatusInternalServerError)
}
responseText, err := json.Marshal(subs)
if err != nil {
errorResponse := fmt.Sprintf("Unable to marshal JSON response: %s", err)
log.Printf(errorResponse)
http.Error(w, errorResponse, http.StatusInternalServerError)
return
}
w.Write(responseText)
})
}
Loading…
Cancel
Save