Files

61 lines
1.6 KiB
Go

package appointment
import (
"api-poliklinik/internal/database"
"api-poliklinik/pkg/database/mongo"
"api-poliklinik/pkg/models/mongo/appointment"
"github.com/gin-gonic/gin"
"net/http"
"os"
"time"
)
func Getappointment(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)
dataAppointment, errSelect := mongoDB.Getappointment()
if errSelect != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": errSelect.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"data": dataAppointment,
"message": "Apponment Sukses Ter-ambil ",
})
}
func Insertappointment(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 *appointment.Appointment
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 = "Appointment"
req.CreatedAt = dateCreated
req.UpdatedAt = dateCreated
errInsert := mongoDB.InsertApointmongo(req)
if errInsert != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": errInsert.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"id": req.ID.Hex(),
"message": "Janji Temu berhasil di Buat",
})
}