Files
2025-07-01 12:42:28 +07:00

122 lines
2.8 KiB
Go

package mongo
import (
"api-poliklinik/pkg/models/mongo/location"
"context"
"go.mongodb.org/mongo-driver/bson"
"log"
"time"
)
func (s *DatabaseService) Insertlokasi(req location.Location) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, err := s.DBMongo.Collection("location").InsertOne(ctx, req)
if err != nil {
log.Println(err)
return err
}
return nil
}
func (s *DatabaseService) Getlocation() ([]*location.Location, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
datalocation, err := s.DBMongo.Collection("location").Find(ctx, bson.D{})
if err != nil {
log.Println(err)
}
var location []*location.Location
err = datalocation.All(ctx, &location)
if err != nil {
log.Println(err)
return nil, err
}
return location, nil
}
func (s *DatabaseService) Getlocationbysubs(subsistem string) ([]*location.Location, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
filter := bson.M{
"$and": []bson.M{
{
"$or": []bson.M{
{
"type.coding": bson.M{
"$elemMatch": bson.M{
"code": "SUBSISTEM",
"display": bson.M{"$regex": subsistem, "$options": "i"},
},
},
},
{
"type.text": bson.M{"$regex": subsistem, "$options": "i"},
},
},
},
{
"type.coding": bson.M{
"$elemMatch": bson.M{
"code": "SMF",
"display": bson.M{"$exists": true, "$regex": ".+"},
},
},
},
},
}
datalocation, err := s.DBMongo.Collection("location").Find(ctx, filter)
if err != nil {
log.Println(err)
}
var location []*location.Location
err = datalocation.All(ctx, &location)
if err != nil {
log.Println(err)
return nil, err
}
return location, nil
}
//func (s *DatabaseService) Getlocationbysubs(subsistem string) ([]*location.Location, error) {
// ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
// defer cancel()
//
// filter := bson.M{
// "$and": []bson.M{
// // Filter 1: Cari yang subsistem-nya cocok
// {
// "type": bson.M{
// "$elemMatch": bson.M{
// "coding": bson.M{
// "$elemMatch": bson.M{
// "code": "SUBSISTEM",
// "display": bson.M{"$regex": subsistem, "$options": "i"},
// },
// },
// },
// },
// },
// },
// }
//
// cursor, err := s.DBMongo.Collection("location").Find(ctx, filter)
// if err != nil {
// log.Printf("Error finding locations: %v", err)
// return nil, err
// }
// defer cursor.Close(ctx)
//
// var locations []*location.Location
// if err = cursor.All(ctx, &locations); err != nil {
// log.Printf("Error decoding locations: %v", err)
// return nil, err
// }
//
// log.Printf("Found %d locations for subsistem '%s' with SMF", len(locations), subsistem)
// return locations, nil
//}