237 lines
6.2 KiB
Go
237 lines
6.2 KiB
Go
package Master
|
|
|
|
import (
|
|
"api-poliklinik/internal/database"
|
|
"api-poliklinik/pkg/database/mongo"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
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",
|
|
})
|
|
}
|
|
|
|
func GetSMF(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)
|
|
dataSmf, errSelect := mongoDB.Getsmf()
|
|
if errSelect != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": errSelect.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"data": dataSmf,
|
|
"message": "SMF Sukses Ter-ambil ",
|
|
})
|
|
}
|
|
|
|
func GetICD(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
|
|
}
|
|
// Default values
|
|
limit := int64(0)
|
|
skip := int64(0)
|
|
|
|
// Ambil dari query jika tersedia
|
|
if l := c.Query("limit"); l != "" {
|
|
if parsed, err := strconv.ParseInt(l, 10, 64); err == nil {
|
|
limit = parsed
|
|
}
|
|
}
|
|
if s := c.Query("skip"); s != "" {
|
|
if parsed, err := strconv.ParseInt(s, 10, 64); err == nil {
|
|
skip = parsed
|
|
}
|
|
}
|
|
mongoDB := mongo.NewDatabaseServiceMongo(db)
|
|
dataSmf, errSelect := mongoDB.GetICD(limit, skip)
|
|
if errSelect != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": errSelect.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"data": dataSmf,
|
|
"message": "ICD Sukses Ter-ambil ",
|
|
})
|
|
}
|