B: Rework Model framework and finish vendor model so UI and callback controller can be built
This commit is contained in:
parent
b1a573b840
commit
ca70d241d6
|
@ -80,16 +80,12 @@ func SignUpHandler (c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//add vendor accounts
|
|
||||||
|
|
||||||
mongo.AddAccountsForUser(user)
|
|
||||||
|
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
exp := time.Now().Add(12 * time.Hour).Unix()
|
exp := time.Now().Add(12 * time.Hour).Unix()
|
||||||
//build jwt
|
//build jwt
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256,
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256,
|
||||||
AuthClaims{
|
AuthClaims{
|
||||||
Subject: user.UserId,
|
Subject: user.MongoId().String(),
|
||||||
Expires: exp,
|
Expires: exp,
|
||||||
IssuedAt: now,
|
IssuedAt: now,
|
||||||
NotBefore: now,
|
NotBefore: now,
|
||||||
|
@ -163,7 +159,7 @@ func LoginHandler(c *gin.Context) {
|
||||||
//build jwt
|
//build jwt
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256,
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256,
|
||||||
AuthClaims{
|
AuthClaims{
|
||||||
Subject: user.UserId,
|
Subject: user.MongoId().String(),
|
||||||
Expires: exp,
|
Expires: exp,
|
||||||
IssuedAt: now,
|
IssuedAt: now,
|
||||||
NotBefore: now,
|
NotBefore: now,
|
||||||
|
|
|
@ -89,7 +89,6 @@ func AuthMiddleware(strict bool) gin.HandlerFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if !parsedToken.Valid {
|
if !parsedToken.Valid {
|
||||||
if strict {
|
if strict {
|
||||||
log.Warn("Redirecting, jwt invalid")
|
log.Warn("Redirecting, jwt invalid")
|
||||||
|
|
|
@ -36,7 +36,3 @@ func BuildRouter(r *gin.Engine) {
|
||||||
dashboard.Use(AuthMiddleware(true))
|
dashboard.Use(AuthMiddleware(true))
|
||||||
dashboard.GET("/", DashboardPage)
|
dashboard.GET("/", DashboardPage)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,7 @@ func SignUpPage(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func DashboardPage(c *gin.Context) {
|
func DashboardPage(c *gin.Context) {
|
||||||
|
//Get vendors
|
||||||
if raw, exists := c.Get(USER_OBJ_KEY); exists {
|
if raw, exists := c.Get(USER_OBJ_KEY); exists {
|
||||||
if user, ok := raw.(*models.User); ok {
|
if user, ok := raw.(*models.User); ok {
|
||||||
renderTempl(c, templates.DashboardPage(user))
|
renderTempl(c, templates.DashboardPage(user))
|
||||||
|
|
|
@ -19,7 +19,6 @@ func badRequest(c *gin.Context, reason string) {
|
||||||
c.JSON(400, map[string]string{"error": reason})
|
c.JSON(400, map[string]string{"error": reason})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func serverError(c *gin.Context, reason string) {
|
func serverError(c *gin.Context, reason string) {
|
||||||
c.JSON(504, map[string]string{"error": reason})
|
c.JSON(504, map[string]string{"error": reason})
|
||||||
}
|
}
|
||||||
|
|
39
ui/db/db.go
39
ui/db/db.go
|
@ -2,14 +2,19 @@ package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"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"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Model interface {
|
type Model interface {
|
||||||
Save(client *mongo.Client) error
|
MongoId() primitive.ObjectID
|
||||||
Delete(client *mongo.Client) error
|
UpdateObjectInfo()
|
||||||
}
|
}
|
||||||
|
|
||||||
type DB struct {
|
type DB struct {
|
||||||
|
@ -26,5 +31,33 @@ func NewClient(uri string) (*DB, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) SaveModel(m Model) 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
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,66 +1,36 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"time"
|
"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/bson/primitive"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const USER_TYPE string = "user"
|
const USER_TYPE string = "user"
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
CommonFields `bson:"obj_info"`
|
*CommonFields `bson:"obj_info"`
|
||||||
mongoId primitive.ObjectID `bson:"_id,omitempty"`
|
mongoId primitive.ObjectID `bson:"_id,omitempty"`
|
||||||
UserId string `bson:"user_id,omitempty"`
|
|
||||||
Email string `bson:"email,omitempty"`
|
Email string `bson:"email,omitempty"`
|
||||||
PassowrdHash string `bson:"password_hash,omitempty"`
|
PassowrdHash string `bson:"password_hash,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (user *User) Save(client *mongo.Client) error {
|
func (user *User) MongoId() primitive.ObjectID {
|
||||||
conf := config.Config()
|
|
||||||
|
|
||||||
if user.mongoId.IsZero() {
|
if user.mongoId.IsZero() {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
user.EntityType = USER_TYPE
|
|
||||||
user.CreatedAt = now
|
|
||||||
user.UpdatedAt = now
|
|
||||||
user.UserId = uuid.New().String()
|
|
||||||
user.mongoId = primitive.NewObjectIDFromTimestamp(now)
|
user.mongoId = primitive.NewObjectIDFromTimestamp(now)
|
||||||
}
|
}
|
||||||
|
|
||||||
opts := options.Update().SetUpsert(true)
|
return user.mongoId
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if res.MatchedCount == 0 && res.ModifiedCount == 0 && res.UpsertedCount == 0 {
|
func (user *User) UpdateObjectInfo() {
|
||||||
return errors.New("Failed to update client properly")
|
now := time.Now()
|
||||||
|
if user.CommonFields == nil {
|
||||||
|
user.CommonFields = new(CommonFields)
|
||||||
|
user.EntityType = USER_TYPE
|
||||||
|
user.CreatedAt = now
|
||||||
}
|
}
|
||||||
|
user.UpdatedAt = 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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,24 +1,46 @@
|
||||||
package models
|
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"
|
const VENDOR_ACCOUNT_TYPE = "vendor_account"
|
||||||
|
|
||||||
type Vendor struct {
|
type OauthCredential struct {
|
||||||
CommonFields `bson:"obj_info"`
|
AccessToken string `bson:"access_token,omitempty" json:"access_token,omitempty"`
|
||||||
mongoId primitive.ObjectID `bson:"_id,omitempty"`
|
ExpiresIn int `bson:"expires_in,omitempty" json:"expires_in,omitempty"`
|
||||||
VendorId string `bson:"vendor_id,omitempty"`
|
ExpiresAt time.Time `bson:"expires_at,omitempty" json:"expires_at,omitempty"`
|
||||||
Name string `bson:"name,omitempty"`
|
TokenType string `bson:"token_type,omitempty" json:"token_type,omitempty"`
|
||||||
OAuthUrl string `bson:"oauth_url"`
|
Scope string `bson:"scope,omitempty" json:"scope,omitempty"`
|
||||||
|
RefreshToken string `bson:"refresh_token,omitempty" json:"refresh_token,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type VendorAccount struct {
|
type VendorAccount struct {
|
||||||
CommonFields `bson:"obj_info"`
|
*CommonFields `bson:"obj_info"`
|
||||||
*Vendor
|
|
||||||
mongoId primitive.ObjectID `bson:"_id,omitempty"`
|
mongoId primitive.ObjectID `bson:"_id,omitempty"`
|
||||||
UserId string `bson:"user_id,omitempty"`
|
UserId primitive.ObjectID `bson:"user_id,omitempty"`
|
||||||
Secret string `bson:"secret,omitempty"`
|
Secret string `bson:"secret,omitempty"`
|
||||||
VendorId string `bson:"vendor_id,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
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ func (db *DB) FindUserById(id string) (*models.User, error) {
|
||||||
conf := config.Config()
|
conf := config.Config()
|
||||||
|
|
||||||
opts := options.FindOne()
|
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() != nil {
|
||||||
if res.Err() == mongo.ErrNoDocuments {
|
if res.Err() == mongo.ErrNoDocuments {
|
||||||
|
|
|
@ -26,4 +26,3 @@ func (db *DB) FindVendorAccountByUser(userId string) ([]models.VendorAccount, er
|
||||||
|
|
||||||
return vendors, nil
|
return vendors, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue