ess-etl-go/pkg/adapter/repository/mongodb/employee_wi/insert.go

39 lines
903 B
Go

package employee_wi
import (
"context"
"fmt"
"gitea.urkob.com/urko/ess-etl-go/pkg/domain"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func (p *Repo) InsertOne(ctx context.Context, dto *domain.EmployeeWorkInformation) (string, error) {
inserted, err := p.collection.InsertOne(ctx, dto)
if err != nil {
return "", fmt.Errorf("p.collection.InsertOne: %s", err)
}
oid, ok := inserted.InsertedID.(primitive.ObjectID)
if !ok {
return "", fmt.Errorf("res.InsertedID is not valid _id")
}
return oid.Hex(), nil
}
func (p *Repo) InsertMany(ctx context.Context, dto []domain.EmployeeWorkInformation) error {
elements := make([]interface{}, 0, len(dto))
for _, v := range dto {
v.ID = primitive.NewObjectID()
elements = append(elements, v)
}
_, err := p.collection.InsertMany(ctx, elements)
if err != nil {
return fmt.Errorf("p.collection.InsertMany: %s", err)
}
return nil
}