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, req)
	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
}
