activity.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package e00_basics
  2. import (
  3. "context"
  4. "github.com/qor5/admin/activity"
  5. "github.com/qor5/admin/presets"
  6. "gorm.io/driver/sqlite"
  7. "gorm.io/gorm"
  8. )
  9. func NewActivitySample() {
  10. // @snippet_begin(NewActivitySample)
  11. presetsBuilder := presets.New()
  12. db, err := gorm.Open(sqlite.Open("/tmp/activity.db"), &gorm.Config{})
  13. if err != nil {
  14. panic(err)
  15. }
  16. activityBuilder := activity.New(presetsBuilder, db)
  17. // @snippet_end
  18. // @snippet_begin(ActivityRegisterPresetsModelsSample)
  19. type Product struct {
  20. Title string
  21. Code string
  22. Price float64
  23. }
  24. productModel := presetsBuilder.Model(&Product{})
  25. activityBuilder.RegisterModel(productModel).UseDefaultTab().AddKeys("Title").AddIgnoredFields("Code").SkipDelete()
  26. // @snippet_end
  27. // @snippet_begin(ActivityRecordLogSample)
  28. currentCtx := context.WithValue(context.Background(), activity.CreatorContextKey, "user1")
  29. activityBuilder.AddRecords("Publish", currentCtx, &Product{Title: "Product 1", Code: "P1", Price: 100})
  30. activityBuilder.AddRecords("Update Price", currentCtx, &Product{Title: "Product 1", Code: "P1", Price: 200})
  31. // @snippet_end
  32. }