Browse Source

Hook up goal creation to DB

master
Stephanie Gredell 1 year ago
parent
commit
8cffa310e0
  1. 86
      pages/goals.go
  2. 16
      pages/wishlist.go
  3. 2
      templates/goals.html

86
pages/goals.go

@ -0,0 +1,86 @@ @@ -0,0 +1,86 @@
package pages
import (
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
"sponsorahacker/db"
"sponsorahacker/utils"
"strconv"
"strings"
"time"
)
type Goal struct {
Name string `form:"item-name" db:"name" binding:"required"`
FundingAmount float64 `form:"funding-amount" db:"funding_amount" binding:"required,numeric"`
Description string `form:"item-description" db:"description" binding:"required"`
LearnMoreURL *string `form:"item-url" db:"learn_more_url"` // Optional field
CreatedAt string `db:"created_at"`
UpdatedAt string `db:"updated_at"`
}
func Goals(c *gin.Context) {
isLoggedIn := utils.CheckIfLoggedIn(c)
c.HTML(http.StatusOK, "goals.html", gin.H{
"title": "Sponsor A Hacker",
"isLoggedIn": isLoggedIn,
})
}
func CreateGoal(c *gin.Context) {
dbClient, err := db.NewDbClient()
if err != nil {
log.Fatalf("Could not connect to database: %v", err)
return
}
isLoggedIn := utils.CheckIfLoggedIn(c)
name := strings.TrimSpace(c.PostForm("item-name"))
fundingAmountStr := strings.TrimSpace(c.PostForm("funding-amount"))
description := strings.TrimSpace(c.PostForm("item-description"))
learnMoreURL := strings.TrimSpace(c.PostForm("item-url"))
if name == "" && fundingAmountStr == "" && description == "" && learnMoreURL == "" {
fieldError := fmt.Errorf("missing a required field. Please check and make sure all fields are filled out")
c.HTML(http.StatusOK, "goals.html", gin.H{
"title": "Sponsor A Hacker",
"isLoggedIn": isLoggedIn,
"error": fieldError,
})
}
createdDate := time.Now()
updatedDate := time.Now()
fundingAmount, err := strconv.ParseFloat(fundingAmountStr, 64)
if err != nil {
fieldError := fmt.Errorf("invalid funding amount")
c.HTML(http.StatusOK, "goals.html", gin.H{
"title": "Sponsor A Hacker",
"isLoggedIn": isLoggedIn,
"error": fieldError,
})
}
_, err = dbClient.Exec(`INSERT INTO goals (name, description, learn_more_url, funding_amount, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?);`, name, description, learnMoreURL, fundingAmount, createdDate, updatedDate)
if err != nil {
fmt.Printf("Error inserting goal: %v", err)
c.HTML(http.StatusOK, "goals.html", gin.H{
"title": "Sponsor A Hacker",
"isLoggedIn": isLoggedIn,
"error": err,
})
}
c.HTML(http.StatusOK, "goals.html", gin.H{
"title": "Sponsor A Hacker",
"isLoggedIn": isLoggedIn,
})
}

16
pages/wishlist.go

@ -1,16 +0,0 @@ @@ -1,16 +0,0 @@
package pages
import (
"github.com/gin-gonic/gin"
"net/http"
"sponsorahacker/utils"
)
func Goals(c *gin.Context) {
isLoggedIn := utils.CheckIfLoggedIn(c)
c.HTML(http.StatusOK, "goals.html", gin.H{
"title": "Sponsor A Hacker",
"isLoggedIn": isLoggedIn,
})
}

2
templates/goals.html

@ -19,7 +19,7 @@ @@ -19,7 +19,7 @@
<section class="add-goal">
<h2 class="add-goal-title">Add a New Goal</h2>
<form id="add-item-form">
<form id="add-item-form" action="/goals" method="post">
<div class="form-group">
<label for="item-name" class="goals-form-label">Goal Name</label>
<input type="text" id="item-name" name="item-name" placeholder="e.g., Full-Stack Certification" required>

Loading…
Cancel
Save