Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4a469d1fd4 |
@@ -91,6 +91,12 @@ func (a *APIHandler) OpenDatabase(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := a.DbHandler.AddNewTable(&models.Group{}); err != nil {
|
||||||
|
a.logger.Error("OpenDatabase", err)
|
||||||
|
c.JSON(http.StatusInternalServerError, nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
a.DbHandler.SetToken(database.Token)
|
a.DbHandler.SetToken(database.Token)
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
|||||||
128
api/groupHandler.go
Normal file
128
api/groupHandler.go
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"gitea.tecamino.com/paadi/memberDB/models"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (a *APIHandler) NewGroup(c *gin.Context) {
|
||||||
|
if !a.DBHandlerIsInitialized() {
|
||||||
|
a.logger.Error("StartNewEvent", "database handler is not initialized")
|
||||||
|
c.JSON(http.StatusInternalServerError, nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var group models.Group
|
||||||
|
err := c.BindJSON(&group)
|
||||||
|
if err != nil {
|
||||||
|
a.logger.Error("NewGroup", err)
|
||||||
|
c.JSON(http.StatusInternalServerError, nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := a.DbHandler.NewGroup(&group); err != nil {
|
||||||
|
a.logger.Error("NewGroup", err)
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"message": "New group added " + group.Name,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *APIHandler) GetGroup(c *gin.Context) {
|
||||||
|
if !a.DBHandlerIsInitialized() {
|
||||||
|
a.logger.Error("GetGroup", "database handler is not initialized")
|
||||||
|
c.JSON(http.StatusInternalServerError, nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var i int
|
||||||
|
var err error
|
||||||
|
|
||||||
|
id := c.Query("id")
|
||||||
|
if id != "" {
|
||||||
|
i, err = strconv.Atoi(id)
|
||||||
|
if err != nil {
|
||||||
|
a.logger.Error("GetGroup", err)
|
||||||
|
|
||||||
|
c.JSON(http.StatusInternalServerError, nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
groups, err := a.DbHandler.GetGroup(uint(i))
|
||||||
|
if err != nil {
|
||||||
|
a.logger.Error("GetGroup", err)
|
||||||
|
c.JSON(http.StatusBadRequest, nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, groups)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *APIHandler) UpdateGroup(c *gin.Context) {
|
||||||
|
if !a.DBHandlerIsInitialized() {
|
||||||
|
a.logger.Error("UpdateGroup", "database handler is not initialized")
|
||||||
|
c.JSON(http.StatusInternalServerError, nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var groups []models.Group
|
||||||
|
err := c.BindJSON(&groups)
|
||||||
|
if err != nil {
|
||||||
|
a.logger.Error("UpdateGroup", err)
|
||||||
|
c.JSON(http.StatusInternalServerError, nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, event := range groups {
|
||||||
|
err = a.DbHandler.UpdateGroup(event.Id, event)
|
||||||
|
if err != nil {
|
||||||
|
a.logger.Error("UpdateGroup", err)
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
|
"message": err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"message": "group(s) updated",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *APIHandler) DeleteGroup(c *gin.Context) {
|
||||||
|
if !a.DBHandlerIsInitialized() {
|
||||||
|
a.logger.Error("DeleteGroup", "database handler is not initialized")
|
||||||
|
c.JSON(http.StatusInternalServerError, nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
|
||||||
|
var request struct {
|
||||||
|
Ids []uint `json:"ids"`
|
||||||
|
}
|
||||||
|
err = c.BindJSON(&request)
|
||||||
|
if err != nil {
|
||||||
|
a.logger.Error("DeleteGroup", err)
|
||||||
|
c.JSON(http.StatusBadRequest, nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = a.DbHandler.DeleteGroup(request.Ids...)
|
||||||
|
if err != nil {
|
||||||
|
a.logger.Error("DeleteGroup", err)
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
|
"message": err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"message": "group deleted",
|
||||||
|
})
|
||||||
|
}
|
||||||
49
handlers/group.go
Normal file
49
handlers/group.go
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"gitea.tecamino.com/paadi/memberDB/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (dh *DatabaseHandler) NewGroup(group *models.Group) error {
|
||||||
|
if !dh.DatabaseOpened() {
|
||||||
|
return errors.New("database not opened")
|
||||||
|
}
|
||||||
|
|
||||||
|
if dh.database.Exists(&models.Group{}, "name", group.Name, false) {
|
||||||
|
return errors.New("group with name: " + group.Name + " exists already")
|
||||||
|
}
|
||||||
|
return dh.database.AddNewColum(&group)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dh *DatabaseHandler) GetGroup(id uint) (group []models.Group, err error) {
|
||||||
|
if !dh.DatabaseOpened() {
|
||||||
|
return group, errors.New("database not opened")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = dh.database.GetById(&group, id)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dh *DatabaseHandler) UpdateGroup(id int, group models.Group) (err error) {
|
||||||
|
if !dh.DatabaseOpened() {
|
||||||
|
return errors.New("database not opened")
|
||||||
|
}
|
||||||
|
return dh.database.UpdateValuesById(&group, uint(group.Id))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dh *DatabaseHandler) DeleteGroup(ids ...uint) error {
|
||||||
|
if !dh.DatabaseOpened() {
|
||||||
|
return errors.New("database not opened")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ids) == 0 {
|
||||||
|
return errors.New("no ids given to be deleted")
|
||||||
|
}
|
||||||
|
|
||||||
|
return dh.database.DeleteById(&models.Group{}, ids...)
|
||||||
|
}
|
||||||
6
models/group.go
Normal file
6
models/group.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
type Group struct {
|
||||||
|
Id int `gorm:"primaryKey" json:"id"`
|
||||||
|
Name string `gorm:"column:name" json:"name"`
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user