mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
upgraded migrations and context usage
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"wallet-api/pkg/api"
|
"wallet-api/pkg/api"
|
||||||
"wallet-api/pkg/middleware"
|
"wallet-api/pkg/middleware"
|
||||||
@@ -14,11 +15,12 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
godotenv.Load()
|
godotenv.Load()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
dbUrl := os.Getenv("DATABASE_URL")
|
dbUrl := os.Getenv("DATABASE_URL")
|
||||||
r := gin.New()
|
r := gin.New()
|
||||||
r.Use(middleware.CORSMiddleware())
|
r.Use(middleware.CORSMiddleware())
|
||||||
|
|
||||||
conn := db.CreateConnection(dbUrl)
|
conn := db.CreateConnection(dbUrl, ctx)
|
||||||
api.Init(r, conn)
|
api.Init(r, conn)
|
||||||
|
|
||||||
server.Start(r)
|
server.Start(r)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"wallet-api/pkg/migrate"
|
"wallet-api/pkg/migrate"
|
||||||
"wallet-api/pkg/utl/db"
|
"wallet-api/pkg/utl/db"
|
||||||
@@ -12,7 +13,8 @@ func main() {
|
|||||||
godotenv.Load()
|
godotenv.Load()
|
||||||
|
|
||||||
dbUrl := os.Getenv("DATABASE_URL")
|
dbUrl := os.Getenv("DATABASE_URL")
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
conn := db.CreateConnection(dbUrl)
|
conn := db.CreateConnection(dbUrl, ctx)
|
||||||
migrate.Start(conn)
|
migrate.Start(conn, "")
|
||||||
}
|
}
|
||||||
|
|||||||
1
go.mod
1
go.mod
@@ -5,6 +5,7 @@ go 1.15
|
|||||||
require (
|
require (
|
||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||||
github.com/gin-gonic/gin v1.7.1
|
github.com/gin-gonic/gin v1.7.1
|
||||||
|
github.com/go-pg/migrations/v8 v8.1.0
|
||||||
github.com/go-pg/pg/v10 v10.9.1
|
github.com/go-pg/pg/v10 v10.9.1
|
||||||
github.com/go-playground/validator/v10 v10.5.0 // indirect
|
github.com/go-playground/validator/v10 v10.5.0 // indirect
|
||||||
github.com/golang/protobuf v1.5.2 // indirect
|
github.com/golang/protobuf v1.5.2 // indirect
|
||||||
|
|||||||
20
go.sum
20
go.sum
@@ -15,7 +15,10 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE
|
|||||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||||
github.com/gin-gonic/gin v1.7.1 h1:qC89GU3p8TvKWMAVhEpmpB2CIb1hnqt2UdKZaP93mS8=
|
github.com/gin-gonic/gin v1.7.1 h1:qC89GU3p8TvKWMAVhEpmpB2CIb1hnqt2UdKZaP93mS8=
|
||||||
github.com/gin-gonic/gin v1.7.1/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
|
github.com/gin-gonic/gin v1.7.1/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
|
||||||
|
github.com/go-pg/migrations/v8 v8.1.0 h1:bc1wQwFoWRKvLdluXCRFRkeaw9xDU4qJ63uCAagh66w=
|
||||||
|
github.com/go-pg/migrations/v8 v8.1.0/go.mod h1:o+CN1u572XHphEHZyK6tqyg2GDkRvL2bIoLNyGIewus=
|
||||||
github.com/go-pg/pg v8.0.7+incompatible h1:ty/sXL1OZLo+47KK9N8llRcmbA9tZasqbQ/OO4ld53g=
|
github.com/go-pg/pg v8.0.7+incompatible h1:ty/sXL1OZLo+47KK9N8llRcmbA9tZasqbQ/OO4ld53g=
|
||||||
|
github.com/go-pg/pg/v10 v10.4.0/go.mod h1:BfgPoQnD2wXNd986RYEHzikqv9iE875PrFaZ9vXvtNM=
|
||||||
github.com/go-pg/pg/v10 v10.9.1 h1:kU4t84zWGGaU0Qsu49FbNtToUVrlSTkNOngW8aQmwvk=
|
github.com/go-pg/pg/v10 v10.9.1 h1:kU4t84zWGGaU0Qsu49FbNtToUVrlSTkNOngW8aQmwvk=
|
||||||
github.com/go-pg/pg/v10 v10.9.1/go.mod h1:rgmTPgHgl5EN2CNKKoMwC7QT62t8BqsdpEkUQuiZMQs=
|
github.com/go-pg/pg/v10 v10.9.1/go.mod h1:rgmTPgHgl5EN2CNKKoMwC7QT62t8BqsdpEkUQuiZMQs=
|
||||||
github.com/go-pg/zerochecker v0.2.0 h1:pp7f72c3DobMWOb2ErtZsnrPaSvHd2W4o9//8HtF4mU=
|
github.com/go-pg/zerochecker v0.2.0 h1:pp7f72c3DobMWOb2ErtZsnrPaSvHd2W4o9//8HtF4mU=
|
||||||
@@ -32,9 +35,11 @@ github.com/go-playground/validator/v10 v10.5.0/go.mod h1:xm76BBt941f7yWdGnI2DVPF
|
|||||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
|
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
|
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
|
||||||
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
||||||
|
github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
||||||
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
|
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
|
||||||
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
|
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
|
||||||
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
|
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
|
||||||
@@ -51,6 +56,7 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
|
|||||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
|
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
|
||||||
@@ -109,12 +115,16 @@ github.com/ugorji/go/codec v1.2.5 h1:8WobZKAk18Msm2CothY2jnztY56YVY8kF1oQrj21iis
|
|||||||
github.com/ugorji/go/codec v1.2.5/go.mod h1:QPxoTbPKSEAlAHPYt02++xp/en9B/wUdwFCz+hj5caA=
|
github.com/ugorji/go/codec v1.2.5/go.mod h1:QPxoTbPKSEAlAHPYt02++xp/en9B/wUdwFCz+hj5caA=
|
||||||
github.com/vmihailenco/bufpool v0.1.11 h1:gOq2WmBrq0i2yW5QJ16ykccQ4wH9UyEsgLm6czKAd94=
|
github.com/vmihailenco/bufpool v0.1.11 h1:gOq2WmBrq0i2yW5QJ16ykccQ4wH9UyEsgLm6czKAd94=
|
||||||
github.com/vmihailenco/bufpool v0.1.11/go.mod h1:AFf/MOy3l2CFTKbxwt0mp2MwnqjNEs5H/UxrkA5jxTQ=
|
github.com/vmihailenco/bufpool v0.1.11/go.mod h1:AFf/MOy3l2CFTKbxwt0mp2MwnqjNEs5H/UxrkA5jxTQ=
|
||||||
|
github.com/vmihailenco/msgpack/v4 v4.3.11/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
|
||||||
|
github.com/vmihailenco/msgpack/v5 v5.0.0-beta.1/go.mod h1:xlngVLeyQ/Qi05oQxhQ+oTuqa03RjMwMfk/7/TCs+QI=
|
||||||
github.com/vmihailenco/msgpack/v5 v5.3.0 h1:8G3at/kelmBKeHY6d6cKnGsYO3BLn+uubitdOtOhyNI=
|
github.com/vmihailenco/msgpack/v5 v5.3.0 h1:8G3at/kelmBKeHY6d6cKnGsYO3BLn+uubitdOtOhyNI=
|
||||||
github.com/vmihailenco/msgpack/v5 v5.3.0/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
|
github.com/vmihailenco/msgpack/v5 v5.3.0/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
|
||||||
|
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
|
||||||
github.com/vmihailenco/tagparser v0.1.2 h1:gnjoVuB/kljJ5wICEEOpx98oXMWPLj22G67Vbd1qPqc=
|
github.com/vmihailenco/tagparser v0.1.2 h1:gnjoVuB/kljJ5wICEEOpx98oXMWPLj22G67Vbd1qPqc=
|
||||||
github.com/vmihailenco/tagparser v0.1.2/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
|
github.com/vmihailenco/tagparser v0.1.2/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
|
||||||
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
|
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
|
||||||
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
|
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
|
||||||
|
go.opentelemetry.io/otel v0.13.0/go.mod h1:dlSNewoRYikTkotEnxdmuBHgzT+k/idJSfDv/FxEnOY=
|
||||||
go.opentelemetry.io/otel v0.19.0 h1:Lenfy7QHRXPZVsw/12CWpxX6d/JkrX8wrx2vO8G80Ng=
|
go.opentelemetry.io/otel v0.19.0 h1:Lenfy7QHRXPZVsw/12CWpxX6d/JkrX8wrx2vO8G80Ng=
|
||||||
go.opentelemetry.io/otel v0.19.0/go.mod h1:j9bF567N9EfomkSidSfmMwIwIBuP37AMAIzVW85OxSg=
|
go.opentelemetry.io/otel v0.19.0/go.mod h1:j9bF567N9EfomkSidSfmMwIwIBuP37AMAIzVW85OxSg=
|
||||||
go.opentelemetry.io/otel/metric v0.19.0 h1:dtZ1Ju44gkJkYvo+3qGqVXmf88tc+a42edOywypengg=
|
go.opentelemetry.io/otel/metric v0.19.0 h1:dtZ1Ju44gkJkYvo+3qGqVXmf88tc+a42edOywypengg=
|
||||||
@@ -126,6 +136,8 @@ golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b/go.mod h1:6SG95UA2DQfeDnf
|
|||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
|
||||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
|
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
|
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
|
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
|
||||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||||
@@ -141,8 +153,12 @@ golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73r
|
|||||||
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
|
||||||
|
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||||
golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||||
|
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||||
|
golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
@@ -160,6 +176,8 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20201017003518-b09fb700fbb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210426230700-d19ff857e887 h1:dXfMednGJh/SUUFjTLsWJz3P+TQt9qnR11GgeI3vWKs=
|
golang.org/x/sys v0.0.0-20210426230700-d19ff857e887 h1:dXfMednGJh/SUUFjTLsWJz3P+TQt9qnR11GgeI3vWKs=
|
||||||
@@ -179,6 +197,8 @@ golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBn
|
|||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||||
|
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
|
||||||
|
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
|
||||||
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||||
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
|
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
|
||||||
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
|
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
|
||||||
|
|||||||
@@ -22,12 +22,14 @@ func NewApiController(as *services.ApiService, s *gin.RouterGroup) *ApiControlle
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ac *ApiController) getFirst(c *gin.Context) {
|
func (ac *ApiController) getFirst(c *gin.Context) {
|
||||||
apiModel := ac.ApiService.GetFirst()
|
apiModel := ac.ApiService.GetFirst(c)
|
||||||
c.JSON(200, apiModel)
|
c.JSON(200, apiModel)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ac *ApiController) postMigrate(c *gin.Context) {
|
func (ac *ApiController) postMigrate(c *gin.Context) {
|
||||||
mr, er := ac.ApiService.PostMigrate()
|
migrateModel := c.MustGet("migrate")
|
||||||
|
version := migrateModel.(middleware.SecretCodeModel).Version
|
||||||
|
mr, er := ac.ApiService.PostMigrate(c, version)
|
||||||
|
|
||||||
if er.Message != "" {
|
if er.Message != "" {
|
||||||
c.JSON(er.StatusCode, er)
|
c.JSON(er.StatusCode, er)
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ func (rc *AuthController) PostLogin(c *gin.Context) {
|
|||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
returnedUser, exceptionReturn := rc.UsersService.Login(body)
|
returnedUser, exceptionReturn := rc.UsersService.Login(c, body)
|
||||||
|
|
||||||
if exceptionReturn.Message != "" {
|
if exceptionReturn.Message != "" {
|
||||||
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
|
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
|
||||||
@@ -48,7 +48,7 @@ func (rc *AuthController) PostRegister(c *gin.Context) {
|
|||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
returnedUser, exceptionReturn := rc.UsersService.Create(body)
|
returnedUser, exceptionReturn := rc.UsersService.Create(c, body)
|
||||||
|
|
||||||
if exceptionReturn.Message != "" {
|
if exceptionReturn.Message != "" {
|
||||||
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
|
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
|
||||||
@@ -61,7 +61,7 @@ func (rc *AuthController) Delete(c *gin.Context) {
|
|||||||
authGet := c.MustGet("auth")
|
authGet := c.MustGet("auth")
|
||||||
auth.Id = authGet.(*models.Auth).Id
|
auth.Id = authGet.(*models.Auth).Id
|
||||||
|
|
||||||
mr, er := rc.UsersService.Deactivate(auth)
|
mr, er := rc.UsersService.Deactivate(c, auth)
|
||||||
|
|
||||||
if er.Message != "" {
|
if er.Message != "" {
|
||||||
c.JSON(er.StatusCode, er)
|
c.JSON(er.StatusCode, er)
|
||||||
|
|||||||
@@ -29,14 +29,14 @@ func (wc *SubscriptionTypeController) New(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
wm := wc.SubscriptionTypeService.New(body)
|
wm := wc.SubscriptionTypeService.New(c, body)
|
||||||
c.JSON(200, wm)
|
c.JSON(200, wm)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wc *SubscriptionTypeController) GetAll(c *gin.Context) {
|
func (wc *SubscriptionTypeController) GetAll(c *gin.Context) {
|
||||||
embed, _ := c.GetQuery("embed")
|
embed, _ := c.GetQuery("embed")
|
||||||
|
|
||||||
wm := wc.SubscriptionTypeService.GetAll(embed)
|
wm := wc.SubscriptionTypeService.GetAll(c, embed)
|
||||||
|
|
||||||
c.JSON(200, wm)
|
c.JSON(200, wm)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ func (wc *SubscriptionController) New(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
wm := wc.SubscriptionService.New(body)
|
wm := wc.SubscriptionService.New(c, body)
|
||||||
c.JSON(200, wm)
|
c.JSON(200, wm)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,7 +41,7 @@ func (wc *SubscriptionController) GetAll(c *gin.Context) {
|
|||||||
fr := FilteredResponse(c)
|
fr := FilteredResponse(c)
|
||||||
wallet, _ := c.GetQuery("walletId")
|
wallet, _ := c.GetQuery("walletId")
|
||||||
|
|
||||||
wc.SubscriptionService.GetAll(body, wallet, fr)
|
wc.SubscriptionService.GetAll(c, body, wallet, fr)
|
||||||
|
|
||||||
c.JSON(200, fr)
|
c.JSON(200, fr)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,14 +29,14 @@ func (wc *TransactionTypeController) New(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
wm := wc.TransactionTypeService.New(body)
|
wm := wc.TransactionTypeService.New(c, body)
|
||||||
c.JSON(200, wm)
|
c.JSON(200, wm)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wc *TransactionTypeController) GetAll(c *gin.Context) {
|
func (wc *TransactionTypeController) GetAll(c *gin.Context) {
|
||||||
embed, _ := c.GetQuery("embed")
|
embed, _ := c.GetQuery("embed")
|
||||||
|
|
||||||
wm := wc.TransactionTypeService.GetAll(embed)
|
wm := wc.TransactionTypeService.GetAll(c, embed)
|
||||||
|
|
||||||
c.JSON(200, wm)
|
c.JSON(200, wm)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ func (wc *TransactionController) New(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
wm := wc.TransactionService.New(body)
|
wm := wc.TransactionService.New(c, body)
|
||||||
c.JSON(200, wm)
|
c.JSON(200, wm)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,7 +41,7 @@ func (wc *TransactionController) GetAll(c *gin.Context) {
|
|||||||
fr := FilteredResponse(c)
|
fr := FilteredResponse(c)
|
||||||
wallet, _ := c.GetQuery("walletId")
|
wallet, _ := c.GetQuery("walletId")
|
||||||
|
|
||||||
wc.TransactionService.GetAll(body, wallet, fr)
|
wc.TransactionService.GetAll(c, body, wallet, fr)
|
||||||
|
|
||||||
c.JSON(200, fr)
|
c.JSON(200, fr)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ func (wc *WalletsHeaderController) Get(c *gin.Context) {
|
|||||||
auth := c.MustGet("auth")
|
auth := c.MustGet("auth")
|
||||||
body.Id = auth.(*models.Auth).Id
|
body.Id = auth.(*models.Auth).Id
|
||||||
|
|
||||||
wm := wc.WalletService.GetHeader(body, walletId)
|
wm := wc.WalletService.GetHeader(c, body, walletId)
|
||||||
|
|
||||||
c.JSON(200, wm)
|
c.JSON(200, wm)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ func (wc *WalletsController) New(c *gin.Context) {
|
|||||||
get := c.MustGet("auth")
|
get := c.MustGet("auth")
|
||||||
body.UserID = get.(*models.Auth).Id
|
body.UserID = get.(*models.Auth).Id
|
||||||
|
|
||||||
wm := wc.WalletService.New(body)
|
wm := wc.WalletService.New(c, body)
|
||||||
c.JSON(200, wm)
|
c.JSON(200, wm)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +44,7 @@ func (wc *WalletsController) Get(c *gin.Context) {
|
|||||||
auth := c.MustGet("auth")
|
auth := c.MustGet("auth")
|
||||||
body.Id = auth.(*models.Auth).Id
|
body.Id = auth.(*models.Auth).Id
|
||||||
|
|
||||||
wm := wc.WalletService.Get(body, embed)
|
wm := wc.WalletService.Get(c, body, embed)
|
||||||
|
|
||||||
c.JSON(200, wm)
|
c.JSON(200, wm)
|
||||||
}
|
}
|
||||||
@@ -56,7 +56,7 @@ func (wc *WalletsController) GetAll(c *gin.Context) {
|
|||||||
|
|
||||||
fr := FilteredResponse(c)
|
fr := FilteredResponse(c)
|
||||||
|
|
||||||
wc.WalletService.GetAll(body, fr)
|
wc.WalletService.GetAll(c, body, fr)
|
||||||
|
|
||||||
c.JSON(200, fr)
|
c.JSON(200, fr)
|
||||||
|
|
||||||
|
|||||||
@@ -16,24 +16,26 @@ func SecretCode(c *gin.Context) {
|
|||||||
if secret == "" {
|
if secret == "" {
|
||||||
secret = configs.SecretCode
|
secret = configs.SecretCode
|
||||||
}
|
}
|
||||||
if secret != secretCode {
|
if secret != secretCode.SecretCode {
|
||||||
exceptionReturn.ErrorCode = "401101"
|
exceptionReturn.ErrorCode = "401101"
|
||||||
exceptionReturn.StatusCode = 401
|
exceptionReturn.StatusCode = 401
|
||||||
exceptionReturn.Message = "Invalid secret code"
|
exceptionReturn.Message = "Invalid secret code"
|
||||||
c.AbortWithStatusJSON(exceptionReturn.StatusCode, exceptionReturn)
|
c.AbortWithStatusJSON(exceptionReturn.StatusCode, exceptionReturn)
|
||||||
}
|
}
|
||||||
|
c.Set("migrate", secretCode)
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExtractCode(c *gin.Context) string {
|
func ExtractCode(c *gin.Context) SecretCodeModel {
|
||||||
secret := new(SecretCodeModel)
|
secret := new(SecretCodeModel)
|
||||||
if err := c.ShouldBindJSON(&secret); err != nil {
|
if err := c.ShouldBindJSON(&secret); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return ""
|
return SecretCodeModel{}
|
||||||
}
|
}
|
||||||
return secret.SecretCode
|
return *secret
|
||||||
}
|
}
|
||||||
|
|
||||||
type SecretCodeModel struct {
|
type SecretCodeModel struct {
|
||||||
SecretCode string `json:"secretCode"`
|
SecretCode string `json:"secretCode"`
|
||||||
|
Version string `json:"version"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +1,22 @@
|
|||||||
package migrations
|
package migrate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/go-pg/pg/v10"
|
||||||
"log"
|
"log"
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
|
|
||||||
"github.com/go-pg/pg/v10"
|
|
||||||
"github.com/go-pg/pg/v10/orm"
|
"github.com/go-pg/pg/v10/orm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ApiMigration struct {
|
func CreateTableApi(db pg.DB) error {
|
||||||
Db *pg.DB
|
|
||||||
}
|
|
||||||
|
|
||||||
func (am *ApiMigration) Create() error {
|
|
||||||
models := []interface{}{
|
models := []interface{}{
|
||||||
(*models.ApiModel)(nil),
|
(*models.ApiModel)(nil),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, model := range models {
|
for _, model := range models {
|
||||||
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
err := db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||||
IfNotExists: true,
|
IfNotExists: true,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1,25 +1,22 @@
|
|||||||
package migrations
|
package migrate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/go-pg/pg/v10"
|
||||||
"log"
|
"log"
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
|
|
||||||
"github.com/go-pg/pg/v10"
|
|
||||||
"github.com/go-pg/pg/v10/orm"
|
"github.com/go-pg/pg/v10/orm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UsersMigration struct {
|
|
||||||
Db *pg.DB
|
|
||||||
}
|
|
||||||
|
|
||||||
func (am *UsersMigration) Create() error {
|
func CreateTableUsers(db pg.DB) error {
|
||||||
models := []interface{}{
|
models := []interface{}{
|
||||||
(*models.User)(nil),
|
(*models.User)(nil),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, model := range models {
|
for _, model := range models {
|
||||||
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
err := db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||||
IfNotExists: true,
|
IfNotExists: true,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1,25 +1,21 @@
|
|||||||
package migrations
|
package migrate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/go-pg/pg/v10"
|
||||||
"log"
|
"log"
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
|
|
||||||
"github.com/go-pg/pg/v10"
|
|
||||||
"github.com/go-pg/pg/v10/orm"
|
"github.com/go-pg/pg/v10/orm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WalletsMigration struct {
|
func CreateTableWallets(db pg.DB) error {
|
||||||
Db *pg.DB
|
|
||||||
}
|
|
||||||
|
|
||||||
func (am *WalletsMigration) Create() error {
|
|
||||||
models := []interface{}{
|
models := []interface{}{
|
||||||
(*models.Wallet)(nil),
|
(*models.Wallet)(nil),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, model := range models {
|
for _, model := range models {
|
||||||
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
err := db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||||
IfNotExists: false,
|
IfNotExists: false,
|
||||||
FKConstraints: true,
|
FKConstraints: true,
|
||||||
})
|
})
|
||||||
30
pkg/migrate/4_create_table_transaction_types.go
Normal file
30
pkg/migrate/4_create_table_transaction_types.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package migrate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/go-pg/pg/v10"
|
||||||
|
"log"
|
||||||
|
"wallet-api/pkg/models"
|
||||||
|
|
||||||
|
"github.com/go-pg/pg/v10/orm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateTableTransactionTypes(db pg.DB) error {
|
||||||
|
models := []interface{}{
|
||||||
|
(*models.TransactionType)(nil),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, model := range models {
|
||||||
|
err := db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||||
|
IfNotExists: false,
|
||||||
|
FKConstraints: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error Creating Table: %s", err)
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
fmt.Println("Table created successfully")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,25 +1,22 @@
|
|||||||
package migrations
|
package migrate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/go-pg/pg/v10"
|
||||||
"log"
|
"log"
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
|
|
||||||
"github.com/go-pg/pg/v10"
|
|
||||||
"github.com/go-pg/pg/v10/orm"
|
"github.com/go-pg/pg/v10/orm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TransactionsMigration struct {
|
|
||||||
Db *pg.DB
|
|
||||||
}
|
|
||||||
|
|
||||||
func (am *TransactionsMigration) Create() error {
|
func CreateTableTransactions(db pg.DB) error {
|
||||||
models := []interface{}{
|
models := []interface{}{
|
||||||
(*models.Transaction)(nil),
|
(*models.Transaction)(nil),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, model := range models {
|
for _, model := range models {
|
||||||
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
err := db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||||
IfNotExists: false,
|
IfNotExists: false,
|
||||||
FKConstraints: true,
|
FKConstraints: true,
|
||||||
})
|
})
|
||||||
30
pkg/migrate/6_create_table_subscription_types.go
Normal file
30
pkg/migrate/6_create_table_subscription_types.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package migrate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/go-pg/pg/v10"
|
||||||
|
"log"
|
||||||
|
"wallet-api/pkg/models"
|
||||||
|
|
||||||
|
"github.com/go-pg/pg/v10/orm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateTableSubscriptionTypes(db pg.DB) error {
|
||||||
|
models := []interface{}{
|
||||||
|
(*models.SubscriptionType)(nil),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, model := range models {
|
||||||
|
err := db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||||
|
IfNotExists: false,
|
||||||
|
FKConstraints: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error Creating Table: %s", err)
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
fmt.Println("Table created successfully")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,25 +1,20 @@
|
|||||||
package migrations
|
package migrate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"wallet-api/pkg/models"
|
|
||||||
|
|
||||||
"github.com/go-pg/pg/v10"
|
"github.com/go-pg/pg/v10"
|
||||||
"github.com/go-pg/pg/v10/orm"
|
"github.com/go-pg/pg/v10/orm"
|
||||||
|
"log"
|
||||||
|
"wallet-api/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SubscriptionsMigration struct {
|
func CreateTableSubscriptions(db pg.DB) error {
|
||||||
Db *pg.DB
|
|
||||||
}
|
|
||||||
|
|
||||||
func (am *SubscriptionsMigration) Create() error {
|
|
||||||
models := []interface{}{
|
models := []interface{}{
|
||||||
(*models.Subscription)(nil),
|
(*models.Subscription)(nil),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, model := range models {
|
for _, model := range models {
|
||||||
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
err := db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||||
IfNotExists: false,
|
IfNotExists: false,
|
||||||
FKConstraints: true,
|
FKConstraints: true,
|
||||||
})
|
})
|
||||||
39
pkg/migrate/8_populate_subscription_types.go
Normal file
39
pkg/migrate/8_populate_subscription_types.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package migrate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-pg/pg/v10"
|
||||||
|
"wallet-api/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PopulateSubscriptionTypes(db pg.DB) error {
|
||||||
|
daily := new(models.SubscriptionType)
|
||||||
|
weekly := new(models.SubscriptionType)
|
||||||
|
monthly := new(models.SubscriptionType)
|
||||||
|
yearly := new(models.SubscriptionType)
|
||||||
|
|
||||||
|
daily.Init()
|
||||||
|
daily.Name = "Daily"
|
||||||
|
daily.Type = "daily"
|
||||||
|
|
||||||
|
weekly.Init()
|
||||||
|
weekly.Name = "Weekly"
|
||||||
|
weekly.Type = "weekly"
|
||||||
|
|
||||||
|
monthly.Init()
|
||||||
|
monthly.Name = "Monthly"
|
||||||
|
monthly.Type = "monthly"
|
||||||
|
|
||||||
|
yearly.Init()
|
||||||
|
yearly.Name = "Yearly"
|
||||||
|
yearly.Type = "yearly"
|
||||||
|
|
||||||
|
_, err := db.Model(daily).Where("? = ?", pg.Ident("type"), daily.Type).SelectOrInsert()
|
||||||
|
|
||||||
|
_, err = db.Model(weekly).Where("? = ?", pg.Ident("type"), weekly.Type).SelectOrInsert()
|
||||||
|
|
||||||
|
_, err = db.Model(monthly).Where("? = ?", pg.Ident("type"), monthly.Type).SelectOrInsert()
|
||||||
|
|
||||||
|
_, err = db.Model(yearly).Where("? = ?", pg.Ident("type"), yearly.Type).SelectOrInsert()
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
25
pkg/migrate/9_populate_transaction_types.go
Normal file
25
pkg/migrate/9_populate_transaction_types.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package migrate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-pg/pg/v10"
|
||||||
|
"wallet-api/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PopulateTransactionTypes(db pg.DB) error {
|
||||||
|
gain := new(models.TransactionType)
|
||||||
|
expense := new(models.TransactionType)
|
||||||
|
|
||||||
|
gain.Init()
|
||||||
|
gain.Name = "Gain"
|
||||||
|
gain.Type = "gain"
|
||||||
|
|
||||||
|
expense.Init()
|
||||||
|
expense.Name = "Expense"
|
||||||
|
expense.Type = "expense"
|
||||||
|
|
||||||
|
_, err := db.Model(gain).Where("? = ?", pg.Ident("type"), gain.Type).SelectOrInsert()
|
||||||
|
|
||||||
|
_, err = db.Model(expense).Where("? = ?", pg.Ident("type"), expense.Type).SelectOrInsert()
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -1,30 +1,49 @@
|
|||||||
package migrate
|
package migrate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"wallet-api/pkg/migrate/migrations"
|
|
||||||
|
|
||||||
"github.com/go-pg/pg/v10"
|
"github.com/go-pg/pg/v10"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Start(conn *pg.DB) error {
|
func Start(conn *pg.DB, version string) {
|
||||||
apiMigration := migrations.ApiMigration{Db: conn}
|
migration001 := Migration{
|
||||||
usersMigration := migrations.UsersMigration{Db: conn}
|
Version: "001",
|
||||||
walletsMigration := migrations.WalletsMigration{Db: conn}
|
Migrations: []interface{}{
|
||||||
transactionTypesMigration := migrations.TransactionTypesMigration{Db: conn}
|
CreateTableApi,
|
||||||
transactionsMigration := migrations.TransactionsMigration{Db: conn}
|
CreateTableUsers,
|
||||||
subscriptionTypesMigration := migrations.SubscriptionTypesMigration{Db: conn}
|
CreateTableWallets,
|
||||||
subscriptionsMigration := migrations.SubscriptionsMigration{Db: conn}
|
CreateTableTransactionTypes,
|
||||||
|
CreateTableTransactions,
|
||||||
|
CreateTableSubscriptionTypes,
|
||||||
|
CreateTableSubscriptions,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
migration002 := Migration{
|
||||||
|
Version: "002",
|
||||||
|
Migrations: []interface{}{
|
||||||
|
PopulateSubscriptionTypes,
|
||||||
|
PopulateTransactionTypes,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
err := apiMigration.Create()
|
migrationsMap := []Migration{
|
||||||
err = usersMigration.Create()
|
migration001,
|
||||||
err = walletsMigration.Create()
|
migration002,
|
||||||
err = transactionTypesMigration.Create()
|
}
|
||||||
err = subscriptionTypesMigration.Create()
|
|
||||||
err = subscriptionsMigration.Create()
|
|
||||||
err = transactionsMigration.Create()
|
|
||||||
|
|
||||||
err = subscriptionTypesMigration.Populate()
|
for _, migrationCol := range migrationsMap {
|
||||||
err = transactionTypesMigration.Populate()
|
if version != "" && version == migrationCol.Version || version == "" {
|
||||||
|
for _, migration := range migrationCol.Migrations {
|
||||||
return err
|
mgFunc, isFunc := migration.(func(pg.DB) error)
|
||||||
|
if isFunc {
|
||||||
|
mgFunc(*conn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Migration struct {
|
||||||
|
Version string
|
||||||
|
Migrations []interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,67 +0,0 @@
|
|||||||
package migrations
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"wallet-api/pkg/models"
|
|
||||||
|
|
||||||
"github.com/go-pg/pg/v10"
|
|
||||||
"github.com/go-pg/pg/v10/orm"
|
|
||||||
)
|
|
||||||
|
|
||||||
type SubscriptionTypesMigration struct {
|
|
||||||
Db *pg.DB
|
|
||||||
}
|
|
||||||
|
|
||||||
func (am *SubscriptionTypesMigration) Create() error {
|
|
||||||
models := []interface{}{
|
|
||||||
(*models.SubscriptionType)(nil),
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, model := range models {
|
|
||||||
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
|
||||||
IfNotExists: false,
|
|
||||||
FKConstraints: true,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error Creating Table: %s", err)
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
fmt.Println("Table created successfully")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (am *SubscriptionTypesMigration) Populate() error {
|
|
||||||
daily := new(models.SubscriptionType)
|
|
||||||
weekly := new(models.SubscriptionType)
|
|
||||||
monthly := new(models.SubscriptionType)
|
|
||||||
yearly := new(models.SubscriptionType)
|
|
||||||
|
|
||||||
daily.Init()
|
|
||||||
daily.Name = "Daily"
|
|
||||||
daily.Type = "daily"
|
|
||||||
|
|
||||||
weekly.Init()
|
|
||||||
weekly.Name = "Weekly"
|
|
||||||
weekly.Type = "weekly"
|
|
||||||
|
|
||||||
monthly.Init()
|
|
||||||
monthly.Name = "Monthly"
|
|
||||||
monthly.Type = "monthly"
|
|
||||||
|
|
||||||
yearly.Init()
|
|
||||||
yearly.Name = "Yearly"
|
|
||||||
yearly.Type = "yearly"
|
|
||||||
|
|
||||||
_, err := am.Db.Model(daily).Where("? = ?", pg.Ident("type"), daily.Type).SelectOrInsert()
|
|
||||||
|
|
||||||
_, err = am.Db.Model(weekly).Where("? = ?", pg.Ident("type"), weekly.Type).SelectOrInsert()
|
|
||||||
|
|
||||||
_, err = am.Db.Model(monthly).Where("? = ?", pg.Ident("type"), monthly.Type).SelectOrInsert()
|
|
||||||
|
|
||||||
_, err = am.Db.Model(yearly).Where("? = ?", pg.Ident("type"), yearly.Type).SelectOrInsert()
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
package migrations
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"wallet-api/pkg/models"
|
|
||||||
|
|
||||||
"github.com/go-pg/pg/v10"
|
|
||||||
"github.com/go-pg/pg/v10/orm"
|
|
||||||
)
|
|
||||||
|
|
||||||
type TransactionTypesMigration struct {
|
|
||||||
Db *pg.DB
|
|
||||||
}
|
|
||||||
|
|
||||||
func (am *TransactionTypesMigration) Create() error {
|
|
||||||
models := []interface{}{
|
|
||||||
(*models.TransactionType)(nil),
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, model := range models {
|
|
||||||
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
|
||||||
IfNotExists: false,
|
|
||||||
FKConstraints: true,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error Creating Table: %s", err)
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
fmt.Println("Table created successfully")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (am *TransactionTypesMigration) Populate() error {
|
|
||||||
gain := new(models.TransactionType)
|
|
||||||
expense := new(models.TransactionType)
|
|
||||||
|
|
||||||
gain.Init()
|
|
||||||
gain.Name = "Gain"
|
|
||||||
gain.Type = "gain"
|
|
||||||
|
|
||||||
expense.Init()
|
|
||||||
expense.Name = "Expense"
|
|
||||||
expense.Type = "expense"
|
|
||||||
|
|
||||||
_, err := am.Db.Model(gain).Where("? = ?", pg.Ident("type"), gain.Type).SelectOrInsert()
|
|
||||||
|
|
||||||
_, err = am.Db.Model(expense).Where("? = ?", pg.Ident("type"), expense.Type).SelectOrInsert()
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"wallet-api/pkg/migrate"
|
"wallet-api/pkg/migrate"
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
|
|
||||||
@@ -11,22 +12,21 @@ type ApiService struct {
|
|||||||
Db *pg.DB
|
Db *pg.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *ApiService) GetFirst() models.ApiModel {
|
func (as *ApiService) GetFirst(ctx context.Context) models.ApiModel {
|
||||||
|
db := as.Db.WithContext(ctx)
|
||||||
|
|
||||||
apiModel := models.ApiModel{Api: "Works"}
|
apiModel := models.ApiModel{Api: "Works"}
|
||||||
as.Db.Model(&apiModel).First()
|
db.Model(&apiModel).First()
|
||||||
return apiModel
|
return apiModel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *ApiService) PostMigrate() (*models.MessageResponse, *models.Exception) {
|
func (as *ApiService) PostMigrate(ctx context.Context, version string) (*models.MessageResponse, *models.Exception) {
|
||||||
|
db := as.Db.WithContext(ctx)
|
||||||
|
|
||||||
mr := new(models.MessageResponse)
|
mr := new(models.MessageResponse)
|
||||||
er := new(models.Exception)
|
er := new(models.Exception)
|
||||||
|
|
||||||
err := migrate.Start(as.Db)
|
migrate.Start(db, version)
|
||||||
if err != nil {
|
|
||||||
er.ErrorCode = "400999"
|
|
||||||
er.StatusCode = 400
|
|
||||||
er.Message = err.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
return mr, er
|
return mr, er
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
"wallet-api/pkg/utl/common"
|
"wallet-api/pkg/utl/common"
|
||||||
|
|
||||||
@@ -11,22 +12,26 @@ type SubscriptionTypeService struct {
|
|||||||
Db *pg.DB
|
Db *pg.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *SubscriptionTypeService) New(body *models.NewSubscriptionTypeBody) *models.SubscriptionType {
|
func (as *SubscriptionTypeService) New(ctx context.Context, body *models.NewSubscriptionTypeBody) *models.SubscriptionType {
|
||||||
|
db := as.Db.WithContext(ctx)
|
||||||
|
|
||||||
tm := new(models.SubscriptionType)
|
tm := new(models.SubscriptionType)
|
||||||
|
|
||||||
tm.Init()
|
tm.Init()
|
||||||
tm.Name = body.Name
|
tm.Name = body.Name
|
||||||
tm.Type = body.Type
|
tm.Type = body.Type
|
||||||
|
|
||||||
as.Db.Model(tm).Insert()
|
db.Model(tm).Insert()
|
||||||
|
|
||||||
return tm
|
return tm
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *SubscriptionTypeService) GetAll(embed string) *[]models.SubscriptionType {
|
func (as *SubscriptionTypeService) GetAll(ctx context.Context, embed string) *[]models.SubscriptionType {
|
||||||
|
db := as.Db.WithContext(ctx)
|
||||||
|
|
||||||
wm := new([]models.SubscriptionType)
|
wm := new([]models.SubscriptionType)
|
||||||
|
|
||||||
query := as.Db.Model(wm)
|
query := db.Model(wm)
|
||||||
common.GenerateEmbed(query, embed).Select()
|
common.GenerateEmbed(query, embed).Select()
|
||||||
|
|
||||||
return wm
|
return wm
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"math"
|
"math"
|
||||||
"time"
|
"time"
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
@@ -12,7 +13,9 @@ type SubscriptionService struct {
|
|||||||
Db *pg.DB
|
Db *pg.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *SubscriptionService) New(body *models.NewSubscriptionBody) *models.Subscription {
|
func (as *SubscriptionService) New(ctx context.Context, body *models.NewSubscriptionBody) *models.Subscription {
|
||||||
|
db := as.Db.WithContext(ctx)
|
||||||
|
|
||||||
tm := new(models.Subscription)
|
tm := new(models.Subscription)
|
||||||
|
|
||||||
amount, _ := body.Amount.Float64()
|
amount, _ := body.Amount.Float64()
|
||||||
@@ -33,7 +36,7 @@ func (as *SubscriptionService) New(body *models.NewSubscriptionBody) *models.Sub
|
|||||||
tm.StartDate = time.Now()
|
tm.StartDate = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
tx, _ := as.Db.Begin()
|
tx, _ := db.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
tx.Model(tm).Insert()
|
tx.Model(tm).Insert()
|
||||||
@@ -44,10 +47,12 @@ func (as *SubscriptionService) New(body *models.NewSubscriptionBody) *models.Sub
|
|||||||
return tm
|
return tm
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *SubscriptionService) GetAll(am *models.Auth, walletId string, filtered *models.FilteredResponse) {
|
func (as *SubscriptionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) {
|
||||||
|
db := as.Db.WithContext(ctx)
|
||||||
|
|
||||||
wm := new([]models.Subscription)
|
wm := new([]models.Subscription)
|
||||||
|
|
||||||
tx, _ := as.Db.Begin()
|
tx, _ := db.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
query := tx.Model(wm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id)
|
query := tx.Model(wm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
"wallet-api/pkg/utl/common"
|
"wallet-api/pkg/utl/common"
|
||||||
|
|
||||||
@@ -11,22 +12,26 @@ type TransactionTypeService struct {
|
|||||||
Db *pg.DB
|
Db *pg.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *TransactionTypeService) New(body *models.NewTransactionTypeBody) *models.TransactionType {
|
func (as *TransactionTypeService) New(ctx context.Context, body *models.NewTransactionTypeBody) *models.TransactionType {
|
||||||
|
db := as.Db.WithContext(ctx)
|
||||||
|
|
||||||
tm := new(models.TransactionType)
|
tm := new(models.TransactionType)
|
||||||
|
|
||||||
tm.Init()
|
tm.Init()
|
||||||
tm.Name = body.Name
|
tm.Name = body.Name
|
||||||
tm.Type = body.Type
|
tm.Type = body.Type
|
||||||
|
|
||||||
as.Db.Model(tm).Insert()
|
db.Model(tm).Insert()
|
||||||
|
|
||||||
return tm
|
return tm
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *TransactionTypeService) GetAll(embed string) *[]models.TransactionType {
|
func (as *TransactionTypeService) GetAll(ctx context.Context, embed string) *[]models.TransactionType {
|
||||||
|
db := as.Db.WithContext(ctx)
|
||||||
|
|
||||||
wm := new([]models.TransactionType)
|
wm := new([]models.TransactionType)
|
||||||
|
|
||||||
query := as.Db.Model(wm)
|
query := db.Model(wm)
|
||||||
common.GenerateEmbed(query, embed).Select()
|
common.GenerateEmbed(query, embed).Select()
|
||||||
|
|
||||||
return wm
|
return wm
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"math"
|
"math"
|
||||||
"time"
|
"time"
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
@@ -13,7 +14,9 @@ type TransactionService struct {
|
|||||||
Ss *SubscriptionService
|
Ss *SubscriptionService
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *TransactionService) New(body *models.NewTransactionBody) *models.Transaction {
|
func (as *TransactionService) New(ctx context.Context, body *models.NewTransactionBody) *models.Transaction {
|
||||||
|
db := as.Db.WithContext(ctx)
|
||||||
|
|
||||||
tm := new(models.Transaction)
|
tm := new(models.Transaction)
|
||||||
|
|
||||||
amount, _ := body.Amount.Float64()
|
amount, _ := body.Amount.Float64()
|
||||||
@@ -29,16 +32,18 @@ func (as *TransactionService) New(body *models.NewTransactionBody) *models.Trans
|
|||||||
tm.TransactionDate = time.Now()
|
tm.TransactionDate = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
as.Db.Model(tm).Insert()
|
db.Model(tm).Insert()
|
||||||
|
|
||||||
return tm
|
return tm
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *TransactionService) GetAll(am *models.Auth, walletId string, filtered *models.FilteredResponse) {
|
func (as *TransactionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) {
|
||||||
|
db := as.Db.WithContext(ctx)
|
||||||
|
|
||||||
wm := new([]models.Transaction)
|
wm := new([]models.Transaction)
|
||||||
sm := new([]models.Subscription)
|
sm := new([]models.Subscription)
|
||||||
|
|
||||||
tx, _ := as.Db.Begin()
|
tx, _ := db.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
query2 := tx.Model(sm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id)
|
query2 := tx.Model(sm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
@@ -17,11 +18,13 @@ type UsersService struct {
|
|||||||
Db *pg.DB
|
Db *pg.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (us *UsersService) Create(registerBody *models.User) (*models.User, *models.Exception) {
|
func (us *UsersService) Create(ctx context.Context, registerBody *models.User) (*models.User, *models.Exception) {
|
||||||
|
db := us.Db.WithContext(ctx)
|
||||||
|
|
||||||
check := new(models.User)
|
check := new(models.User)
|
||||||
exceptionReturn := new(models.Exception)
|
exceptionReturn := new(models.Exception)
|
||||||
|
|
||||||
tx, _ := us.Db.Begin()
|
tx, _ := db.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
tx.Model(check).Where("? = ?", pg.Ident("username"), registerBody.Username).WhereOr("? = ?", pg.Ident("email"), registerBody.Email).Select()
|
tx.Model(check).Where("? = ?", pg.Ident("username"), registerBody.Username).WhereOr("? = ?", pg.Ident("email"), registerBody.Email).Select()
|
||||||
@@ -49,12 +52,14 @@ func (us *UsersService) Create(registerBody *models.User) (*models.User, *models
|
|||||||
return registerBody, exceptionReturn
|
return registerBody, exceptionReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (us *UsersService) Login(loginBody *models.Login) (*models.Token, *models.Exception) {
|
func (us *UsersService) Login(ctx context.Context, loginBody *models.Login) (*models.Token, *models.Exception) {
|
||||||
|
db := us.Db.WithContext(ctx)
|
||||||
|
|
||||||
check := new(models.User)
|
check := new(models.User)
|
||||||
exceptionReturn := new(models.Exception)
|
exceptionReturn := new(models.Exception)
|
||||||
tokenPayload := new(models.Token)
|
tokenPayload := new(models.Token)
|
||||||
|
|
||||||
us.Db.Model(check).Where("? = ?", pg.Ident("email"), loginBody.Email).Select()
|
db.Model(check).Where("? = ?", pg.Ident("email"), loginBody.Email).Select()
|
||||||
if check.Email == "" {
|
if check.Email == "" {
|
||||||
exceptionReturn.Message = "Email not found"
|
exceptionReturn.Message = "Email not found"
|
||||||
exceptionReturn.ErrorCode = "400103"
|
exceptionReturn.ErrorCode = "400103"
|
||||||
@@ -84,12 +89,14 @@ func (us *UsersService) Login(loginBody *models.Login) (*models.Token, *models.E
|
|||||||
return tokenPayload, exceptionReturn
|
return tokenPayload, exceptionReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (us *UsersService) Deactivate(auth *models.Auth) (*models.MessageResponse, *models.Exception) {
|
func (us *UsersService) Deactivate(ctx context.Context, auth *models.Auth) (*models.MessageResponse, *models.Exception) {
|
||||||
|
db := us.Db.WithContext(ctx)
|
||||||
|
|
||||||
mm := new(models.MessageResponse)
|
mm := new(models.MessageResponse)
|
||||||
me := new(models.Exception)
|
me := new(models.Exception)
|
||||||
um := new(models.User)
|
um := new(models.User)
|
||||||
|
|
||||||
tx, _ := us.Db.Begin()
|
tx, _ := db.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
err := tx.Model(um).Where("? = ?", pg.Ident("id"), auth.Id).Select()
|
err := tx.Model(um).Where("? = ?", pg.Ident("id"), auth.Id).Select()
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
@@ -14,40 +15,46 @@ type WalletService struct {
|
|||||||
Ss *SubscriptionService
|
Ss *SubscriptionService
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *WalletService) New(am *models.NewWalletBody) *models.Wallet {
|
func (as *WalletService) New(ctx context.Context, am *models.NewWalletBody) *models.Wallet {
|
||||||
|
db := as.Db.WithContext(ctx)
|
||||||
|
|
||||||
walletModel := new(models.Wallet)
|
walletModel := new(models.Wallet)
|
||||||
walletModel.Init()
|
walletModel.Init()
|
||||||
walletModel.UserID = am.UserID
|
walletModel.UserID = am.UserID
|
||||||
walletModel.Name = am.Name
|
walletModel.Name = am.Name
|
||||||
as.Db.Model(walletModel).Insert()
|
db.Model(walletModel).Insert()
|
||||||
return walletModel
|
return walletModel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *WalletService) Get(am *models.Auth, embed string) *models.Wallet {
|
func (as *WalletService) Get(ctx context.Context, am *models.Auth, embed string) *models.Wallet {
|
||||||
|
db := as.Db.WithContext(ctx)
|
||||||
|
|
||||||
wm := new(models.Wallet)
|
wm := new(models.Wallet)
|
||||||
|
|
||||||
query := as.Db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
|
query := db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
|
||||||
common.GenerateEmbed(query, embed).Select()
|
common.GenerateEmbed(query, embed).Select()
|
||||||
|
|
||||||
return wm
|
return wm
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *WalletService) GetAll(am *models.Auth, filtered *models.FilteredResponse) {
|
func (as *WalletService) GetAll(ctx context.Context, am *models.Auth, filtered *models.FilteredResponse) {
|
||||||
|
db := as.Db.WithContext(ctx)
|
||||||
wm := new([]models.Wallet)
|
wm := new([]models.Wallet)
|
||||||
|
|
||||||
query := as.Db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
|
query := db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
|
||||||
FilteredResponse(query, wm, filtered)
|
FilteredResponse(query, wm, filtered)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *WalletService) GetHeader(am *models.Auth, walletId string) *models.WalletHeader {
|
func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletId string) *models.WalletHeader {
|
||||||
|
db := as.Db.WithContext(ctx)
|
||||||
|
|
||||||
wm := new(models.WalletHeader)
|
wm := new(models.WalletHeader)
|
||||||
wallets := new([]models.WalletTransactions)
|
wallets := new([]models.WalletTransactions)
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
transactions := new([]models.Transaction)
|
transactions := new([]models.Transaction)
|
||||||
subscriptions := new([]models.Subscription)
|
subscriptions := new([]models.Subscription)
|
||||||
|
|
||||||
tx, _ := as.Db.Begin()
|
tx, _ := db.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
query2 := tx.Model(subscriptions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType").Relation("SubscriptionType")
|
query2 := tx.Model(subscriptions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType").Relation("SubscriptionType")
|
||||||
@@ -83,22 +90,22 @@ func (as *WalletService) GetHeader(am *models.Auth, walletId string) *models.Wal
|
|||||||
|
|
||||||
for i, wallet := range *wallets {
|
for i, wallet := range *wallets {
|
||||||
for _, trans := range wallet.Transactions {
|
for _, trans := range wallet.Transactions {
|
||||||
tzFirstOfMonthAfterNext := firstOfMonthAfterNext.In(trans.TransactionDate.Location())
|
// tzFirstOfMonthAfterNext := firstOfMonthAfterNext.In(trans.TransactionDate.Location())
|
||||||
tzFirstOfNextMonth := firstOfNextMonth.In(trans.TransactionDate.Location())
|
// tzFirstOfNextMonth := firstOfNextMonth.In(trans.TransactionDate.Location())
|
||||||
tzFirstOfMonth := firstOfMonth.In(trans.TransactionDate.Location())
|
// tzFirstOfMonth := firstOfMonth.In(trans.TransactionDate.Location())
|
||||||
if trans.TransactionDate.Before(tzFirstOfNextMonth) && trans.TransactionDate.After(tzFirstOfMonth) || trans.TransactionDate.Equal(tzFirstOfMonth) {
|
if trans.TransactionDate.Before(firstOfMonth) && trans.TransactionDate.After(firstOfMonth) || trans.TransactionDate.Equal(firstOfMonth) {
|
||||||
if trans.TransactionType.Type == "expense" {
|
if trans.TransactionType.Type == "expense" {
|
||||||
(*wallets)[i].CurrentBalance -= trans.Amount
|
(*wallets)[i].CurrentBalance -= trans.Amount
|
||||||
} else {
|
} else {
|
||||||
(*wallets)[i].CurrentBalance += trans.Amount
|
(*wallets)[i].CurrentBalance += trans.Amount
|
||||||
}
|
}
|
||||||
} else if trans.TransactionDate.Before(tzFirstOfMonthAfterNext) && trans.TransactionDate.After(tzFirstOfNextMonth) {
|
} else if trans.TransactionDate.Before(firstOfMonthAfterNext) && trans.TransactionDate.After(firstOfNextMonth) {
|
||||||
if trans.TransactionType.Type == "expense" {
|
if trans.TransactionType.Type == "expense" {
|
||||||
(*wallets)[i].NextMonth -= trans.Amount
|
(*wallets)[i].NextMonth -= trans.Amount
|
||||||
} else {
|
} else {
|
||||||
(*wallets)[i].NextMonth += trans.Amount
|
(*wallets)[i].NextMonth += trans.Amount
|
||||||
}
|
}
|
||||||
} else if trans.TransactionDate.Before(tzFirstOfMonth) {
|
} else if trans.TransactionDate.Before(firstOfMonth) {
|
||||||
if trans.TransactionType.Type == "expense" {
|
if trans.TransactionType.Type == "expense" {
|
||||||
(*wallets)[i].LastMonth -= trans.Amount
|
(*wallets)[i].LastMonth -= trans.Amount
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,17 +1,19 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"wallet-api/pkg/utl/common"
|
"wallet-api/pkg/utl/common"
|
||||||
|
|
||||||
"github.com/go-pg/pg/v10"
|
"github.com/go-pg/pg/v10"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateConnection(dbUrl string) *pg.DB {
|
func CreateConnection(dbUrl string, ctx context.Context) *pg.DB {
|
||||||
opt, err := pg.ParseURL(dbUrl)
|
opt, err := pg.ParseURL(dbUrl)
|
||||||
common.CheckError(err)
|
common.CheckError(err)
|
||||||
conn := pg.Connect(opt)
|
conn := pg.Connect(opt)
|
||||||
|
db := conn.WithContext(ctx)
|
||||||
|
|
||||||
fmt.Println("Successfully connected!")
|
fmt.Println("Successfully connected!")
|
||||||
return conn
|
return db
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user