50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package mongo
|
|
|
|
import (
|
|
"api-poliklinik/pkg/models/mongo/appointment"
|
|
"context"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
func (s *DatabaseService) Getappointment() ([]*appointment.Appointment, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
dataappointment, err := s.DBMongo.Collection("appointment").Find(ctx, bson.D{})
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
var appointment []*appointment.Appointment
|
|
err = dataappointment.All(ctx, &appointment)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return nil, err
|
|
}
|
|
return appointment, nil
|
|
}
|
|
|
|
func (s *DatabaseService) InsertApointmongo(req *appointment.Appointment) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
// Jika ID belum diinisialisasi, set ID baru
|
|
if req.ID.IsZero() {
|
|
req.ID = primitive.NewObjectID()
|
|
}
|
|
|
|
result, err := s.DBMongo.Collection("appointment").InsertOne(ctx, req)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return err
|
|
}
|
|
|
|
// Mengambil ID yang dihasilkan dan memasukkannya kembali ke struct
|
|
if oid, ok := result.InsertedID.(primitive.ObjectID); ok {
|
|
req.ID = oid
|
|
}
|
|
|
|
return nil
|
|
}
|