Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a delete endpoint to delete rsvps and an entire event #34

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions handlers/events.go
Original file line number Diff line number Diff line change
@@ -15,17 +15,43 @@ 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")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE")
}

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

c.AbortWithStatus(204)
return

}

// delete an RSVP by event
// delete the Event itself

// DeleteEvent was define in main.go in handlers
// c *gin.Context --> gin is the api framework, context will contain the endpoint, will contain the event id, the api http method: DELETE, contains headers, sending large info of sql body in Json file
func DeleteEvent(c *gin.Context) {
setCors(c)

// finding and getting an individual eventid, delete all rsvps assoicated with the event
eventID := c.Param("eventID")
intEventID, err := strconv.Atoi(eventID)
if err != nil {
log.Printf("ERROR: %+v", err)
c.IndentedJSON(http.StatusBadRequest, nil) //bad data
return
}

err = eventsdb.DeleteEventByEventId(intEventID) //eventsdb.DeleteEvent is diff from handlers.DeleteEvent
if err != nil {
log.Printf("ERROR: %+v", err)
c.IndentedJSON(http.StatusInternalServerError, nil) //server error
return
}

c.JSON(204, nil) //success
}

// creates a new event
func CreateEvent(c *gin.Context) {
setCors(c)
21 changes: 21 additions & 0 deletions internal/database/event_table.go
Original file line number Diff line number Diff line change
@@ -122,3 +122,24 @@ func GetEventsByField(field, value string) ([]models.Event, error) {

return filteredEvents, nil
}

// function to delete an event and RSVP by eventId
func DeleteEventByEventId(eventID int) error {
_, err := dbmap.Exec(
"DELETE FROM rsvp WHERE event_id = ?",
eventID)

if err != nil {
return err
}

_, err = dbmap.Exec(
"DELETE FROM event WHERE event_id = ?",
eventID)

if err != nil {
return err
}

return nil
}
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -55,6 +55,10 @@ func main() {
api.GET("/rsvp/:rsvpID", handlers.GetRSVP)
api.OPTIONS("/rsvp/:rsvpID", handlers.HandleCors)

// DELETE an event
api.DELETE("/event/:eventID/delete", handlers.DeleteEvent) // api calls delete to an individual event by calling :eventID (path parameter) & /event/:eventID is a path to a specific event
api.OPTIONS("/event/:eventID/delete", handlers.HandleCors) // context works with api.DeLEte and does cool stuff

// Start and run the server
router.Run(":3000")
}