73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
package Patient
|
|
|
|
import (
|
|
"api-poliklinik/internal/database"
|
|
"api-poliklinik/pkg/database/mongo"
|
|
"api-poliklinik/pkg/models/patient"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
func InsertPatient(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 *patient.Patient
|
|
err := c.Bind(&req)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
|
|
return
|
|
}
|
|
|
|
dateCreated := time.Now().Format("2006-01-02 15:04:05")
|
|
req.ResourceType = "Patient"
|
|
req.CreatedAt = dateCreated
|
|
|
|
errInsert := mongoDB.InsertPatient(req)
|
|
if errInsert != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": errInsert.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "Patient successfully inserted"})
|
|
}
|
|
|
|
func GetAllPatient(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
|
|
}
|
|
|
|
// Default values
|
|
limit := int64(10)
|
|
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)
|
|
dataPatient, errSelect := mongoDB.GetAllDataPatient(limit, skip)
|
|
if errSelect != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": errSelect.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, dataPatient)
|
|
}
|