261 lines
6.4 KiB
Go
261 lines
6.4 KiB
Go
package menu
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"os"
|
|
"template_blueprint/internal/database"
|
|
"template_blueprint/pkg/database/mongo"
|
|
MenuHandler "template_blueprint/pkg/models/menu"
|
|
)
|
|
|
|
func Getmenukeuangan(c *gin.Context) {
|
|
local := os.Getenv("MONGODB_DEV_KEUANGAN")
|
|
db := database.New(local).GetMongoDB()
|
|
if db == nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
|
|
return
|
|
}
|
|
|
|
mongoDB := mongo.NewDatabaseServiceMongo(db)
|
|
menu, errSelect := mongoDB.GetMenu()
|
|
if errSelect != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": errSelect.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, menu)
|
|
}
|
|
|
|
func Getrole(c *gin.Context) {
|
|
local := os.Getenv("MONGODB_DEV_KEUANGAN")
|
|
db := database.New(local).GetMongoDB()
|
|
if db == nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
|
|
return
|
|
}
|
|
|
|
mongoDB := mongo.NewDatabaseServiceMongo(db)
|
|
role, errSelect := mongoDB.Getrole()
|
|
if errSelect != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": errSelect.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, role)
|
|
}
|
|
|
|
func Gettype(c *gin.Context) {
|
|
local := os.Getenv("MONGODB_DEV_KEUANGAN")
|
|
db := database.New(local).GetMongoDB()
|
|
if db == nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
|
|
return
|
|
}
|
|
|
|
mongoDB := mongo.NewDatabaseServiceMongo(db)
|
|
typeuser, errSelect := mongoDB.Gettype()
|
|
if errSelect != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": errSelect.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, typeuser)
|
|
}
|
|
|
|
func GetroleByType(c *gin.Context) {
|
|
local := os.Getenv("MONGODB_DEV_KEUANGAN")
|
|
db := database.New(local).GetMongoDB()
|
|
if db == nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
|
|
return
|
|
}
|
|
|
|
id := c.Param("id")
|
|
|
|
if id == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"message": "Parameter 'id' dibutuhkan"})
|
|
return
|
|
}
|
|
|
|
mongoDB := mongo.NewDatabaseServiceMongo(db)
|
|
dataID, err := mongoDB.GetBytypeID(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"data": dataID,
|
|
"message": "ID berhasil di cari",
|
|
})
|
|
}
|
|
|
|
func GetmenuByID(c *gin.Context) {
|
|
local := os.Getenv("MONGODB_DEV_KEUANGAN")
|
|
db := database.New(local).GetMongoDB()
|
|
if db == nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
|
|
return
|
|
}
|
|
|
|
id := c.Param("id")
|
|
|
|
if id == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"message": "Parameter 'id' dibutuhkan"})
|
|
return
|
|
}
|
|
|
|
mongoDB := mongo.NewDatabaseServiceMongo(db)
|
|
dataID, err := mongoDB.GetmenuByID(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"data": dataID,
|
|
"message": "ID berhasil di cari",
|
|
})
|
|
}
|
|
|
|
func GettypeByID(c *gin.Context) {
|
|
local := os.Getenv("MONGODB_DEV_KEUANGAN")
|
|
db := database.New(local).GetMongoDB()
|
|
if db == nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
|
|
return
|
|
}
|
|
|
|
id := c.Param("id")
|
|
|
|
if id == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"message": "Parameter 'id' dibutuhkan"})
|
|
return
|
|
}
|
|
|
|
mongoDB := mongo.NewDatabaseServiceMongo(db)
|
|
dataID, err := mongoDB.GettypeByID(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"data": dataID,
|
|
"message": "ID berhasil di cari",
|
|
})
|
|
}
|
|
|
|
func GetroleByTypeWithUserInfo(c *gin.Context) {
|
|
local := os.Getenv("MONGODB_DEV_KEUANGAN")
|
|
db := database.New(local).GetMongoDB()
|
|
if db == nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
|
|
return
|
|
}
|
|
|
|
id := c.Param("id")
|
|
|
|
if id == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"message": "Parameter 'id' dibutuhkan"})
|
|
return
|
|
}
|
|
|
|
mongoDB := mongo.NewDatabaseServiceMongo(db)
|
|
dataID, err := mongoDB.GetGroupedBytypeIDFlatWithMenu(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"data": dataID,
|
|
"message": "ID berhasil di cari",
|
|
})
|
|
}
|
|
|
|
//func GetBytypeIDdelete(c *gin.Context) {
|
|
// local := os.Getenv("MONGODB_DEV_KEUANGAN")
|
|
// db := database.New(local).GetMongoDB()
|
|
// if db == nil {
|
|
// c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
|
|
// return
|
|
// }
|
|
//
|
|
// id := c.Param("id")
|
|
//
|
|
// if id == "" {
|
|
// c.JSON(http.StatusBadRequest, gin.H{"message": "Parameter 'id' dibutuhkan"})
|
|
// return
|
|
// }
|
|
//
|
|
// mongoDB := mongo.NewDatabaseServiceMongo(db)
|
|
// err := mongoDB.GetBytypeIDdelete(id)
|
|
// if err != nil {
|
|
// c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
|
|
// return
|
|
// }
|
|
//
|
|
// c.JSON(http.StatusOK, gin.H{
|
|
// "message": "ID berhasil di delete",
|
|
// })
|
|
//}
|
|
//
|
|
//func InsertRoles(c *gin.Context) {
|
|
// local := os.Getenv("MONGODB_DEV_KEUANGAN")
|
|
// db := database.New(local).GetMongoDB()
|
|
// if db == nil {
|
|
// c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
|
|
// return
|
|
// }
|
|
// mongoDB := mongo.NewDatabaseServiceMongo(db)
|
|
// var req []MenuHandler.Rolemenu
|
|
// if err := c.ShouldBindJSON(&req); err != nil {
|
|
// c.JSON(http.StatusBadRequest, gin.H{"message": "Invalid request format"})
|
|
// return
|
|
// }
|
|
// errInsert := mongoDB.InsertRole(req)
|
|
// if errInsert != nil {
|
|
// c.JSON(http.StatusInternalServerError, gin.H{"message": errInsert.Error()})
|
|
// return
|
|
// }
|
|
// c.JSON(http.StatusOK, gin.H{"message": "Role berhasil di Buat"})
|
|
//}
|
|
|
|
func GabunganApi(c *gin.Context) {
|
|
local := os.Getenv("MONGODB_DEV_KEUANGAN")
|
|
db := database.New(local).GetMongoDB()
|
|
if db == nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
|
|
return
|
|
}
|
|
|
|
mongoDB := mongo.NewDatabaseServiceMongo(db)
|
|
|
|
var req []MenuHandler.Rolemenu
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"message": "Invalid request format"})
|
|
return
|
|
}
|
|
|
|
id := c.DefaultQuery("id", "")
|
|
if id == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"message": "Parameter 'id' dibutuhkan"})
|
|
return
|
|
}
|
|
|
|
err := mongoDB.GetBytypeIDdelete(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
|
|
return
|
|
}
|
|
|
|
errInsert := mongoDB.InsertRole(req)
|
|
if errInsert != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": errInsert.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "Update berhasil",
|
|
})
|
|
}
|