updating rest-api

This commit is contained in:
2026-07-17 11:56:13 -05:00
parent 39433a0044
commit c4d7f52d37
15 changed files with 276 additions and 9 deletions

View File

@@ -0,0 +1,51 @@
package routes
import (
"net/http"
"strconv"
"example.com/rest-api/models"
"github.com/gin-gonic/gin"
)
func registerForEvent(context *gin.Context) {
userId := context.GetInt64("userId")
eventId, err := strconv.ParseInt(context.Param("id"), 10, 64)
if err != nil {
context.JSON(http.StatusBadRequest, gin.H{"message": "Could not parse event id."})
return
}
event, err := models.GetEventByID(eventId)
if err != nil {
context.JSON(http.StatusInternalServerError, gin.H{"message": "Could not fetch event."})
return
}
err = event.Register(userId)
if err != nil {
context.JSON(http.StatusInternalServerError, gin.H{"message": "Could not register user for event"})
return
}
context.JSON(http.StatusCreated, gin.H{"message": "Registered!"})
}
func cancelRegistration(context *gin.Context) {
userId := context.GetInt64("userId")
eventId, err := strconv.ParseInt(context.Param("id"), 10, 64)
var event models.Event
event.ID = eventId
err = event.CancelRegistration(userId)
if err != nil {
context.JSON(http.StatusInternalServerError, gin.H{"message": "Could not cancel registration"})
return
}
context.JSON(http.StatusOK, gin.H{"message": "Cancelled!"})
}