add new handlers for event and responsible tables
This commit is contained in:
148
api/eventHandler.go
Normal file
148
api/eventHandler.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"gitea.tecamino.com/paadi/memberDB/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func (a *APIHandler) StartNewEvent(c *gin.Context) {
|
||||
if !a.databaseOpened(c) {
|
||||
return
|
||||
}
|
||||
|
||||
name := c.Query("name")
|
||||
if name == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": "missing query 'name'",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if err := a.DbHandler.StartNewEvent(name); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "New Event added " + name,
|
||||
})
|
||||
}
|
||||
|
||||
func (a *APIHandler) GetEventById(c *gin.Context) {
|
||||
if !a.databaseOpened(c) {
|
||||
return
|
||||
}
|
||||
|
||||
var i int
|
||||
var err error
|
||||
|
||||
id := c.Query("id")
|
||||
if id != "" {
|
||||
i, err = strconv.Atoi(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
events, err := a.DbHandler.GetEvent(i)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, events)
|
||||
}
|
||||
|
||||
func (a *APIHandler) DeleteEvent(c *gin.Context) {
|
||||
if !a.databaseOpened(c) {
|
||||
return
|
||||
}
|
||||
var err error
|
||||
|
||||
var request struct {
|
||||
Ids []int `json:"ids"`
|
||||
}
|
||||
err = c.BindJSON(&request)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
err = a.DbHandler.DeleteEvent(request.Ids...)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "event deleted",
|
||||
})
|
||||
}
|
||||
|
||||
func (a *APIHandler) AddNewAttendees(c *gin.Context) {
|
||||
if !a.databaseOpened(c) {
|
||||
return
|
||||
}
|
||||
|
||||
var event models.Event
|
||||
err := c.BindJSON(&event)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
err = a.DbHandler.AddAttendeesToEvent(event)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "attendee(s) added",
|
||||
})
|
||||
}
|
||||
|
||||
func (a *APIHandler) DeleteAttendee(c *gin.Context) {
|
||||
if !a.databaseOpened(c) {
|
||||
return
|
||||
}
|
||||
var err error
|
||||
|
||||
var event models.Event
|
||||
err = c.BindJSON(&event)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
err = a.DbHandler.DeleteAttendeesFromEvent(event)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "attendee(s) deleted",
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user