-
Notifications
You must be signed in to change notification settings - Fork 0
/
registrations.go
55 lines (43 loc) · 1.32 KB
/
registrations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package routes
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/slimreaper35/konflux-test/models"
)
func RegisterForEventHandler(context *gin.Context) {
eventID, err := strconv.ParseInt(context.Param("id"), 10, 64)
if err != nil {
context.JSON(http.StatusBadRequest, gin.H{"error": "invalid event id"})
return
}
event, err := models.GetEventBy(eventID)
if err != nil {
context.JSON(http.StatusNotFound, gin.H{"error": "event not found"})
return
}
var userID = context.GetInt64("userID")
if event.RegisterUser(userID) != nil {
context.JSON(http.StatusInternalServerError, gin.H{"error": "failed to register for event"})
return
}
context.JSON(http.StatusCreated, gin.H{"message": "OK"})
}
func UnregisterFromEventHandler(context *gin.Context) {
id, err := strconv.ParseInt(context.Param("id"), 10, 64)
if err != nil {
context.JSON(http.StatusBadRequest, gin.H{"error": "invalid event id"})
return
}
event, err := models.GetEventBy(id)
if err != nil {
context.JSON(http.StatusNotFound, gin.H{"error": "event not found"})
return
}
var userID = context.GetInt64("userID")
if event.UnregisterUser(userID) != nil {
context.JSON(http.StatusInternalServerError, gin.H{"error": "failed to unregister from event"})
return
}
context.JSON(http.StatusOK, gin.H{"message": "OK"})
}