Capstone/ui/db/vendors.go

79 lines
2.2 KiB
Go
Raw Normal View History

2023-10-31 21:31:26 -04:00
package db
import (
"context"
"git.preston-baxter.com/Preston_PLB/capstone/frontend-service/config"
"git.preston-baxter.com/Preston_PLB/capstone/frontend-service/db/models"
"go.mongodb.org/mongo-driver/bson"
2023-11-02 19:23:07 -04:00
"go.mongodb.org/mongo-driver/bson/primitive"
2023-11-02 21:06:35 -04:00
"go.mongodb.org/mongo-driver/mongo"
2023-10-31 21:31:26 -04:00
"go.mongodb.org/mongo-driver/mongo/options"
)
2023-11-16 20:45:48 -05:00
// return all vendor accounts for a user
2023-11-14 13:48:12 -05:00
func (db *DB) FindAllVendorAccountsByUser(userId primitive.ObjectID) ([]models.VendorAccount, error) {
2023-10-31 21:31:26 -04:00
conf := config.Config()
opts := options.Find()
res, err := db.client.Database(conf.Mongo.EntDb).Collection(conf.Mongo.EntCol).Find(context.Background(), bson.M{"user_id": userId, "obj_info.ent": models.VENDOR_ACCOUNT_TYPE}, opts)
if err != nil {
2023-11-02 21:06:35 -04:00
if err == mongo.ErrNoDocuments {
return nil, nil
}
2023-10-31 21:31:26 -04:00
return nil, err
}
vendors := []models.VendorAccount{}
2023-11-02 19:23:07 -04:00
err = res.All(context.Background(), &vendors)
2023-10-31 21:31:26 -04:00
if err != nil {
return nil, err
}
return vendors, nil
}
2023-11-16 20:45:48 -05:00
// find vendor for user by name
2023-11-14 14:08:53 -05:00
func (db *DB) FindVendorAccountByUser(userId primitive.ObjectID, name string) (*models.VendorAccount, error) {
2023-11-14 13:48:12 -05:00
conf := config.Config()
opts := options.FindOne()
res := db.client.Database(conf.Mongo.EntDb).Collection(conf.Mongo.EntCol).FindOne(context.Background(), bson.M{"user_id": userId, "name": name, "obj_info.ent": models.VENDOR_ACCOUNT_TYPE}, opts)
if res.Err() != nil {
if res.Err() == mongo.ErrNoDocuments {
return nil, nil
}
return nil, res.Err()
}
vendor := &models.VendorAccount{}
err := res.Decode(vendor)
if err != nil {
return nil, err
}
return vendor, nil
}
2023-11-16 20:45:48 -05:00
// find vendoraccount by its unique id
func (db *DB) FindVendorAccountById(vendorId primitive.ObjectID) (*models.VendorAccount, error) {
conf := config.Config()
opts := options.FindOne()
res := db.client.Database(conf.Mongo.EntDb).Collection(conf.Mongo.EntCol).FindOne(context.Background(), bson.M{"_id": vendorId, "obj_info.ent": models.VENDOR_ACCOUNT_TYPE}, opts)
if res.Err() != nil {
if res.Err() == mongo.ErrNoDocuments {
return nil, nil
}
return nil, res.Err()
}
vendor := &models.VendorAccount{}
err := res.Decode(vendor)
if err != nil {
return nil, err
}
return vendor, nil
}