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

Update response endpoint #20

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
97 changes: 88 additions & 9 deletions handlers/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -73,4 +73,83 @@ func CreateRSVP(c *gin.Context) {
return
}
c.JSON(201, rsvp) //success
}
}

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
}
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
18 changes: 18 additions & 0 deletions models/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

}
12 changes: 8 additions & 4 deletions sql/make_comment_id_nullable_in_rsvp.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
umidar marked this conversation as resolved.
Show resolved Hide resolved
-- DROP COLUMN #_of_RSVP;
-- ALTER TABLE event
-- ADD COLUMN num_of_RSVP integer;


ALTER TABLE rsvp
ADD COLUMN contact_info text;