Created
September 11, 2019 14:48
-
-
Save WoolWarrior/add5a8adab34d4b8522be7008ba93cdb to your computer and use it in GitHub Desktop.
qor-admin replacing sqlite3 with dummy object and dynamoDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models | |
import ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"time" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/google/uuid" | |
"github.com/guregu/dynamo" | |
"github.com/qor/admin" | |
"github.com/qor/qor" | |
"github.com/qor/qor/resource" | |
"github.com/qor/roles" | |
) | |
type Product struct { | |
// gorm.Model | |
ID uuid.UUID `gorm:"primary_key;type:uuid;default:uuid_generate_v4()"` | |
CreatedAt time.Time | |
UpdatedAt time.Time | |
DeletedAt *time.Time `sql:"index"` | |
Name string | |
Description string | |
} | |
func ConfigureQorResource(r resource.Resourcer) { | |
var dummyProduct1 Product | |
dummyProduct1.ID, _ = uuid.Parse("1D50A411-4927-4812-B6D0-215E8620F68B") | |
dummyProduct1.Name = "dummy product 1" | |
dummyProduct1.Description = "the first dummy product" | |
dummyProduct1.CreatedAt = time.Now() | |
dummyProduct1.UpdatedAt = time.Now() | |
var dummyProduct2 Product | |
dummyProduct2.ID, _ = uuid.Parse("0052B26D-CA72-434A-BAEF-8D047A2F9F32") | |
dummyProduct2.Name = "dummy product 2" | |
dummyProduct2.Description = "the second dummy product" | |
dummyProduct2.CreatedAt = time.Now() | |
dummyProduct2.UpdatedAt = time.Now() | |
var dummyProduct3 Product | |
dummyProduct3.ID, _ = uuid.Parse("6400F6FA-56CA-457E-927B-CB18F44B298F") | |
dummyProduct3.Name = "dummy product 3" | |
dummyProduct3.Description = "the third dummy product" | |
dummyProduct3.CreatedAt = time.Now() | |
dummyProduct3.UpdatedAt = time.Now() | |
dummyProducts := make([]Product, 0) | |
dummyProducts = append(dummyProducts, dummyProduct1) | |
dummyProducts = append(dummyProducts, dummyProduct2) | |
dummyProducts = append(dummyProducts, dummyProduct3) | |
p, ok := r.(*admin.Resource) | |
if !ok { | |
panic(fmt.Sprintf("Unexpected resource! T: %T", r)) | |
} | |
// find record and decode it to result | |
p.FindOneHandler = func(result interface{}, metaValues *resource.MetaValues, context *qor.Context) error { | |
if p.HasPermission(roles.Read, context) { | |
var dummyProductTMP Product | |
fmt.Println("result before FindOneHandler: ", result) | |
dummyProductTMP.ID, _ = uuid.Parse(context.ResourceID) | |
for i := 0; i < len(dummyProducts); i++ { | |
if dummyProducts[i].ID == dummyProductTMP.ID { | |
var buf bytes.Buffer | |
json.NewEncoder(&buf).Encode(dummyProducts[i]) | |
json.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(&result) | |
} | |
} | |
fmt.Println("result after FindOneHandler: ", result) | |
return nil | |
} | |
return roles.ErrPermissionDenied | |
} | |
p.FindManyHandler = func(result interface{}, context *qor.Context) error { | |
if p.HasPermission(roles.Read, context) { | |
var buf bytes.Buffer | |
json.NewEncoder(&buf).Encode(dummyProducts) | |
json.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(&result) | |
return nil | |
} | |
return roles.ErrPermissionDenied | |
} | |
p.SaveHandler = func(result interface{}, context *qor.Context) error { | |
if p.HasPermission(roles.Create, context) || p.HasPermission(roles.Update, context) { | |
tmpUUID, _ := uuid.Parse("00000000-0000-0000-0000-000000000000") | |
var dummyProductTMP Product | |
var buf bytes.Buffer | |
json.NewEncoder(&buf).Encode(result) | |
json.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(&dummyProductTMP) | |
if dummyProductTMP.ID == tmpUUID { | |
dummyProductTMP.ID, _ = uuid.NewRandom() | |
// .NewV4() | |
dummyProducts = append(dummyProducts, dummyProductTMP) | |
} else { | |
for i := 0; i < len(dummyProducts); i++ { | |
if dummyProducts[i].ID == dummyProductTMP.ID { | |
var buf bytes.Buffer | |
json.NewEncoder(&buf).Encode(dummyProductTMP) | |
json.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(&dummyProducts[i]) | |
} | |
} | |
} | |
// fmt.Println("result at save: ", result) | |
// fmt.Println("dummyProductTMP at save: ", dummyProductTMP) | |
// fmt.Println("context.ResouceID: ", context.ResourceID) | |
return nil | |
} | |
return roles.ErrPermissionDenied | |
} | |
p.DeleteHandler = func(result interface{}, context *qor.Context) error { | |
if p.HasPermission(roles.Delete, context) { | |
var dummyProductTMP Product | |
fmt.Println("result before DeleteHandler: ", result) | |
dummyProductTMP.ID, _ = uuid.Parse(context.ResourceID) | |
for i := 0; i < len(dummyProducts); i++ { | |
if dummyProducts[i].ID == dummyProductTMP.ID { | |
copy(dummyProducts[i:], dummyProducts[i+1:]) | |
dummyProducts = dummyProducts[:len(dummyProducts)-1] | |
} | |
} | |
return nil | |
} | |
return roles.ErrPermissionDenied | |
} | |
} | |
func ConfigureQorResourceDynamoDB(r resource.Resourcer) { | |
config := &aws.Config{ | |
Region: aws.String("us-west-2"), | |
Endpoint: aws.String("http://localhost:8000"), | |
} | |
db := dynamo.New(session.New(), config) | |
table := db.Table("Products") | |
p, ok := r.(*admin.Resource) | |
if !ok { | |
panic(fmt.Sprintf("Unexpected resource! T: %T", r)) | |
} | |
p.FindOneHandler = func(result interface{}, metaValues *resource.MetaValues, context *qor.Context) error { | |
fmt.Println("FindOneHandler") | |
if p.HasPermission(roles.Read, context) { | |
var dbProductTMP Product | |
fmt.Println("result before FindOneHandler: ", result) | |
dbProductTMP.ID, _ = uuid.Parse(context.ResourceID) | |
err := table.Get("ID", dbProductTMP.ID).One(&dbProductTMP) | |
var buf bytes.Buffer | |
json.NewEncoder(&buf).Encode(dbProductTMP) | |
json.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(&result) | |
return err | |
} | |
return roles.ErrPermissionDenied | |
} | |
p.FindManyHandler = func(result interface{}, context *qor.Context) error { | |
fmt.Println("FindManyHandler") | |
if p.HasPermission(roles.Read, context) { | |
var dbProducts []Product | |
err := table.Scan().All(&dbProducts) | |
// var dbresult []Product | |
// err := table.Scan().All(&dbresult) | |
// fmt.Println("1.dbresult, &dbresult: ", dbresult, &dbresult) | |
var buf bytes.Buffer | |
json.NewEncoder(&buf).Encode(dbProducts) | |
json.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(&result) | |
return err | |
} | |
return roles.ErrPermissionDenied | |
} | |
p.SaveHandler = func(result interface{}, context *qor.Context) error { | |
fmt.Println("SaveHandler") | |
if p.HasPermission(roles.Create, context) || p.HasPermission(roles.Update, context) { | |
tmpUUID, _ := uuid.Parse("00000000-0000-0000-0000-000000000000") | |
var dummyProductTMP Product | |
var buf bytes.Buffer | |
json.NewEncoder(&buf).Encode(result) | |
json.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(&dummyProductTMP) | |
var err error | |
if dummyProductTMP.ID == tmpUUID { | |
dummyProductTMP.ID, _ = uuid.NewRandom() | |
err = table.Put(dummyProductTMP).Run() | |
} else { | |
err = table.Put(dummyProductTMP).Run() | |
} | |
return err | |
} | |
return roles.ErrPermissionDenied | |
} | |
p.DeleteHandler = func(result interface{}, context *qor.Context) error { | |
fmt.Println("DeleteHandler") | |
if p.HasPermission(roles.Delete, context) { | |
var dbProductTMP Product | |
dbProductTMP.ID, _ = uuid.Parse(context.ResourceID) | |
err := table.Delete("ID", dbProductTMP.ID).Run() | |
return err | |
} | |
return roles.ErrPermissionDenied | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment