Files
api-poliklinik/pkg/handlers/Master/master.go
T
2025-05-11 22:15:40 +07:00

182 lines
4.8 KiB
Go

package Master
import (
"api-poliklinik/internal/database"
"api-poliklinik/pkg/database/mongo"
"github.com/gin-gonic/gin"
"net/http"
"os"
)
func GetProvinsi(c *gin.Context) {
local := os.Getenv("MONGODB_DEV_MASTER")
db := database.New(local).GetMongoDB()
if db == nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
return
}
mongoDB := mongo.NewDatabaseServiceMongo(db)
dataProvinsi, errSelect := mongoDB.Getdataprovinsi()
if errSelect != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": errSelect.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"data": dataProvinsi,
"message": "Provinsi Sukses Ter-ambil ",
})
}
func GetKota(c *gin.Context) {
local := os.Getenv("MONGODB_DEV_MASTER")
db := database.New(local).GetMongoDB()
if db == nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
return
}
mongoDB := mongo.NewDatabaseServiceMongo(db)
dataKota, errSelect := mongoDB.Getdatakota()
if errSelect != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": errSelect.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"data": dataKota,
"message": "Kota Sukses Ter-ambil ",
})
}
func GetKecamatan(c *gin.Context) {
local := os.Getenv("MONGODB_DEV_MASTER")
db := database.New(local).GetMongoDB()
if db == nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
return
}
mongoDB := mongo.NewDatabaseServiceMongo(db)
datakecamatan, errSelect := mongoDB.Getdatakecamatan()
if errSelect != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": errSelect.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"data": datakecamatan,
"message": "Kecamatan Sukses Ter-ambil ",
})
}
func GetKelurahan(c *gin.Context) {
local := os.Getenv("MONGODB_DEV_MASTER")
db := database.New(local).GetMongoDB()
if db == nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
return
}
display := c.Param("name")
if display == "" {
c.JSON(http.StatusBadRequest, gin.H{"message": "Parameter 'name' dibutuhkan"})
return
}
mongoDB := mongo.NewDatabaseServiceMongo(db)
datakelurahan, errSelect := mongoDB.Getdatakelurahan(display)
if errSelect != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": errSelect.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"data": datakelurahan,
"message": "Keluruhan Sukses Ter-ambil ",
})
}
// Handler untuk mencari kelurahan berdasarkan query
func SearchKelurahan(c *gin.Context) {
local := os.Getenv("MONGODB_DEV_MASTER")
db := database.New(local).GetMongoDB()
if db == nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
return
}
query := c.Query("name")
if query == "" {
c.JSON(http.StatusBadRequest, gin.H{"message": "Parameter 'name' dibutuhkan"})
return
}
// Gunakan service database untuk pencarian
mongoDB := mongo.NewDatabaseServiceMongo(db)
dataKelurahan, errSearch := mongoDB.SearchKelurahan(query)
if errSearch != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": errSearch.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"data": dataKelurahan,
"message": "Pencarian kelurahan berhasil",
})
}
// Handler untuk mendapatkan hierarki berdasarkan kelurahan
func GetHierarchyByKelurahan(c *gin.Context) {
local := os.Getenv("MONGODB_DEV_MASTER")
db := database.New(local).GetMongoDB()
if db == nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
return
}
// Ambil ID kelurahan dari parameter path
kelurahanId := c.Param("id")
if kelurahanId == "" {
c.JSON(http.StatusBadRequest, gin.H{"message": "Parameter 'id' dibutuhkan"})
return
}
// Gunakan service database untuk mendapatkan hierarki
mongoDB := mongo.NewDatabaseServiceMongo(db)
hierarchy, errGet := mongoDB.GetHierarchyByKelurahan(kelurahanId)
if errGet != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": errGet.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"data": hierarchy,
"message": "Hierarki kelurahan berhasil diambil",
})
}
func GetHierarchyFromProvinsi(c *gin.Context) {
local := os.Getenv("MONGODB_DEV_MASTER")
db := database.New(local).GetMongoDB()
if db == nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
return
}
provinsiId := c.Param("id")
if provinsiId == "" {
c.JSON(http.StatusBadRequest, gin.H{"message": "Parameter 'id' dibutuhkan"})
return
}
mongoDB := mongo.NewDatabaseServiceMongo(db)
hierarchy, errGet := mongoDB.GetHierarchyFromProvinsi(provinsiId)
if errGet != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": errGet.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"data": hierarchy,
"message": "Hierarki dari provinsi berhasil diambil",
})
}