implement dbhandler package and new test file
This commit is contained in:
@@ -9,22 +9,24 @@ import (
|
||||
)
|
||||
|
||||
func (a *APIHandler) StartNewEvent(c *gin.Context) {
|
||||
if !a.databaseOpened(c) {
|
||||
if !a.DBHandlerIsInitialized() {
|
||||
a.logger.Error("StartNewEvent", "database handler is not initialized")
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
name := c.Query("name")
|
||||
if name == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": "missing query 'name'",
|
||||
})
|
||||
a.logger.Error("StartNewEvent", "missing query 'name'")
|
||||
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err := a.DbHandler.StartNewEvent(name); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
a.logger.Error("StartNewEvent", err)
|
||||
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -33,8 +35,10 @@ func (a *APIHandler) StartNewEvent(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func (a *APIHandler) GetEventById(c *gin.Context) {
|
||||
if !a.databaseOpened(c) {
|
||||
func (a *APIHandler) GetEvent(c *gin.Context) {
|
||||
if !a.DBHandlerIsInitialized() {
|
||||
a.logger.Error("GetEvent", "database handler is not initialized")
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -45,18 +49,17 @@ func (a *APIHandler) GetEventById(c *gin.Context) {
|
||||
if id != "" {
|
||||
i, err = strconv.Atoi(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
a.logger.Error("GetEvent", err)
|
||||
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
events, err := a.DbHandler.GetEvent(i)
|
||||
events, err := a.DbHandler.GetEvent(uint(i))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
a.logger.Error("GetEvent", err)
|
||||
c.JSON(http.StatusBadRequest, nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -64,24 +67,27 @@ func (a *APIHandler) GetEventById(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (a *APIHandler) DeleteEvent(c *gin.Context) {
|
||||
if !a.databaseOpened(c) {
|
||||
if !a.DBHandlerIsInitialized() {
|
||||
a.logger.Error("DeleteEvent", "database handler is not initialized")
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
var request struct {
|
||||
Ids []int `json:"ids"`
|
||||
Ids []uint `json:"ids"`
|
||||
}
|
||||
err = c.BindJSON(&request)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
a.logger.Error("DeleteEvent", err)
|
||||
c.JSON(http.StatusBadRequest, nil)
|
||||
return
|
||||
}
|
||||
|
||||
err = a.DbHandler.DeleteEvent(request.Ids...)
|
||||
if err != nil {
|
||||
a.logger.Error("DeleteEvent", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
@@ -93,22 +99,25 @@ func (a *APIHandler) DeleteEvent(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (a *APIHandler) AddNewAttendees(c *gin.Context) {
|
||||
if !a.databaseOpened(c) {
|
||||
if !a.DBHandlerIsInitialized() {
|
||||
a.logger.Error("AddNewAttendees", "database handler is not initialized")
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
var event models.Event
|
||||
err := c.BindJSON(&event)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
a.logger.Error("AddNewAttendees", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
err = a.DbHandler.AddAttendeesToEvent(event)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
a.logger.Error("DeleteEvent", err)
|
||||
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
@@ -120,23 +129,26 @@ func (a *APIHandler) AddNewAttendees(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (a *APIHandler) DeleteAttendee(c *gin.Context) {
|
||||
if !a.databaseOpened(c) {
|
||||
if !a.DBHandlerIsInitialized() {
|
||||
a.logger.Error("DeleteAttendee", "database handler is not initialized")
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
var event models.Event
|
||||
err = c.BindJSON(&event)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
a.logger.Error("DeleteAttendee", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
err = a.DbHandler.DeleteAttendeesFromEvent(event)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
a.logger.Error("DeleteAttendeesFromEvent", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user