58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package encounter
|
|
|
|
import (
|
|
"api-poliklinik/internal/database"
|
|
"api-poliklinik/pkg/database/mongo"
|
|
"api-poliklinik/pkg/models/mongo/encounter"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
func Getencounter(c *gin.Context) {
|
|
local := os.Getenv("MONGODB_DEV_LOCAL")
|
|
db := database.New(local).GetMongoDB()
|
|
if db == nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
|
|
return
|
|
}
|
|
|
|
mongoDB := mongo.NewDatabaseServiceMongo(db)
|
|
dataEncounter, errSelect := mongoDB.Getencounter()
|
|
if errSelect != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": errSelect.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"data": dataEncounter,
|
|
"message": "Encounter Sukses Ter-ambil ",
|
|
})
|
|
}
|
|
|
|
func InsertEncounter(c *gin.Context) {
|
|
local := os.Getenv("MONGODB_DEV_LOCAL")
|
|
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 *encounter.Encounter
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"message": "Invalid request format"})
|
|
return
|
|
}
|
|
dateCreated := time.Now().Format("2006-01-02 15:04:05")
|
|
req.ResourceType = "Encounter"
|
|
req.CreatedAt = dateCreated
|
|
req.UpdatedAt = dateCreated
|
|
|
|
errInsert := mongoDB.InsertEncounter(req)
|
|
if errInsert != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": errInsert.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "Encounter berhasil di Buat"})
|
|
}
|