Capstone/ui/db/user.go

78 lines
1.7 KiB
Go
Raw Normal View History

2023-10-28 14:42:29 -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-10-28 17:50:44 -04:00
"go.mongodb.org/mongo-driver/mongo"
2023-10-28 14:42:29 -04:00
"go.mongodb.org/mongo-driver/mongo/options"
)
func (db *DB) FindUserByEmail(email string) (*models.User, error) {
conf := config.Config()
opts := options.FindOne()
res := db.client.Database(conf.Mongo.EntDb).Collection(conf.Mongo.EntCol).FindOne(context.Background(), bson.M{"email": email}, opts)
if res.Err() != nil {
2023-10-28 17:50:44 -04:00
if res.Err() == mongo.ErrNoDocuments {
return nil, nil
}
2023-10-28 14:42:29 -04:00
return nil, res.Err()
}
user := &models.User{}
err := res.Decode(user)
if err != nil {
return nil, err
}
return user, nil
}
func (db *DB) FindUserById(id string) (*models.User, error) {
conf := config.Config()
opts := options.FindOne()
res := db.client.Database(conf.Mongo.EntDb).Collection(conf.Mongo.EntCol).FindOne(context.Background(), bson.M{"user_id": id}, opts)
if res.Err() != nil {
2023-10-28 17:50:44 -04:00
if res.Err() == mongo.ErrNoDocuments {
return nil, nil
}
2023-10-28 14:42:29 -04:00
return nil, res.Err()
}
user := &models.User{}
err := res.Decode(user)
if err != nil {
return nil, err
}
return user, nil
}
func (db *DB) FindAllUsers() ([]models.User, error) {
conf := config.Config()
opts := options.Find()
res, err := db.client.Database(conf.Mongo.EntDb).Collection(conf.Mongo.EntCol).Find(context.Background(), bson.M{"obj_info.ent": models.USER_TYPE}, opts)
2023-10-28 14:42:29 -04:00
if err != nil {
2023-10-28 17:50:44 -04:00
if err == mongo.ErrNoDocuments {
return nil, nil
}
2023-10-28 14:42:29 -04:00
return nil, res.Err()
}
users := []models.User{}
err = res.All(context.Background(), users)
if err != nil {
return nil, err
}
return users, nil
}