Implement ObjectID Conversion in GetByIDs Method of Company Repository

- Enhanced the GetByIDs method to convert string IDs to BSON ObjectIDs before querying the database, ensuring proper data type handling.
- This change improves the robustness of the method by preventing potential errors related to ID format mismatches during database operations.
This commit is contained in:
n.nakhostin
2025-09-22 14:04:15 +03:30
parent 5bd8c2b53d
commit c2531a005e
+10 -1
View File
@@ -101,7 +101,16 @@ func (r *companyRepository) GetByID(ctx context.Context, id string) (*Company, e
// GetByIDs retrieves companies by IDs
func (r *companyRepository) GetByIDs(ctx context.Context, ids []string) ([]Company, error) {
companies, err := r.ormRepo.FindAll(ctx, bson.M{"_id": bson.M{"$in": ids}}, orm.NewPaginationBuilder().Build())
objectIDs := make([]bson.ObjectID, len(ids))
for i, id := range ids {
objectID, err := bson.ObjectIDFromHex(id)
if err != nil {
return nil, err
}
objectIDs[i] = objectID
}
companies, err := r.ormRepo.FindAll(ctx, bson.M{"_id": bson.M{"$in": objectIDs}}, orm.NewPaginationBuilder().Build())
if err != nil {
return nil, err
}