Files
api-tes/pkg/database/mongo/patient.go
2025-03-19 13:47:29 +07:00

45 lines
1014 B
Go

package mongo
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"log"
"template_blueprint/pkg/models/patient"
"time"
)
func (s *DatabaseService) InsertPatient(req *patient.Patient) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, err := s.DB.Collection("patient").InsertOne(ctx, patient.Patient{
ID: req.ID,
NoRM: req.NoRM,
Name: req.Name,
Telecom: req.Telecom,
Gender: req.Gender,
BirthDate: req.BirthDate,
Address: req.Address,
})
if err != nil {
log.Println(err)
return err
}
return nil
}
func (s *DatabaseService) GetAllDataPatient() ([]*patient.Patient, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
dataUser, err := s.DB.Collection("patient").Find(ctx, bson.D{})
if err != nil {
log.Println(err)
}
var users []*patient.Patient
err = dataUser.All(ctx, &users)
if err != nil {
log.Println(err)
return nil, err
}
return users, nil
}