servicemongo
This commit is contained in:
41
pkg/database/mongo/patient.go
Normal file
41
pkg/database/mongo/patient.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"api-poliklinik/pkg/models/patient"
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (s *DatabaseService) InsertPatient(req *patient.Patient) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
_, err := s.DBMongo.Collection("patient").InsertOne(ctx, req)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *DatabaseService) GetAllDataPatient(limit int64, skip int64) ([]*patient.Patient, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
findOptions := options.Find()
|
||||
findOptions.SetLimit(limit)
|
||||
findOptions.SetSkip(skip)
|
||||
|
||||
dataUser, err := s.DBMongo.Collection("patient").Find(ctx, bson.D{}, findOptions)
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user