B: Rework Model framework and finish vendor model so UI and callback controller can be built

This commit is contained in:
Preston Baxter 2023-11-01 21:40:50 -05:00
parent b1a573b840
commit ca70d241d6
11 changed files with 104 additions and 89 deletions

View File

@ -80,16 +80,12 @@ func SignUpHandler (c *gin.Context) {
return
}
//add vendor accounts
mongo.AddAccountsForUser(user)
now := time.Now().Unix()
exp := time.Now().Add(12 * time.Hour).Unix()
//build jwt
token := jwt.NewWithClaims(jwt.SigningMethodHS256,
AuthClaims{
Subject: user.UserId,
Subject: user.MongoId().String(),
Expires: exp,
IssuedAt: now,
NotBefore: now,
@ -163,7 +159,7 @@ func LoginHandler(c *gin.Context) {
//build jwt
token := jwt.NewWithClaims(jwt.SigningMethodHS256,
AuthClaims{
Subject: user.UserId,
Subject: user.MongoId().String(),
Expires: exp,
IssuedAt: now,
NotBefore: now,

View File

@ -89,7 +89,6 @@ func AuthMiddleware(strict bool) gin.HandlerFunc {
}
}
if !parsedToken.Valid {
if strict {
log.Warn("Redirecting, jwt invalid")

View File

@ -36,7 +36,3 @@ func BuildRouter(r *gin.Engine) {
dashboard.Use(AuthMiddleware(true))
dashboard.GET("/", DashboardPage)
}

View File

@ -25,6 +25,7 @@ func SignUpPage(c *gin.Context) {
}
func DashboardPage(c *gin.Context) {
//Get vendors
if raw, exists := c.Get(USER_OBJ_KEY); exists {
if user, ok := raw.(*models.User); ok {
renderTempl(c, templates.DashboardPage(user))

View File

@ -19,7 +19,6 @@ func badRequest(c *gin.Context, reason string) {
c.JSON(400, map[string]string{"error": reason})
}
func serverError(c *gin.Context, reason string) {
c.JSON(504, map[string]string{"error": reason})
}

View File

@ -2,14 +2,19 @@ package db
import (
"context"
"errors"
"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"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Model interface {
Save(client *mongo.Client) error
Delete(client *mongo.Client) error
MongoId() primitive.ObjectID
UpdateObjectInfo()
}
type DB struct {
@ -26,5 +31,33 @@ func NewClient(uri string) (*DB, error) {
}
func (db *DB) SaveModel(m Model) error {
return m.Save(db.client)
conf := config.Config()
opts := options.Update().SetUpsert(true)
res, err := db.client.Database(conf.Mongo.EntDb).Collection(conf.Mongo.EntCol).UpdateOne(context.Background(), bson.M{"_id": m.MongoId()}, bson.M{"$set": m}, opts)
if err != nil {
return err
}
if res.MatchedCount == 0 && res.ModifiedCount == 0 && res.UpsertedCount == 0 {
return errors.New("Failed to update vendor account properly")
}
return nil
}
func (db *DB) DeleteModel(m Model) error {
conf := config.Config()
opts := options.Delete()
res, err := db.client.Database(conf.Mongo.EntDb).Collection(conf.Mongo.EntCol).DeleteOne(context.Background(), bson.M{"_id": m.MongoId()}, opts)
if err != nil {
return err
}
if res.DeletedCount == 0 {
return errors.New("There was no item to delete")
}
return nil
}

View File

@ -1,66 +1,36 @@
package models
import (
"context"
"errors"
"time"
"git.preston-baxter.com/Preston_PLB/capstone/frontend-service/config"
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const USER_TYPE string = "user"
type User struct {
CommonFields `bson:"obj_info"`
*CommonFields `bson:"obj_info"`
mongoId primitive.ObjectID `bson:"_id,omitempty"`
UserId string `bson:"user_id,omitempty"`
Email string `bson:"email,omitempty"`
PassowrdHash string `bson:"password_hash,omitempty"`
}
func (user *User) Save(client *mongo.Client) error {
conf := config.Config()
func (user *User) MongoId() primitive.ObjectID {
if user.mongoId.IsZero() {
now := time.Now()
user.EntityType = USER_TYPE
user.CreatedAt = now
user.UpdatedAt = now
user.UserId = uuid.New().String()
user.mongoId = primitive.NewObjectIDFromTimestamp(now)
}
opts := options.Update().SetUpsert(true)
res, err := client.Database(conf.Mongo.EntDb).Collection(conf.Mongo.EntCol).UpdateOne(context.Background(), bson.M{"user_id": user.UserId}, bson.M{"$set": user},opts)
if err != nil {
return err
return user.mongoId
}
if res.MatchedCount == 0 && res.ModifiedCount == 0 && res.UpsertedCount == 0 {
return errors.New("Failed to update client properly")
func (user *User) UpdateObjectInfo() {
now := time.Now()
if user.CommonFields == nil {
user.CommonFields = new(CommonFields)
user.EntityType = USER_TYPE
user.CreatedAt = now
}
return nil
}
func (user *User) Delete(client *mongo.Client) error {
conf := config.Config()
opts := options.Delete()
res, err := client.Database(conf.Mongo.EntDb).Collection(conf.Mongo.EntCol).DeleteOne(context.Background(), bson.M{"_id": user.mongoId}, opts)
if err != nil {
return err
}
if res.DeletedCount == 0 {
return errors.New("There was no item to delete")
}
return nil
user.UpdatedAt = now
}

View File

@ -1,24 +1,46 @@
package models
import "go.mongodb.org/mongo-driver/bson/primitive"
import (
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
)
const VENDOR_TYPE = "vendor"
const VENDOR_ACCOUNT_TYPE = "vendor_account"
type Vendor struct {
CommonFields `bson:"obj_info"`
mongoId primitive.ObjectID `bson:"_id,omitempty"`
VendorId string `bson:"vendor_id,omitempty"`
Name string `bson:"name,omitempty"`
OAuthUrl string `bson:"oauth_url"`
type OauthCredential struct {
AccessToken string `bson:"access_token,omitempty" json:"access_token,omitempty"`
ExpiresIn int `bson:"expires_in,omitempty" json:"expires_in,omitempty"`
ExpiresAt time.Time `bson:"expires_at,omitempty" json:"expires_at,omitempty"`
TokenType string `bson:"token_type,omitempty" json:"token_type,omitempty"`
Scope string `bson:"scope,omitempty" json:"scope,omitempty"`
RefreshToken string `bson:"refresh_token,omitempty" json:"refresh_token,omitempty"`
}
type VendorAccount struct {
CommonFields `bson:"obj_info"`
*Vendor
*CommonFields `bson:"obj_info"`
mongoId primitive.ObjectID `bson:"_id,omitempty"`
UserId string `bson:"user_id,omitempty"`
UserId primitive.ObjectID `bson:"user_id,omitempty"`
Secret string `bson:"secret,omitempty"`
VendorId string `bson:"vendor_id,omitempty"`
Authenticated bool `bson:"authenticated,omitempty"`
OauthCredentials *OauthCredential `bson:"ouath_credentials,omitempty"`
}
func (va *VendorAccount) MongoId() primitive.ObjectID {
if va.mongoId.IsZero() {
now := time.Now()
va.mongoId = primitive.NewObjectIDFromTimestamp(now)
}
return va.mongoId
}
func (va *VendorAccount) UpdateObjectInfo() {
now := time.Now()
if va.CommonFields == nil {
va.CommonFields = new(CommonFields)
va.EntityType = VENDOR_ACCOUNT_TYPE
va.CreatedAt = now
}
va.UpdatedAt = now
}

View File

@ -36,7 +36,7 @@ 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, "obj_info.ent": models.USER_TYPE}, opts)
res := db.client.Database(conf.Mongo.EntDb).Collection(conf.Mongo.EntCol).FindOne(context.Background(), bson.M{"_id": id, "obj_info.ent": models.USER_TYPE}, opts)
if res.Err() != nil {
if res.Err() == mongo.ErrNoDocuments {

View File

@ -26,4 +26,3 @@ func (db *DB) FindVendorAccountByUser(userId string) ([]models.VendorAccount, er
return vendors, nil
}