implement dbhandler package and new test file
This commit is contained in:
@@ -10,16 +10,17 @@ import (
|
||||
)
|
||||
|
||||
func (a *APIHandler) AddNewResponsible(c *gin.Context) {
|
||||
if !a.databaseOpened(c) {
|
||||
if !a.DBHandlerIsInitialized() {
|
||||
a.logger.Error("AddNewResponsible", "database handler is not initialized")
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
var responsible models.Person
|
||||
err := c.BindJSON(&responsible)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
a.logger.Error("AddNewResponsible", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -36,11 +37,12 @@ func (a *APIHandler) AddNewResponsible(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func (a *APIHandler) GetResponsibleById(c *gin.Context) {
|
||||
if !a.databaseOpened(c) {
|
||||
func (a *APIHandler) GetResponsible(c *gin.Context) {
|
||||
if !a.DBHandlerIsInitialized() {
|
||||
a.logger.Error("GetResponsible", "database handler is not initialized")
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
var i int
|
||||
var err error
|
||||
|
||||
@@ -48,15 +50,15 @@ func (a *APIHandler) GetResponsibleById(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("GetResponsible", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
members, err := a.DbHandler.GetResponsible(i)
|
||||
members, err := a.DbHandler.GetResponsible(uint(i))
|
||||
if err != nil {
|
||||
a.logger.Error("GetResponsible", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
@@ -66,24 +68,26 @@ func (a *APIHandler) GetResponsibleById(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (a *APIHandler) DeleteResponsible(c *gin.Context) {
|
||||
if !a.databaseOpened(c) {
|
||||
if !a.DBHandlerIsInitialized() {
|
||||
a.logger.Error("DeleteResponsible", "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("DeleteResponsible", err)
|
||||
c.JSON(http.StatusBadRequest, nil)
|
||||
return
|
||||
}
|
||||
|
||||
err = a.DbHandler.DeleteResponsible(request.Ids...)
|
||||
if err != nil {
|
||||
a.logger.Error("DeleteResponsible", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user