Spaces:
Running
Running
Anousone Worlakoumman commited on
Commit ·
e2e7b3b
0
Parent(s):
list orders & update order status
Browse files- .gitignore +2 -0
- go.mod +42 -0
- go.sum +125 -0
- main.go +153 -0
- orders.go +82 -0
- provider.go +294 -0
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pb_data/
|
| 2 |
+
api
|
go.mod
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module api
|
| 2 |
+
|
| 3 |
+
go 1.23.5
|
| 4 |
+
|
| 5 |
+
require (
|
| 6 |
+
github.com/pocketbase/dbx v1.11.0
|
| 7 |
+
github.com/pocketbase/pocketbase v0.27.0
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
require (
|
| 11 |
+
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
|
| 12 |
+
github.com/disintegration/imaging v1.6.2 // indirect
|
| 13 |
+
github.com/domodwyer/mailyak/v3 v3.6.2 // indirect
|
| 14 |
+
github.com/dustin/go-humanize v1.0.1 // indirect
|
| 15 |
+
github.com/fatih/color v1.18.0 // indirect
|
| 16 |
+
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
|
| 17 |
+
github.com/ganigeorgiev/fexpr v0.5.0 // indirect
|
| 18 |
+
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 // indirect
|
| 19 |
+
github.com/golang-jwt/jwt/v5 v5.2.2 // indirect
|
| 20 |
+
github.com/google/uuid v1.6.0 // indirect
|
| 21 |
+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
| 22 |
+
github.com/mattn/go-colorable v0.1.14 // indirect
|
| 23 |
+
github.com/mattn/go-isatty v0.0.20 // indirect
|
| 24 |
+
github.com/ncruces/go-strftime v0.1.9 // indirect
|
| 25 |
+
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
| 26 |
+
github.com/spf13/cast v1.7.1 // indirect
|
| 27 |
+
github.com/spf13/cobra v1.9.1 // indirect
|
| 28 |
+
github.com/spf13/pflag v1.0.6 // indirect
|
| 29 |
+
github.com/stretchr/testify v1.10.0 // indirect
|
| 30 |
+
golang.org/x/crypto v0.37.0 // indirect
|
| 31 |
+
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 // indirect
|
| 32 |
+
golang.org/x/image v0.26.0 // indirect
|
| 33 |
+
golang.org/x/net v0.39.0 // indirect
|
| 34 |
+
golang.org/x/oauth2 v0.29.0 // indirect
|
| 35 |
+
golang.org/x/sync v0.13.0 // indirect
|
| 36 |
+
golang.org/x/sys v0.32.0 // indirect
|
| 37 |
+
golang.org/x/text v0.24.0 // indirect
|
| 38 |
+
modernc.org/libc v1.62.1 // indirect
|
| 39 |
+
modernc.org/mathutil v1.7.1 // indirect
|
| 40 |
+
modernc.org/memory v1.9.1 // indirect
|
| 41 |
+
modernc.org/sqlite v1.37.0 // indirect
|
| 42 |
+
)
|
go.sum
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
|
| 2 |
+
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
|
| 3 |
+
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
|
| 4 |
+
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
| 5 |
+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
| 6 |
+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
| 7 |
+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
| 8 |
+
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
|
| 9 |
+
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
|
| 10 |
+
github.com/domodwyer/mailyak/v3 v3.6.2 h1:x3tGMsyFhTCaxp6ycgR0FE/bu5QiNp+hetUuCOBXMn8=
|
| 11 |
+
github.com/domodwyer/mailyak/v3 v3.6.2/go.mod h1:lOm/u9CyCVWHeaAmHIdF4RiKVxKUT/H5XX10lIKAL6c=
|
| 12 |
+
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
| 13 |
+
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
| 14 |
+
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
|
| 15 |
+
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
|
| 16 |
+
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
| 17 |
+
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
| 18 |
+
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
|
| 19 |
+
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
|
| 20 |
+
github.com/ganigeorgiev/fexpr v0.5.0 h1:XA9JxtTE/Xm+g/JFI6RfZEHSiQlk+1glLvRK1Lpv/Tk=
|
| 21 |
+
github.com/ganigeorgiev/fexpr v0.5.0/go.mod h1:RyGiGqmeXhEQ6+mlGdnUleLHgtzzu/VGO2WtJkF5drE=
|
| 22 |
+
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 h1:byhDUpfEwjsVQb1vBunvIjh2BHQ9ead57VkAEY4V+Es=
|
| 23 |
+
github.com/go-ozzo/ozzo-validation/v4 v4.3.0/go.mod h1:2NKgrcHl3z6cJs+3Oo940FPRiTzuqKbvfrL2RxCj6Ew=
|
| 24 |
+
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
|
| 25 |
+
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
|
| 26 |
+
github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeDy8=
|
| 27 |
+
github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
|
| 28 |
+
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
| 29 |
+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
| 30 |
+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
| 31 |
+
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
|
| 32 |
+
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
|
| 33 |
+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
| 34 |
+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
| 35 |
+
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
| 36 |
+
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
| 37 |
+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
| 38 |
+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
| 39 |
+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
| 40 |
+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
| 41 |
+
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
|
| 42 |
+
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
|
| 43 |
+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
| 44 |
+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
| 45 |
+
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
|
| 46 |
+
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
| 47 |
+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
| 48 |
+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
| 49 |
+
github.com/pocketbase/dbx v1.11.0 h1:LpZezioMfT3K4tLrqA55wWFw1EtH1pM4tzSVa7kgszU=
|
| 50 |
+
github.com/pocketbase/dbx v1.11.0/go.mod h1:xXRCIAKTHMgUCyCKZm55pUOdvFziJjQfXaWKhu2vhMs=
|
| 51 |
+
github.com/pocketbase/pocketbase v0.27.0 h1:TWyWBOpPRODEvaJfGFSDS1+m13aJQJqVZqvOo2W3Tdo=
|
| 52 |
+
github.com/pocketbase/pocketbase v0.27.0/go.mod h1:W1iPzrOpUtRhzOwHZu9+awW7LhPPdji5ZMnTL2XFcGM=
|
| 53 |
+
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
| 54 |
+
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
| 55 |
+
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
| 56 |
+
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
| 57 |
+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
| 58 |
+
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
|
| 59 |
+
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
| 60 |
+
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
|
| 61 |
+
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
|
| 62 |
+
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
|
| 63 |
+
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
| 64 |
+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
| 65 |
+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
| 66 |
+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
| 67 |
+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
| 68 |
+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
| 69 |
+
golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE=
|
| 70 |
+
golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc=
|
| 71 |
+
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 h1:R84qjqJb5nVJMxqWYb3np9L5ZsaDtB+a39EqjV0JSUM=
|
| 72 |
+
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0/go.mod h1:S9Xr4PYopiDyqSyp5NjCrhFrqg6A5zA2E/iPHPhqnS8=
|
| 73 |
+
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
| 74 |
+
golang.org/x/image v0.26.0 h1:4XjIFEZWQmCZi6Wv8BoxsDhRU3RVnLX04dToTDAEPlY=
|
| 75 |
+
golang.org/x/image v0.26.0/go.mod h1:lcxbMFAovzpnJxzXS3nyL83K27tmqtKzIJpctK8YO5c=
|
| 76 |
+
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
|
| 77 |
+
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
|
| 78 |
+
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
|
| 79 |
+
golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
|
| 80 |
+
golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
|
| 81 |
+
golang.org/x/oauth2 v0.29.0 h1:WdYw2tdTK1S8olAzWHdgeqfy+Mtm9XNhv/xJsY65d98=
|
| 82 |
+
golang.org/x/oauth2 v0.29.0/go.mod h1:onh5ek6nERTohokkhCD/y2cV4Do3fxFHFuAejCkRWT8=
|
| 83 |
+
golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610=
|
| 84 |
+
golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
| 85 |
+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
| 86 |
+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
| 87 |
+
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
|
| 88 |
+
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
| 89 |
+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
| 90 |
+
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
| 91 |
+
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
|
| 92 |
+
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
|
| 93 |
+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
| 94 |
+
golang.org/x/tools v0.32.0 h1:Q7N1vhpkQv7ybVzLFtTjvQya2ewbwNDZzUgfXGqtMWU=
|
| 95 |
+
golang.org/x/tools v0.32.0/go.mod h1:ZxrU41P/wAbZD8EDa6dDCa6XfpkhJ7HFMjHJXfBDu8s=
|
| 96 |
+
google.golang.org/appengine v1.6.5 h1:tycE03LOZYQNhDpS27tcQdAzLCVMaj7QT2SXxebnpCM=
|
| 97 |
+
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
|
| 98 |
+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
| 99 |
+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
| 100 |
+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
| 101 |
+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
| 102 |
+
modernc.org/cc/v4 v4.25.2 h1:T2oH7sZdGvTaie0BRNFbIYsabzCxUQg8nLqCdQ2i0ic=
|
| 103 |
+
modernc.org/cc/v4 v4.25.2/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0=
|
| 104 |
+
modernc.org/ccgo/v4 v4.25.1 h1:TFSzPrAGmDsdnhT9X2UrcPMI3N/mJ9/X9ykKXwLhDsU=
|
| 105 |
+
modernc.org/ccgo/v4 v4.25.1/go.mod h1:njjuAYiPflywOOrm3B7kCB444ONP5pAVr8PIEoE0uDw=
|
| 106 |
+
modernc.org/fileutil v1.3.0 h1:gQ5SIzK3H9kdfai/5x41oQiKValumqNTDXMvKo62HvE=
|
| 107 |
+
modernc.org/fileutil v1.3.0/go.mod h1:XatxS8fZi3pS8/hKG2GH/ArUogfxjpEKs3Ku3aK4JyQ=
|
| 108 |
+
modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
|
| 109 |
+
modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
|
| 110 |
+
modernc.org/libc v1.62.1 h1:s0+fv5E3FymN8eJVmnk0llBe6rOxCu/DEU+XygRbS8s=
|
| 111 |
+
modernc.org/libc v1.62.1/go.mod h1:iXhATfJQLjG3NWy56a6WVU73lWOcdYVxsvwCgoPljuo=
|
| 112 |
+
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
| 113 |
+
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
| 114 |
+
modernc.org/memory v1.9.1 h1:V/Z1solwAVmMW1yttq3nDdZPJqV1rM05Ccq6KMSZ34g=
|
| 115 |
+
modernc.org/memory v1.9.1/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
|
| 116 |
+
modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8=
|
| 117 |
+
modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
|
| 118 |
+
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
|
| 119 |
+
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
|
| 120 |
+
modernc.org/sqlite v1.37.0 h1:s1TMe7T3Q3ovQiK2Ouz4Jwh7dw4ZDqbebSDTlSJdfjI=
|
| 121 |
+
modernc.org/sqlite v1.37.0/go.mod h1:5YiWv+YviqGMuGw4V+PNplcyaJ5v+vQd7TQOgkACoJM=
|
| 122 |
+
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
|
| 123 |
+
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
|
| 124 |
+
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
| 125 |
+
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
main.go
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package main
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"fmt"
|
| 5 |
+
"log"
|
| 6 |
+
"strings"
|
| 7 |
+
|
| 8 |
+
"github.com/pocketbase/dbx"
|
| 9 |
+
"github.com/pocketbase/pocketbase"
|
| 10 |
+
"github.com/pocketbase/pocketbase/apis"
|
| 11 |
+
"github.com/pocketbase/pocketbase/core"
|
| 12 |
+
"github.com/pocketbase/pocketbase/tools/hook"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
var app *pocketbase.PocketBase
|
| 16 |
+
|
| 17 |
+
func main() {
|
| 18 |
+
app = pocketbase.New()
|
| 19 |
+
|
| 20 |
+
app.OnServe().BindFunc(func(se *core.ServeEvent) error {
|
| 21 |
+
// attach global middleware
|
| 22 |
+
se.Router.BindFunc(func(e *core.RequestEvent) error {
|
| 23 |
+
println(0)
|
| 24 |
+
return e.Next()
|
| 25 |
+
}).Bind(apis.SkipSuccessActivityLog())
|
| 26 |
+
|
| 27 |
+
sub := se.Router.Group("/sub")
|
| 28 |
+
|
| 29 |
+
// attach group middleware
|
| 30 |
+
sub.BindFunc(func(e *core.RequestEvent) error {
|
| 31 |
+
println(1)
|
| 32 |
+
return e.Next()
|
| 33 |
+
})
|
| 34 |
+
|
| 35 |
+
// attach group middleware with an id and custom priority
|
| 36 |
+
sub.Bind(&hook.Handler[*core.RequestEvent]{
|
| 37 |
+
Id: "something",
|
| 38 |
+
Func: func(e *core.RequestEvent) error {
|
| 39 |
+
println(2)
|
| 40 |
+
return e.Next()
|
| 41 |
+
},
|
| 42 |
+
Priority: -1,
|
| 43 |
+
})
|
| 44 |
+
|
| 45 |
+
// attach middleware to a single route
|
| 46 |
+
//
|
| 47 |
+
// "GET /sub/hello" should print the sequence: 2,0,1,3,4
|
| 48 |
+
sub.GET("/hello", func(e *core.RequestEvent) error {
|
| 49 |
+
println(4)
|
| 50 |
+
return e.String(200, "Hello!")
|
| 51 |
+
}).BindFunc(func(e *core.RequestEvent) error {
|
| 52 |
+
println(3)
|
| 53 |
+
return e.Next()
|
| 54 |
+
}).Unbind("something")
|
| 55 |
+
|
| 56 |
+
se.Router.POST("/auth/phone-login", func(e *core.RequestEvent) error {
|
| 57 |
+
data := struct {
|
| 58 |
+
Phone string `json:"phone" form:"phone"`
|
| 59 |
+
Password string `json:"password" form:"password"`
|
| 60 |
+
}{}
|
| 61 |
+
if err := e.BindBody(&data); err != nil {
|
| 62 |
+
return e.BadRequestError("Failed to read request data", err)
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
record, err := e.App.FindFirstRecordByData("users", "phone_number", data.Phone)
|
| 66 |
+
if err != nil || !record.ValidatePassword(data.Password) {
|
| 67 |
+
return e.BadRequestError("Invalid credentials", err)
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
return apis.RecordAuthResponse(e, record, "phone", nil)
|
| 71 |
+
})
|
| 72 |
+
|
| 73 |
+
se.Router.PUT("/order-status", func(e *core.RequestEvent) error {
|
| 74 |
+
data := struct {
|
| 75 |
+
OrderID string `json:"orderId"`
|
| 76 |
+
Status OrderStatus `json:"status"`
|
| 77 |
+
}{}
|
| 78 |
+
|
| 79 |
+
if err := e.BindBody(&data); err != nil {
|
| 80 |
+
return e.BadRequestError("Failed to read request data", err)
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
_, err := app.DB().
|
| 84 |
+
Update(
|
| 85 |
+
"orders",
|
| 86 |
+
dbx.Params{"status": data.Status},
|
| 87 |
+
dbx.NewExp("LOWER([[id]])={:id}", dbx.Params{"id": strings.ToLower(data.OrderID)}),
|
| 88 |
+
).
|
| 89 |
+
Execute()
|
| 90 |
+
if err != nil {
|
| 91 |
+
return apis.NewInternalServerError("Failed to retrieve order data", err) // More specific error message
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
return e.JSON(200, map[string]string{"status": "success"})
|
| 95 |
+
})
|
| 96 |
+
|
| 97 |
+
se.Router.GET("/order-list", ListOrders)
|
| 98 |
+
|
| 99 |
+
return se.Next()
|
| 100 |
+
})
|
| 101 |
+
|
| 102 |
+
app.OnSettingsReload().BindFunc(func(e *core.SettingsReloadEvent) error {
|
| 103 |
+
println("on setting reload")
|
| 104 |
+
if err := e.Next(); err != nil {
|
| 105 |
+
return err
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
e.App.Settings()
|
| 109 |
+
|
| 110 |
+
return nil
|
| 111 |
+
})
|
| 112 |
+
|
| 113 |
+
app.OnTerminate().BindFunc(func(e *core.TerminateEvent) error {
|
| 114 |
+
println("app is terminated")
|
| 115 |
+
return e.Next()
|
| 116 |
+
})
|
| 117 |
+
|
| 118 |
+
app.OnMailerSend().BindFunc(func(e *core.MailerEvent) error {
|
| 119 |
+
e.Message.Subject = "new subject"
|
| 120 |
+
return e.Next()
|
| 121 |
+
})
|
| 122 |
+
|
| 123 |
+
app.OnMailerRecordOTPSend().BindFunc(func(e *core.MailerRecordEvent) error {
|
| 124 |
+
e.Message.Subject = "new subject"
|
| 125 |
+
return e.Next()
|
| 126 |
+
})
|
| 127 |
+
|
| 128 |
+
// use for sse
|
| 129 |
+
app.OnRealtimeConnectRequest().BindFunc(func(e *core.RealtimeConnectRequestEvent) error {
|
| 130 |
+
println("sse connecting")
|
| 131 |
+
fmt.Printf("app: %v\n", e.App)
|
| 132 |
+
fmt.Printf("client: %v\n", e.Client)
|
| 133 |
+
fmt.Printf("timeout: %v\n", e.IdleTimeout)
|
| 134 |
+
return e.Next()
|
| 135 |
+
})
|
| 136 |
+
|
| 137 |
+
app.OnRealtimeMessageSend().BindFunc(func(e *core.RealtimeMessageEvent) error {
|
| 138 |
+
println("send sse message")
|
| 139 |
+
fmt.Printf("app: %v\n", e.App)
|
| 140 |
+
fmt.Printf("client: %v\n", e.Client)
|
| 141 |
+
fmt.Printf("message: %v\n", e.Message)
|
| 142 |
+
return e.Next()
|
| 143 |
+
})
|
| 144 |
+
|
| 145 |
+
app.OnCollectionsListRequest().BindFunc(func(e *core.CollectionsListRequestEvent) error {
|
| 146 |
+
fmt.Printf("list collection....")
|
| 147 |
+
return e.Next()
|
| 148 |
+
})
|
| 149 |
+
|
| 150 |
+
if err := app.Start(); err != nil {
|
| 151 |
+
log.Fatal(err)
|
| 152 |
+
}
|
| 153 |
+
}
|
orders.go
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package main
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"fmt"
|
| 5 |
+
"net/http"
|
| 6 |
+
|
| 7 |
+
"github.com/pocketbase/dbx"
|
| 8 |
+
"github.com/pocketbase/pocketbase/core"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
type OrderStatus string
|
| 12 |
+
|
| 13 |
+
var (
|
| 14 |
+
OrderStatusPending OrderStatus = "pending"
|
| 15 |
+
OrderStatusPurchased OrderStatus = "purchased"
|
| 16 |
+
OrderStatusCancel OrderStatus = "cancel"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
func (s *OrderStatus) UnmarshalText(text []byte) error {
|
| 20 |
+
switch status := string(text); status {
|
| 21 |
+
case string(OrderStatusPending), string(OrderStatusPurchased), string(OrderStatusCancel):
|
| 22 |
+
*s = OrderStatus(status)
|
| 23 |
+
return nil
|
| 24 |
+
default:
|
| 25 |
+
return fmt.Errorf("invalid order status: %s", status)
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
type Order struct {
|
| 30 |
+
Id string `db:"id" json:"id"`
|
| 31 |
+
ReferenceID string `db:"reference_id" json:"referenceID"`
|
| 32 |
+
CustomerID string `db:"customer_id" json:"customerID"`
|
| 33 |
+
CustomerName string `db:"customer_name" json:"customerName"`
|
| 34 |
+
PhoneNumber string `db:"phone_number" json:"phoneNumber"`
|
| 35 |
+
Address string `db:"address" json:"address"`
|
| 36 |
+
Status string `db:"status" json:"status"`
|
| 37 |
+
Qty *int `db:"quantity" json:"quantity"`
|
| 38 |
+
PriceLAK *string `db:"price_lak" json:"amountLAK"`
|
| 39 |
+
PriceTHB *string `db:"price_thb" json:"amountTHB"`
|
| 40 |
+
PriceUSD *string `db:"price_usd" json:"amountUSD"`
|
| 41 |
+
Created string `db:"created" json:"created"`
|
| 42 |
+
Updated string `db:"updated" json:"updated"`
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
func ListOrders(e *core.RequestEvent) error {
|
| 46 |
+
query := app.DB().
|
| 47 |
+
Select(
|
| 48 |
+
"o.id AS id",
|
| 49 |
+
"o.reference_id",
|
| 50 |
+
"o.customer_id",
|
| 51 |
+
"o.customer_name",
|
| 52 |
+
"o.phone_number",
|
| 53 |
+
"o.address",
|
| 54 |
+
"o.status",
|
| 55 |
+
"o.created",
|
| 56 |
+
"o.updated",
|
| 57 |
+
"SUM(item.quantity) AS quantity",
|
| 58 |
+
"SUM(item.price_lak) AS price_lak",
|
| 59 |
+
"SUM(item.price_thb) AS price_thb",
|
| 60 |
+
"SUM(item.price_usd) AS price_usd",
|
| 61 |
+
).
|
| 62 |
+
From("orders o").
|
| 63 |
+
LeftJoin("order_items item", dbx.NewExp("item.order_id = o.id")).
|
| 64 |
+
GroupBy("o.id")
|
| 65 |
+
|
| 66 |
+
subquery := app.DB().
|
| 67 |
+
Select(
|
| 68 |
+
"count(*)",
|
| 69 |
+
).
|
| 70 |
+
From("orders")
|
| 71 |
+
|
| 72 |
+
searchProvider := NewProvider().Query(query).SubQuery(subquery)
|
| 73 |
+
|
| 74 |
+
records := make([]Order, 0)
|
| 75 |
+
|
| 76 |
+
result, err := searchProvider.ParseAndExec(e.Request.URL.Query().Encode(), records)
|
| 77 |
+
if err != nil {
|
| 78 |
+
return fmt.Errorf("ParseAndExec failed: %w", err)
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
return e.JSON(http.StatusOK, result)
|
| 82 |
+
}
|
provider.go
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package main
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"errors"
|
| 5 |
+
"math"
|
| 6 |
+
"net/url"
|
| 7 |
+
"strconv"
|
| 8 |
+
|
| 9 |
+
"github.com/pocketbase/dbx"
|
| 10 |
+
"golang.org/x/sync/errgroup"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
const (
|
| 14 |
+
// DefaultPerPage specifies the default number of returned search result items.
|
| 15 |
+
DefaultPerPage int = 30
|
| 16 |
+
|
| 17 |
+
// DefaultFilterExprLimit specifies the default filter expressions limit.
|
| 18 |
+
DefaultFilterExprLimit int = 200
|
| 19 |
+
|
| 20 |
+
// DefaultSortExprLimit specifies the default sort expressions limit.
|
| 21 |
+
DefaultSortExprLimit int = 8
|
| 22 |
+
|
| 23 |
+
// MaxPerPage specifies the max allowed search result items returned in a single page.
|
| 24 |
+
MaxPerPage int = 1000
|
| 25 |
+
|
| 26 |
+
// MaxFilterLength specifies the max allowed individual search filter parsable length.
|
| 27 |
+
MaxFilterLength int = 3500
|
| 28 |
+
|
| 29 |
+
// MaxSortFieldLength specifies the max allowed individual sort field parsable length.
|
| 30 |
+
MaxSortFieldLength int = 255
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
// Common search errors.
|
| 34 |
+
var (
|
| 35 |
+
ErrEmptyQuery = errors.New("search query is not set")
|
| 36 |
+
ErrSortExprLimit = errors.New("max sort expressions limit reached")
|
| 37 |
+
ErrFilterExprLimit = errors.New("max filter expressions limit reached")
|
| 38 |
+
ErrFilterLengthLimit = errors.New("max filter length limit reached")
|
| 39 |
+
ErrSortFieldLengthLimit = errors.New("max sort field length limit reached")
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
// URL search query params
|
| 43 |
+
const (
|
| 44 |
+
PageQueryParam string = "page"
|
| 45 |
+
PerPageQueryParam string = "perPage"
|
| 46 |
+
SortQueryParam string = "sort"
|
| 47 |
+
FilterQueryParam string = "filter"
|
| 48 |
+
SkipTotalQueryParam string = "skipTotal"
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
// Result defines the returned search result structure.
|
| 52 |
+
type Result struct {
|
| 53 |
+
Items any `json:"items"`
|
| 54 |
+
Page int `json:"page"`
|
| 55 |
+
PerPage int `json:"perPage"`
|
| 56 |
+
TotalItems int `json:"totalItems"`
|
| 57 |
+
TotalPages int `json:"totalPages"`
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
// Provider represents a single configured search provider instance.
|
| 61 |
+
type Provider struct {
|
| 62 |
+
query *dbx.SelectQuery
|
| 63 |
+
subquery *dbx.SelectQuery
|
| 64 |
+
countCol string
|
| 65 |
+
page int
|
| 66 |
+
perPage int
|
| 67 |
+
skipTotal bool
|
| 68 |
+
maxFilterExprLimit int
|
| 69 |
+
maxSortExprLimit int
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
// NewProvider initializes and returns a new search provider.
|
| 73 |
+
//
|
| 74 |
+
// Example:
|
| 75 |
+
//
|
| 76 |
+
// baseQuery := db.Select("*").From("user")
|
| 77 |
+
// fieldResolver := search.NewSimpleFieldResolver("id", "name")
|
| 78 |
+
// models := []*YourDataStruct{}
|
| 79 |
+
//
|
| 80 |
+
// result, err := search.NewProvider(fieldResolver).
|
| 81 |
+
// Query(baseQuery).
|
| 82 |
+
// ParseAndExec("page=2&filter=id>0&sort=-email", &models)
|
| 83 |
+
func NewProvider() *Provider {
|
| 84 |
+
return &Provider{
|
| 85 |
+
countCol: "id",
|
| 86 |
+
page: 1,
|
| 87 |
+
perPage: DefaultPerPage,
|
| 88 |
+
maxFilterExprLimit: DefaultFilterExprLimit,
|
| 89 |
+
maxSortExprLimit: DefaultSortExprLimit,
|
| 90 |
+
}
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
// MaxFilterExprLimit changes the default max allowed filter expressions.
|
| 94 |
+
//
|
| 95 |
+
// Note that currently the limit is applied individually for each separate filter.
|
| 96 |
+
func (s *Provider) MaxFilterExprLimit(max int) *Provider {
|
| 97 |
+
s.maxFilterExprLimit = max
|
| 98 |
+
return s
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
// MaxSortExprLimit changes the default max allowed sort expressions.
|
| 102 |
+
func (s *Provider) MaxSortExprLimit(max int) *Provider {
|
| 103 |
+
s.maxSortExprLimit = max
|
| 104 |
+
return s
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
// Query sets the base query that will be used to fetch the search items.
|
| 108 |
+
func (s *Provider) Query(query *dbx.SelectQuery) *Provider {
|
| 109 |
+
s.query = query
|
| 110 |
+
return s
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
func (s *Provider) SubQuery(query *dbx.SelectQuery) *Provider {
|
| 114 |
+
s.subquery = query
|
| 115 |
+
return s
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
// SkipTotal changes the `skipTotal` field of the current search provider.
|
| 119 |
+
func (s *Provider) SkipTotal(skipTotal bool) *Provider {
|
| 120 |
+
s.skipTotal = skipTotal
|
| 121 |
+
return s
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
// CountCol allows changing the default column (id) that is used
|
| 125 |
+
// to generate the COUNT SQL query statement.
|
| 126 |
+
//
|
| 127 |
+
// This field is ignored if skipTotal is true.
|
| 128 |
+
func (s *Provider) CountCol(name string) *Provider {
|
| 129 |
+
s.countCol = name
|
| 130 |
+
return s
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
// Page sets the `page` field of the current search provider.
|
| 134 |
+
//
|
| 135 |
+
// Normalization on the `page` value is done during `Exec()`.
|
| 136 |
+
func (s *Provider) Page(page int) *Provider {
|
| 137 |
+
s.page = page
|
| 138 |
+
return s
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
// PerPage sets the `perPage` field of the current search provider.
|
| 142 |
+
//
|
| 143 |
+
// Normalization on the `perPage` value is done during `Exec()`.
|
| 144 |
+
func (s *Provider) PerPage(perPage int) *Provider {
|
| 145 |
+
s.perPage = perPage
|
| 146 |
+
return s
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
// Sort sets the `sort` field of the current search provider.
|
| 150 |
+
// func (s *Provider) Sort(sort []SortField) *Provider {
|
| 151 |
+
// s.sort = sort
|
| 152 |
+
// return s
|
| 153 |
+
// }
|
| 154 |
+
//
|
| 155 |
+
// // AddSort appends the provided SortField to the existing provider's sort field.
|
| 156 |
+
// func (s *Provider) AddSort(field SortField) *Provider {
|
| 157 |
+
// s.sort = append(s.sort, field)
|
| 158 |
+
// return s
|
| 159 |
+
// }
|
| 160 |
+
//
|
| 161 |
+
// // Filter sets the `filter` field of the current search provider.
|
| 162 |
+
// func (s *Provider) Filter(filter []FilterData) *Provider {
|
| 163 |
+
// s.filter = filter
|
| 164 |
+
// return s
|
| 165 |
+
// }
|
| 166 |
+
//
|
| 167 |
+
// // AddFilter appends the provided FilterData to the existing provider's filter field.
|
| 168 |
+
// func (s *Provider) AddFilter(filter FilterData) *Provider {
|
| 169 |
+
// if filter != "" {
|
| 170 |
+
// s.filter = append(s.filter, filter)
|
| 171 |
+
// }
|
| 172 |
+
// return s
|
| 173 |
+
// }
|
| 174 |
+
|
| 175 |
+
// Parse parses the search query parameter from the provided query string
|
| 176 |
+
// and assigns the found fields to the current search provider.
|
| 177 |
+
//
|
| 178 |
+
// The data from the "sort" and "filter" query parameters are appended
|
| 179 |
+
// to the existing provider's `sort` and `filter` fields
|
| 180 |
+
// (aka. using `AddSort` and `AddFilter`).
|
| 181 |
+
func (s *Provider) Parse(urlQuery string) error {
|
| 182 |
+
params, err := url.ParseQuery(urlQuery)
|
| 183 |
+
if err != nil {
|
| 184 |
+
return err
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
if raw := params.Get(SkipTotalQueryParam); raw != "" {
|
| 188 |
+
v, err := strconv.ParseBool(raw)
|
| 189 |
+
if err != nil {
|
| 190 |
+
return err
|
| 191 |
+
}
|
| 192 |
+
s.SkipTotal(v)
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
if raw := params.Get(PageQueryParam); raw != "" {
|
| 196 |
+
v, err := strconv.Atoi(raw)
|
| 197 |
+
if err != nil {
|
| 198 |
+
return err
|
| 199 |
+
}
|
| 200 |
+
s.Page(v)
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
if raw := params.Get(PerPageQueryParam); raw != "" {
|
| 204 |
+
v, err := strconv.Atoi(raw)
|
| 205 |
+
if err != nil {
|
| 206 |
+
return err
|
| 207 |
+
}
|
| 208 |
+
s.PerPage(v)
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
// if raw := params.Get(SortQueryParam); raw != "" {
|
| 212 |
+
// for _, sortField := range ParseSortFromString(raw) {
|
| 213 |
+
// s.AddSort(sortField)
|
| 214 |
+
// }
|
| 215 |
+
// }
|
| 216 |
+
//
|
| 217 |
+
// if raw := params.Get(FilterQueryParam); raw != "" {
|
| 218 |
+
// s.AddFilter(FilterData(raw))
|
| 219 |
+
// }
|
| 220 |
+
|
| 221 |
+
return nil
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
// Exec executes the search provider and fills/scans
|
| 225 |
+
// the provided `items` slice with the found models.
|
| 226 |
+
func (s *Provider) Exec(items []Order) (*Result, error) {
|
| 227 |
+
if s.query == nil || s.subquery == nil {
|
| 228 |
+
return nil, ErrEmptyQuery
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
modelsQuery := *s.query
|
| 232 |
+
|
| 233 |
+
// normalize page
|
| 234 |
+
if s.page <= 0 {
|
| 235 |
+
s.page = 1
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
// normalize perPage
|
| 239 |
+
if s.perPage <= 0 {
|
| 240 |
+
s.perPage = DefaultPerPage
|
| 241 |
+
} else if s.perPage > MaxPerPage {
|
| 242 |
+
s.perPage = MaxPerPage
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
// negative value to differentiate from the zero default
|
| 246 |
+
totalCount := -1
|
| 247 |
+
totalPages := -1
|
| 248 |
+
|
| 249 |
+
countQuery := *s.subquery
|
| 250 |
+
countExec := func() error {
|
| 251 |
+
err := countQuery.Row(&totalCount)
|
| 252 |
+
if err != nil {
|
| 253 |
+
return err
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
totalPages = int(math.Ceil(float64(totalCount) / float64(s.perPage)))
|
| 257 |
+
|
| 258 |
+
return nil
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
// apply pagination to the original query and fetch the models
|
| 262 |
+
modelsExec := func() error {
|
| 263 |
+
modelsQuery.Limit(int64(s.perPage))
|
| 264 |
+
modelsQuery.Offset(int64(s.perPage * (s.page - 1)))
|
| 265 |
+
|
| 266 |
+
return modelsQuery.All(&items)
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
errg := new(errgroup.Group)
|
| 270 |
+
errg.SetLimit(2)
|
| 271 |
+
errg.Go(countExec)
|
| 272 |
+
errg.Go(modelsExec)
|
| 273 |
+
if err := errg.Wait(); err != nil {
|
| 274 |
+
return nil, err
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
result := &Result{
|
| 278 |
+
Page: s.page,
|
| 279 |
+
PerPage: s.perPage,
|
| 280 |
+
TotalItems: totalCount,
|
| 281 |
+
TotalPages: totalPages,
|
| 282 |
+
Items: items,
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
return result, nil
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
func (s *Provider) ParseAndExec(urlQuery string, records []Order) (*Result, error) {
|
| 289 |
+
if err := s.Parse(urlQuery); err != nil {
|
| 290 |
+
return nil, err
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
return s.Exec(records)
|
| 294 |
+
}
|