diff --git a/handlers/events.go b/handlers/events.go index 6ac029b5..00920747 100644 --- a/handlers/events.go +++ b/handlers/events.go @@ -20,7 +20,7 @@ func CreateEvent(c *gin.Context) { _, err := dbmap.Query( "INSERT INTO event (title, date, time, location, host_name, description, contact_info, public_private, num_of_RSVP, max_attendees) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", event.EventTitle, event.Date, event.Time, event.Location, event.HostName, event.Description, event.ContactInfo, event.PublicPrivate, event.NumRSVP, event.MaxAttendees) - + if err != nil { c.IndentedJSON(http.StatusInternalServerError, nil) //server error return @@ -37,17 +37,17 @@ func GetEvent(c *gin.Context) { see_row) fmt.Printf("%+v", eventrow) - + for eventrow.Next() { var event models.GetEvent - // for each row, scan into the event struct + // for each row, scan into the event struct err = eventrow.Scan(&event.EventID, &event.EventTitle, &event.Date, &event.Time, &event.Location, &event.HostName, &event.Description, &event.ContactInfo, &event.PublicPrivate, &event.NumRSVP, &event.MaxAttendees) if err != nil { - fmt.Println(err) - c.IndentedJSON(http.StatusInternalServerError, nil) //server error - return - } - // append the event into events array + fmt.Println(err) + c.IndentedJSON(http.StatusInternalServerError, nil) //server error + return + } + // append the event into events array events = append(events, event) } c.JSON(201, events) //success @@ -73,4 +73,83 @@ func CreateRSVP(c *gin.Context) { return } c.JSON(201, rsvp) //success -} \ No newline at end of file +} + +func GetRSVP(c *gin.Context) { + var rsvpList []models.GetRSVP + + seeRow := c.Param("responseID") + RSVProw, err := dbmap.Query( + "SELECT response_id, event_id, name, rsvp, comment_id, contact_info FROM RSVP WHERE response_id=?;", + seeRow) + + for RSVProw.Next() { + var rsvp models.GetRSVP + // for each row, scan into the event struct + err = RSVProw.Scan(&rsvp.ResponseID, &rsvp.EventID, &rsvp.Name, &rsvp.RSVP, &rsvp.CommentID, &rsvp.ContactInfo) + if err != nil { + c.IndentedJSON(http.StatusInternalServerError, nil) //server error + fmt.Println(err) + return + } + // append the event into events array + rsvpList = append(rsvpList, rsvp) + } + c.JSON(201, rsvpList) //success +} + +func UpdateResponse(c *gin.Context) { + var rsvp models.UpdateResponse + var response []models.GetRSVP + + id := c.Param("rsvpID") + rsvpinfo, err := dbmap.Query( + "SELECT response_id, name, rsvp, contact_info FROM rsvp WHERE response_id=?", + id) + + fmt.Printf("%+v", rsvpinfo) + for rsvpinfo.Next() { + var userResponse models.GetRSVP + // for each row, scan into the event struct + err = rsvpinfo.Scan(&rsvp.ResponseID, &rsvp.Name, &rsvp.RSVP, &rsvp.ContactInfo) + if err != nil { + fmt.Println(err) + c.IndentedJSON(http.StatusInternalServerError, nil) //server error + return + } + // append the event into events array + response = append(response, userResponse) + } + + // Call BindJSON to bind the received JSON to event +add error handling later + if err = c.BindJSON(&rsvp); err != nil { + fmt.Println(err) + c.IndentedJSON(http.StatusBadRequest, nil) //bad data + return + } + + if rsvp.Name == ""{ + rsvp.Name = response[0].Name + } + if rsvp.ContactInfo == nil{ + rsvp.ContactInfo = response[0].ContactInfo + } + if rsvp.RSVP == ""{ + rsvp.RSVP = response[0].RSVP + } + if rsvp.ResponseID == 0{ + rsvp.ResponseID = response[0].ResponseID + } + + _, err = dbmap.Query( + "UPDATE rsvp SET name = ?, contact_info = ?, rsvp = ? WHERE response_id = ?;", + rsvp.Name, rsvp.ContactInfo, rsvp.RSVP, id) + + if err != nil { + fmt.Println(err) + c.IndentedJSON(http.StatusInternalServerError, nil) //server error + return + } + + c.JSON(201, rsvp) //success +} diff --git a/main.go b/main.go index 75bd76dd..1398375a 100644 --- a/main.go +++ b/main.go @@ -35,6 +35,11 @@ func main() { api.GET("/event/:eventID", handlers.GetEvent) api.POST("/event/:eventID/rsvp", handlers.CreateRSVP) + + api.GET("/RSVP/:responseID", handlers.GetRSVP) + + api.PATCH("/rsvp/:rsvpID", handlers.UpdateResponse) + // Start and run the server router.Run(":3000") } diff --git a/models/events.go b/models/events.go index dcdeecba..3db913b6 100644 --- a/models/events.go +++ b/models/events.go @@ -32,4 +32,22 @@ type CreateRSVP struct { EventID int `json:"event_id" binding:"required"` ResponderName string `json:"name" binding:"required"` RSVP string `json:"rsvp" binding:"required"` +} + + +type GetRSVP struct { + ResponseID int `json:"response_id"` + EventID int `json:"event_id"` + Name string `json:"name` + RSVP string `json:"rsvp" ` + CommentID *int `json:"comment_id" ` + ContactInfo *string `json:"contact_info"` +} + +type UpdateResponse struct { + ResponseID int `json:"response_id"` + Name string `json:"name"` + RSVP string `json:"rsvp"` + ContactInfo *string `json:"contact_info"` + } \ No newline at end of file diff --git a/sql/make_comment_id_nullable_in_rsvp.sql b/sql/make_comment_id_nullable_in_rsvp.sql index df6eb85b..2472899b 100644 --- a/sql/make_comment_id_nullable_in_rsvp.sql +++ b/sql/make_comment_id_nullable_in_rsvp.sql @@ -3,7 +3,11 @@ use events; ALTER TABLE rsvp MODIFY comment_id integer DEFAULT NULL; -ALTER TABLE event -DROP COLUMN #_of_RSVP; -ALTER TABLE event -ADD COLUMN num_of_RSVP integer; +-- ALTER TABLE event +-- DROP COLUMN #_of_RSVP; +-- ALTER TABLE event +-- ADD COLUMN num_of_RSVP integer; + + +ALTER TABLE rsvp +ADD COLUMN contact_info text;