Skip to content

Commit

Permalink
Merge pull request #1 from bitly/add-actual-code
Browse files Browse the repository at this point in the history
add sprinternship files
  • Loading branch information
dianaabishop authored Jan 5, 2025
2 parents 2b709d4 + e933377 commit dfa9506
Show file tree
Hide file tree
Showing 67 changed files with 32,753 additions and 0 deletions.
21 changes: 21 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module main.go

go 1.16

require (
github.com/gin-gonic/contrib v0.0.0-20221130124618-7e01895a63f2
github.com/gin-gonic/gin v1.8.1
github.com/go-gorp/gorp v2.2.0+incompatible
github.com/go-sql-driver/mysql v1.7.0
github.com/google/go-cmp v0.5.9 // indirect
github.com/lib/pq v1.10.7 // indirect
github.com/mattn/go-sqlite3 v1.14.16 // indirect
github.com/poy/onpar v1.1.2 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/ziutek/mymysql v1.5.4 // indirect
golang.org/x/net v0.16.0 // indirect
golang.org/x/sys v0.14.0 // indirect
google.golang.org/appengine v1.1.0
gopkg.in/yaml.v3 v3.0.1 // indirect

)
272 changes: 272 additions & 0 deletions go.sum

Large diffs are not rendered by default.

142 changes: 142 additions & 0 deletions handlers/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package handlers

import (
"net/http"
"strconv"

"github.com/gin-gonic/gin"
"google.golang.org/appengine/log"
eventsdb "main.go/internal/database"
"main.go/models"
)

func setCors(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
}

func HandleCors(c *gin.Context) {
setCors(c)

c.AbortWithStatus(204)
return

}

// creates a new event
func CreateEvent(c *gin.Context) {
setCors(c)
var event models.Event

// Call BindJSON to bind the received JSON to event +add error handling later
if err := c.BindJSON(&event); err != nil {
log.Errorf(c, "ERROR: %+v", err)
c.IndentedJSON(http.StatusBadRequest, nil) //bad data
return
}

createdEvent, err := eventsdb.CreateEvent(event)
if err != nil {
log.Errorf(c, "ERROR: %+v", err)
c.IndentedJSON(http.StatusInternalServerError, nil) //server error
return
}

c.JSON(201, createdEvent) //success
}

func GetEvent(c *gin.Context) {
setCors(c)

eventID := c.Param("eventID")
intEventID, err := strconv.Atoi(eventID)
if err != nil {
log.Errorf(c, "ERROR: %+v", err)
c.IndentedJSON(http.StatusBadRequest, nil) //bad data
return
}

event, err := eventsdb.GetEvent(intEventID)
if err != nil {
log.Errorf(c, "ERROR: %+v", err)
c.IndentedJSON(http.StatusInternalServerError, err) //server error
return
}

if event == nil {
c.IndentedJSON(http.StatusNotFound, nil) //event not found
return
}
c.JSON(200, event) //success
}

func GetPublicEvents(c *gin.Context) {
setCors(c)

// Fetch all public events from the database
publicEvents, err := eventsdb.GetAllPublicEvents()
if err != nil {
log.Errorf(c, "ERROR: %+v", err)
c.IndentedJSON(http.StatusInternalServerError, nil) //server error
return
}
c.JSON(200, publicEvents) //success - return the list of public events
}

// updating an event
func UpdateEventByEventId(c *gin.Context) {
setCors(c)

var updatedEventData models.Event

eventID := c.Param("eventID")
intEventID, err := strconv.Atoi(eventID)
if err != nil {
log.Errorf(c, "ERROR: %+v", err)
c.IndentedJSON(http.StatusBadRequest, nil)
return
}

// Call BindJSON to bind the received JSON to event +add error handling later
if err := c.BindJSON(&updatedEventData); err != nil {
log.Errorf(c, "ERROR: %+v", err)
c.IndentedJSON(http.StatusBadRequest, nil) //bad data
return
}

updatedEvent, err := eventsdb.UpdateEventByEventId(intEventID, updatedEventData)
if err != nil {
log.Errorf(c, "ERROR: %+v", err)
c.IndentedJSON(http.StatusInternalServerError, nil) //server error
return
}

c.JSON(201, updatedEvent) //update created - success
}

func GetEventsByField(c *gin.Context) {
setCors(c)

// Retrieve the field and value from query parameters
field := c.Query("field")
value := c.Query("value")

// Validate that both field and value are provided
if field == "" || value == "" {
log.Errorf(c, "ERROR: Both field and value query parameters are required")
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "Both 'field' and 'value' query parameters are required"})
return
}

// Fetch events from the database based on the specified field and value
filteredEvents, err := eventsdb.GetEventsByField(field, value)
if err != nil {
log.Errorf(c, "ERROR: %+v", err)
c.IndentedJSON(http.StatusInternalServerError, nil) //server error
return
}

c.JSON(http.StatusOK, filteredEvents) //success - return the list of filtered events
}
32 changes: 32 additions & 0 deletions handlers/hosts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package handlers

import (
"net/http"

"github.com/gin-gonic/gin"
"google.golang.org/appengine/log"
eventsdb "main.go/internal/database"
"main.go/models"
)

// creates a new event host
func CreateHost(c *gin.Context) {
setCors(c)
var host models.Host

// Call BindJSON to bind the received JSON to event +add error handling later
if err := c.BindJSON(&host); err != nil {
log.Errorf(c, "ERROR: %+v", err)
c.IndentedJSON(http.StatusBadRequest, nil) //bad data
return
}

createdHost, err := eventsdb.CreateHost(host)
if err != nil {
log.Errorf(c, "ERROR: %+v", err)
c.IndentedJSON(http.StatusInternalServerError, nil) //server error
return
}

c.JSON(201, createdHost) //success
}
53 changes: 53 additions & 0 deletions handlers/rsvp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package handlers

import (
"net/http"

"github.com/gin-gonic/gin"
"google.golang.org/appengine/log"
eventsdb "main.go/internal/database"
"main.go/models"
)

// creates a new RSVP
func CreateRSVP(c *gin.Context) {
setCors(c)
var rsvp models.RSVP

// Call BindJSON to bind the received JSON to event +add error handling later
if err := c.BindJSON(&rsvp); err != nil {
log.Errorf(c, "ERROR: %+v", err)
c.IndentedJSON(http.StatusBadRequest, nil) //bad data
return
}

// populated with an event
getEvent, err := eventsdb.GetEvent(rsvp.EventID)
if err != nil {
log.Errorf(c, "ERROR: %+v", err)
c.IndentedJSON(http.StatusInternalServerError, nil) //server error
return
}

getRSVPById, err := eventsdb.GetRSVPsByEventId(rsvp.EventID)
if err != nil {
log.Errorf(c, "ERROR: %+v", err)
c.IndentedJSON(http.StatusInternalServerError, nil) //server error
return
}

if !(getEvent.MaxAttendees > len(getRSVPById)) {
log.Errorf(c, "ERROR: %+v", err)
c.IndentedJSON(http.StatusBadRequest, nil) //bad data
return
}

createdRSVP, err := eventsdb.CreateRSVP(rsvp)
if err != nil {
log.Errorf(c, "ERROR: %+v", err)
c.IndentedJSON(http.StatusInternalServerError, nil) //server error
return
}

c.JSON(201, createdRSVP) //success
}
Loading

0 comments on commit dfa9506

Please sign in to comment.