terbaru
This commit is contained in:
No files matched your search
@@ -0,0 +1,73 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"api-poliklinik/pkg/models/mongo/relatedperson"
|
||||
"context"
|
||||
"fmt"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (s *DatabaseService) GetAllDataRelatedPerson(limit int64, skip int64) ([]*relatedperson.RelatedPerson, 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("relatedperson").Find(ctx, bson.D{}, findOptions)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
var users []*relatedperson.RelatedPerson
|
||||
err = dataUser.All(ctx, &users)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (s *DatabaseService) InsertRelatedPerson(req *relatedperson.RelatedPerson) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
_, err := s.DBMongo.Collection("relatedperson").InsertOne(ctx, req)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *DatabaseService) DeleteRelatedPerson(id string, req *relatedperson.DeletedataRelatedPerson) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
objectID, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
log.Println("Invalid ID:", err)
|
||||
return err
|
||||
}
|
||||
(*req).ID = ""
|
||||
filter := bson.M{"_id": objectID}
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"active": req.Active,
|
||||
},
|
||||
}
|
||||
|
||||
res, err := s.DBMongo.Collection("relatedperson").UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Println("Error updating document:", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if res.MatchedCount == 0 {
|
||||
return fmt.Errorf("no document found with id: %s", id)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user