Skip to content

Commit

Permalink
Merge pull request #1 from dxe/jake/add-tally-endpoint
Browse files Browse the repository at this point in the history
add tally endpoint
  • Loading branch information
jakehobbs authored Jun 21, 2024
2 parents 5d8a835 + b82bd3f commit 07a956e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 22 deletions.
3 changes: 2 additions & 1 deletion service/db.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"log"

"github.com/dxe/helptheducks.com/service/config"
"github.com/jmoiron/sqlx"
"log"
)

// TODO: make a separate script to init db b/c this is kinda sketch for prod.
Expand Down
33 changes: 31 additions & 2 deletions service/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"database/sql"
"encoding/json"
"fmt"
"github.com/dxe/helptheducks.com/service/config"
"github.com/dxe/helptheducks.com/service/model"
"net/http"
"net/mail"

"github.com/dxe/helptheducks.com/service/config"
"github.com/dxe/helptheducks.com/service/model"
"github.com/go-chi/chi/v5"
)

type CreateMessageInput struct {
Expand Down Expand Up @@ -92,3 +94,30 @@ func createMessageHandler(w http.ResponseWriter, r *http.Request) {

w.Write([]byte("ok"))
}

type GetTallyResp struct {
Total int `json:"total"`
}

func getTallyHandler(w http.ResponseWriter, r *http.Request) {
campaign := chi.URLParam(r, "campaign")

tally, err := model.GetTally(db, campaign)
if err != nil {
http.Error(w, fmt.Sprintf("error getting tally: %v", err), http.StatusInternalServerError)
return
}

resp := GetTallyResp{
Total: tally,
}
json, err := json.Marshal(resp)
if err != nil {
http.Error(w, "failed to marshal json", http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(json)
}
11 changes: 7 additions & 4 deletions service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package main

import (
"fmt"
"net/http"
"strings"
"time"
"unicode"

"github.com/aws/aws-sdk-go/service/ses"
"github.com/dxe/helptheducks.com/service/config"
"github.com/dxe/helptheducks.com/service/mailer"
Expand All @@ -11,10 +16,6 @@ import (
"github.com/go-chi/cors"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"net/http"
"strings"
"time"
"unicode"

"golang.org/x/text/runes"
"golang.org/x/text/transform"
Expand Down Expand Up @@ -53,6 +54,8 @@ func main() {
r.Post("/create", createMessageHandler)
})

r.Get("/tally", getTallyHandler)

go worker()

fmt.Printf("Listening on port %v\n", config.Port)
Expand Down
18 changes: 18 additions & 0 deletions service/model/tally.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package model

import "github.com/jmoiron/sqlx"

func GetTally(db *sqlx.DB, campaign string) (int, error) {
var tally int
err := db.Get(
&tally,
`SELECT count(*)
FROM messages
WHERE campaign = ?`,
campaign,
)
if (err != nil) {
return 0, err
}
return tally, nil
}
2 changes: 1 addition & 1 deletion src/components/petition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const Petition = () => {
...(data.zip && { zip: data.zip }),
...(data.city && { city: data.city }),
message: data.message,
campaign: "duck",
campaign: import.meta.env.PROD ? "duck" : "test",
token,
},
headers: {
Expand Down
14 changes: 0 additions & 14 deletions src/routeTree.gen.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
// This file is auto-generated by TanStack Router

import { FileRoute, lazyRouteComponent } from '@tanstack/react-router'

// Import Routes

import { Route as rootRoute } from './routes/__root'

// Create Virtual Routes

const IndexComponentImport = new FileRoute('/').createRoute()

// Create/Update Routes

const IndexComponentRoute = IndexComponentImport.update({
path: '/',
getParentRoute: () => rootRoute,
Expand All @@ -21,9 +13,6 @@ const IndexComponentRoute = IndexComponentImport.update({
'component',
),
})

// Populate the FileRoutesByPath interface

declare module '@tanstack/react-router' {
interface FileRoutesByPath {
'/': {
Expand All @@ -32,7 +21,4 @@ declare module '@tanstack/react-router' {
}
}
}

// Create and export the route tree

export const routeTree = rootRoute.addChildren([IndexComponentRoute])

0 comments on commit 07a956e

Please sign in to comment.